diff --git a/src/main/java/com/amazon/ion/impl/UnifiedSavePointManagerX.java b/src/main/java/com/amazon/ion/impl/UnifiedSavePointManagerX.java index aa8bddf0d..fe3187411 100644 --- a/src/main/java/com/amazon/ion/impl/UnifiedSavePointManagerX.java +++ b/src/main/java/com/amazon/ion/impl/UnifiedSavePointManagerX.java @@ -1,18 +1,5 @@ -/* - * Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ - +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 package com.amazon.ion.impl; @@ -248,11 +235,22 @@ private final void save_point_mark_end(SavePoint sp, int offset) { curr_idx++; curr = _buffer.getPage(curr_idx); } - else if (curr_pos < curr.getStartingOffset()) { - int pos_offset = curr_pos - curr.getOriginalStartingOffset(); - curr_idx--; - curr = _buffer.getPage(curr_idx); - curr_pos = curr.getBufferLimit() - pos_offset; + else { + // The end point can be on an earlier page, even several pages back: each refill takes + // a new page while a save point is open, so a page may hold only a few characters. + // Step back a page at a time, but not before the page the save point starts in. + while (curr_pos < curr.getStartingOffset()) { + if (curr_idx <= sp.getStartIdx()) { + end_point_too_far(curr_idx); + } + // How far the end point is before this page's first character (negative). This + // page starts where the previous one ends, so that is how far it is before the + // previous page's limit. + int pos_offset = curr_pos - curr.getOriginalStartingOffset(); + curr_idx--; + curr = _buffer.getPage(curr_idx); + curr_pos = curr.getBufferLimit() + pos_offset; + } } if (curr == null || curr_pos >= curr.getBufferLimit() || curr_pos < curr.getStartingOffset()) { end_point_too_far(curr_idx); @@ -264,11 +262,11 @@ else if (curr_pos < curr.getStartingOffset()) { return; } private final void end_point_too_far(int curr_idx) { - String message = "end point [" + String message = "end point is outside the pages held for the save point (reached page [" + curr_idx - + "] must be within 1 page of current [" + + "], current page [" + _buffer.getCurrentPageIdx() - + "]"; + + "])"; throw new IllegalArgumentException(message); } diff --git a/src/test/java/com/amazon/ion/impl/IonReaderTextFastPathsTest.java b/src/test/java/com/amazon/ion/impl/IonReaderTextFastPathsTest.java index bb336d46b..b051203ad 100644 --- a/src/test/java/com/amazon/ion/impl/IonReaderTextFastPathsTest.java +++ b/src/test/java/com/amazon/ion/impl/IonReaderTextFastPathsTest.java @@ -21,7 +21,9 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.function.Executable; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.EnumSource; +import org.junit.jupiter.params.provider.MethodSource; import java.io.ByteArrayInputStream; import java.io.FilterInputStream; @@ -33,6 +35,8 @@ import java.util.Base64; import java.util.List; import java.util.Random; +import java.util.stream.IntStream; +import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -90,8 +94,12 @@ public int read(byte[] b, int off, int len) throws IOException { } private static IonDatagram load(Source source, String text) { + return load(source.open(text)); + } + + private static IonDatagram load(IonReader input) { IonDatagram datagram = SYSTEM.newDatagram(); - try (IonReader reader = source.open(text)) { + try (IonReader reader = input) { IonWriter writer = SYSTEM.newWriter(datagram); writer.writeValues(reader); writer.close(); @@ -152,6 +160,88 @@ public void blobsDecodeTheSameWithWhitespaceAndPadding(Source source) { } } + /** Each text with every number of bytes per read, from 1 to the text's length. */ + private static Stream everyReadSize(String... texts) { + return Arrays.stream(texts).flatMap(text -> IntStream.rangeClosed(1, text.getBytes(StandardCharsets.UTF_8).length) + .mapToObj(bytesPerRead -> Arguments.of(text, bytesPerRead))); + } + + private static IonReader inReadsOf(int bytesPerRead, String text) { + return SYSTEM.newReader(new LimitedReads(text.getBytes(StandardCharsets.UTF_8), bytesPerRead)); + } + + private static Stream tripleQuotedText() { + return everyReadSize("{{'''abc'''}}", "{{''''''}}", "{{'''a''b''' '''c'''}} 7", "{a:{{'''abc'''}}, b:[{{'''d'''}}]}", + "{'''abc''':1}"); + } + + /** + * Triple-quoted clobs and field names read the same wherever pages end around their closing quotes. After the + * closing quotes, the reader marks the end of the text at the first of them, which can be on an earlier page, or + * several pages back when each read returns a few bytes. See: https://github.com/amazon-ion/ion-java/issues/1187 + */ + @ParameterizedTest(name = "{0} read {1} bytes at a time") + @MethodSource("tripleQuotedText") + public void tripleQuotedTextReadsTheSameWhereverPagesEnd(String text, int bytesPerRead) { + assertEquals(load(Source.BYTES, text), load(inReadsOf(bytesPerRead, text))); + } + + private static Stream malformedTripleQuotedText() { + return everyReadSize("{{'''abc''", "{'''abc''"); + } + + /** Unterminated triple-quoted text throws the same IonException wherever pages end. */ + @ParameterizedTest(name = "{0} read {1} bytes at a time") + @MethodSource("malformedTripleQuotedText") + public void malformedTripleQuotedTextThrowsTheSameWhereverPagesEnd(String text, int bytesPerRead) { + IonException expected = assertThrows(IonException.class, () -> load(Source.BYTES, text)); + IonException actual = assertThrows(IonException.class, () -> load(inReadsOf(bytesPerRead, text))); + assertEquals(expected.getClass(), actual.getClass()); + } + + private static Stream tripleQuotedClob() { + return everyReadSize("{{'''abc'''}}"); + } + + /** newBytes(), byteSize() and getBytes() return a triple-quoted clob's bytes wherever pages end. */ + @ParameterizedTest(name = "{0} read {1} bytes at a time") + @MethodSource("tripleQuotedClob") + public void tripleQuotedClobBytesWhereverPagesEnd(String text, int bytesPerRead) throws IOException { + byte[] expected; + try (IonReader reader = Source.BYTES.open(text)) { + assertEquals(IonType.CLOB, reader.next()); + expected = reader.newBytes(); + } + try (IonReader reader = inReadsOf(bytesPerRead, text)) { + assertEquals(IonType.CLOB, reader.next()); + assertArrayEquals(expected, reader.newBytes()); + } + try (IonReader reader = inReadsOf(bytesPerRead, text)) { + assertEquals(IonType.CLOB, reader.next()); + assertEquals(expected.length, reader.byteSize()); + } + try (IonReader reader = inReadsOf(bytesPerRead, text)) { + assertEquals(IonType.CLOB, reader.next()); + byte[] buffer = new byte[expected.length]; + assertEquals(expected.length, reader.getBytes(buffer, 0, buffer.length)); + assertArrayEquals(expected, buffer); + } + } + + /** Triple-quoted clobs longer than a page read the same when their closing quotes straddle the end of a page. */ + @ParameterizedTest + @EnumSource(value = Source.class, names = "STREAM_1_BYTE_READS", mode = EnumSource.Mode.EXCLUDE) + public void tripleQuotedClobsEndingAcrossAPageEnd(Source source) throws IOException { + for (int length = PAGE - 40; length <= PAGE; length++) { + String content = repeat("x", length); + try (IonReader reader = source.open("{{'''" + content + "'''}} 7")) { + assertEquals(IonType.CLOB, reader.next(), "length " + length); + assertArrayEquals(content.getBytes(StandardCharsets.US_ASCII), reader.newBytes(), "length " + length); + assertEquals(IonType.INT, reader.next(), "length " + length); + } + } + } + @ParameterizedTest @EnumSource(value = Source.class, names = "STREAM_1_BYTE_READS", mode = EnumSource.Mode.EXCLUDE) public void skippedValuesLeaveTheReaderOnTheNextValue(Source source) { @@ -334,12 +424,9 @@ public void nonAsciiInBase64IsRejectedAsBefore(Source source) { @ParameterizedTest @EnumSource(Source.class) public void malformedValuesLeaveTheReaderUsable(Source source) throws IOException { - List lobs = new ArrayList<>(Arrays.asList("{{invalid_blob}} 7", "{{\"x\\q\"}} 7")); + List lobs = new ArrayList<>(Arrays.asList("{{invalid_blob}} 7", "{{\"x\\q\"}} 7", "{{'''x\\q'''}} 7")); List fieldNames = new ArrayList<>(Arrays.asList("{'x\\q':1, b:2} 7", "{\"x\\q\":1, b:2} 7")); if (source != Source.STREAM_1_BYTE_READS) { - // A triple-quoted clob read a byte at a time fails even when it is valid. - // See: https://github.com/amazon-ion/ion-java/issues/1187 - lobs.add("{{'''x\\q'''}} 7"); // Values longer than a page. lobs.add("{{" + repeat("A", 2 * PAGE) + "_}} 7"); lobs.add("{{\"" + repeat("x", 2 * PAGE) + "\\q\"}} 7");