Skip to content

Commit

Permalink
Adds changes based on PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
popematt committed Dec 1, 2023
1 parent 02beb8d commit f8fb6dd
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static final boolean isSurrogate(int c) {
(byte) 0xEA };

/**
* The byte sequence indicating use of Ion 1.0 binary format.
* The byte sequence indicating use of Ion 1.1 binary format.
*/
public static final byte[] BINARY_VERSION_MARKER_1_1 = { (byte) 0xE0,
(byte) 0x01,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class IonRawBinaryWriter_1_1 internal constructor(
override fun writeIVM() {
confirm(currentContainer.type == Top) { "IVM can only be written at the top level of an Ion stream." }
confirm(numAnnotations == 0) { "Cannot write an IVM with annotations" }
buffer.writeBytes(Ion_1_1_Constants.IVM)
buffer.writeBytes(_Private_IonConstants.BINARY_VERSION_MARKER_1_1)
}

override fun writeAnnotations(annotation0: Int) {
Expand Down Expand Up @@ -299,7 +299,7 @@ class IonRawBinaryWriter_1_1 internal constructor(
if (contentLength <= 0xF) {
// Clean up any unused space that was pre-allocated.
buffer.shiftBytesLeft(currentContainer.length.toInt(), lengthPrefixPreallocation)
buffer.writeUInt8At(currentContainer.position, 0xA0L or contentLength)
buffer.writeUInt8At(currentContainer.position, OpCodes.LIST_ZERO_LENGTH + contentLength)
} else {
val lengthPrefixBytesRequired = FlexInt.flexUIntLength(contentLength)
thisContainerTotalLength += lengthPrefixBytesRequired
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/amazon/ion/impl/bin/Ion_1_1_Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
public class Ion_1_1_Constants {
private Ion_1_1_Constants() {}

static final byte[] IVM = new byte[] { (byte) 0xE0, 0x01, 0x01, (byte) 0xEA };

static final int FIRST_2_BYTE_SYMBOL_ADDRESS = 256;
static final int FIRST_MANY_BYTE_SYMBOL_ADDRESS = 65792;
public static final int MAX_NANOSECONDS = 999999999;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/amazon/ion/impl/bin/OpCodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ private OpCodes() {}

public static final byte INLINE_SYMBOL_ZERO_LENGTH = (byte) 0x90;

public static final byte LIST_ZERO_LENGTH = (byte) 0xA0;

public static final byte SYMBOL_ADDRESS_1_BYTE = (byte) 0xE1;
public static final byte SYMBOL_ADDRESS_2_BYTES = (byte) 0xE2;
public static final byte SYMBOL_ADDRESS_MANY_BYTES = (byte) 0xE3;
Expand Down
26 changes: 18 additions & 8 deletions src/main/java/com/amazon/ion/impl/bin/WriteBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,17 @@ public void truncate(final long position)
* There is no guarantee as to what values the reserved bytes will have.
* Only use this method if you will overwrite the bytes later with valid data, or if you have already written dato
* to these bytes.
*
* Returns the position of the first reserved byte.
*/
public void reserve(int numBytes) {
public long reserve(int numBytes) {
long startOfReservedBytes = position();
// It would also fit in the current block if numBytes == current.remaining(), but then we would have to
// increment `index` and check whether to allocate a new block. So, we'll optimize the early return for the most
// common situation, and lump the == case into the slower path.
if (numBytes < current.remaining()) {
current.limit += numBytes;
return;
return startOfReservedBytes;
}

while (numBytes > 0) {
Expand All @@ -130,6 +136,7 @@ public void reserve(int numBytes) {
current = blocks.get(index);
}
}
return startOfReservedBytes;
}

/** Returns the amount of capacity left in the current block. */
Expand Down Expand Up @@ -1316,8 +1323,9 @@ public void writeLowerNibbleAt(final long position, final long value) {
/** Writes a FlexInt to this WriteBuffer, returning the number of bytes that were needed to encode the value */
public int writeFlexInt(final long value) {
int numBytes = FlexInt.flexIntLength(value);
writeFlexIntOrUIntAt(position(), value, numBytes);
reserve(numBytes);
// writeFlexIntOrUIntAt does not advance index or limit, so we reserve the bytes, and then write out the number
long position = reserve(numBytes);
writeFlexIntOrUIntAt(position, value, numBytes);
return numBytes;
}

Expand All @@ -1327,8 +1335,9 @@ public int writeFlexUInt(final int value) {
throw new IllegalArgumentException("Attempted to write a FlexUInt for " + value);
}
int numBytes = FlexInt.flexUIntLength(value);
writeFlexIntOrUIntAt(position(), value, numBytes);
reserve(numBytes);
// writeFlexIntOrUIntAt does not advance index or limit, so we reserve the bytes, and then write out the number
long position = reserve(numBytes);
writeFlexIntOrUIntAt(position, value, numBytes);
return numBytes;
}

Expand All @@ -1338,8 +1347,9 @@ public int writeFlexUInt(final long value) {
throw new IllegalArgumentException("Attempted to write a FlexUInt for " + value);
}
int numBytes = FlexInt.flexUIntLength(value);
writeFlexIntOrUIntAt(position(), value, numBytes);
reserve(numBytes);
// writeFlexIntOrUIntAt does not advance index or limit, so we reserve the bytes, and then write out the number
long position = reserve(numBytes);
writeFlexIntOrUIntAt(position, value, numBytes);
return numBytes;
}

Expand Down

0 comments on commit f8fb6dd

Please sign in to comment.