/** * Writes the given int value (32bit, 4 bytes) to the given position in big endian * byte order. This method's speed depends on the system's native byte order, and it * is possibly slower than {@link #putInt(int, int)}. For most cases (such as * transient storage in memory or serialization for I/O and network), * it suffices to know that the byte order in which the value is written is the same as the * one in which it is read, and {@link #putInt(int, int)} is the preferable choice. * * @param index The position at which the value will be written. * @param value The int value to be written. * * @throws IndexOutOfBoundsException Thrown, if the index is negative, or larger then the segment * size minus 4. */ public final void putIntBigEndian(int index, int value) { if (LITTLE_ENDIAN) { putInt(index, Integer.reverseBytes(value)); } else { putInt(index, value); } }
/** * Writes the given int value (32bit, 4 bytes) to the given position in little endian * byte order. This method's speed depends on the system's native byte order, and it * is possibly slower than {@link #putInt(int, int)}. For most cases (such as * transient storage in memory or serialization for I/O and network), * it suffices to know that the byte order in which the value is written is the same as the * one in which it is read, and {@link #putInt(int, int)} is the preferable choice. * * @param index The position at which the value will be written. * @param value The int value to be written. * * @throws IndexOutOfBoundsException Thrown, if the index is negative, or larger then the segment * size minus 4. */ public final void putIntLittleEndian(int index, int value) { if (LITTLE_ENDIAN) { putInt(index, value); } else { putInt(index, Integer.reverseBytes(value)); } }
/** * Writes the given single-precision float value (32bit, 4 bytes) to the given position in the system's native * byte order. This method offers the best speed for float writing and should be used * unless a specific byte order is required. In most cases, it suffices to know that the * byte order in which the value is written is the same as the one in which it is read * (such as transient storage in memory, or serialization for I/O and network), making this * method the preferable choice. * * @param index The position at which the value will be written. * @param value The float value to be written. * * @throws IndexOutOfBoundsException Thrown, if the index is negative, or larger then the segment * size minus 4. */ public final void putFloat(int index, float value) { putInt(index, Float.floatToRawIntBits(value)); }
segment.putInt(-1, 0); fail("IndexOutOfBoundsException expected"); segment.putInt(pageSize, 0); fail("IndexOutOfBoundsException expected"); segment.putInt(pageSize - 3, 0); fail("IndexOutOfBoundsException expected"); segment.putInt(Integer.MIN_VALUE, 0); fail("IndexOutOfBoundsException expected"); segment.putInt(Integer.MAX_VALUE, 0); fail("IndexOutOfBoundsException expected"); segment.putInt(Integer.MAX_VALUE - 3, 0); fail("IndexOutOfBoundsException expected"); segment.putInt(i, random.nextInt()); segment.putInt(pos, random.nextInt());
segment.putInt(0, 42); fail("Should fail with an exception"); segment.putInt(-1, 42); fail("Should fail with an exception"); segment.putInt(1, 42); fail("Should fail with an exception"); segment.putInt(segment.size(), 42); fail("Should fail with an exception"); segment.putInt(-segment.size(), 42); fail("Should fail with an exception"); segment.putInt(Integer.MAX_VALUE, 42); fail("Should fail with an exception"); segment.putInt(Integer.MIN_VALUE, 42); fail("Should fail with an exception");
/** * Writes the given single-precision float value (32bit, 4 bytes) to the given position in the system's native * byte order. This method offers the best speed for float writing and should be used * unless a specific byte order is required. In most cases, it suffices to know that the * byte order in which the value is written is the same as the one in which it is read * (such as transient storage in memory, or serialization for I/O and network), making this * method the preferable choice. * * @param index The position at which the value will be written. * @param value The float value to be written. * * @throws IndexOutOfBoundsException Thrown, if the index is negative, or larger then the segment * size minus 4. */ public final void putFloat(int index, float value) { putInt(index, Float.floatToRawIntBits(value)); }
segment.putInt(0, 0); fail("IndexOutOfBoundsException expected"); segment.putInt(1, 0); fail("IndexOutOfBoundsException expected"); segment.putInt(-1, 0); fail("IndexOutOfBoundsException expected"); segment.putInt(8, 0); fail("IndexOutOfBoundsException expected"); segment.putInt(-8, 0); fail("IndexOutOfBoundsException expected"); segment.putInt(Integer.MAX_VALUE, 0); fail("IndexOutOfBoundsException expected"); segment.putInt(Integer.MIN_VALUE, 0); fail("IndexOutOfBoundsException expected");
/** * First, reset. */ public void reset() { this.cursor = fixedSize; for (int i = 0; i < nullBitsSizeInBytes; i += 8) { segment.putLong(i, 0L); } this.segment.putInt(0, numElements); }
public static void setInt(MemorySegment[] segments, int offset, int value) { if (segments.length == 1) { segments[0].putInt(offset, value); } else { setIntMultiSeg(segments, offset, value); } }
@Override public void writeInt(int pos, int value) { segment.putInt(getElementOffset(pos, 4), value); }
@Override public void writeInt(int pos, int value) { segment.putInt(getFieldOffset(pos), value); }
public void setNullInt(int ordinal) { setNullBit(ordinal); // put zero into the corresponding field when set null segment.putInt(getElementOffset(ordinal, 4), 0); }
private static void setIntMultiSeg(MemorySegment[] segments, int offset, int value) { int segSize = segments[0].size(); int segIndex = offset / segSize; int segOffset = offset - segIndex * segSize; // equal to % if (segOffset < segSize - 3) { segments[segIndex].putInt(segOffset, value); } else { setIntSlowly(segments, segSize, segIndex, segOffset, value); } }
private void writeSegment(MemorySegment segment, int writePosition, boolean lastSegment) throws IOException { segment.putShort(0, HEADER_MAGIC_NUMBER); segment.putShort(HEADER_FLAGS_OFFSET, lastSegment ? FLAG_LAST_BLOCK : 0); segment.putInt(HEAD_BLOCK_LENGTH_OFFSET, writePosition); this.writer.writeBlock(segment); this.bytesBeforeSegment += writePosition - HEADER_LENGTH; } }
private void writeSegment(MemorySegment segment, int writePosition, boolean lastSegment) throws IOException { segment.putShort(0, HEADER_MAGIC_NUMBER); segment.putShort(HEADER_FLAGS_OFFSET, lastSegment ? FLAG_LAST_BLOCK : 0); segment.putInt(HEAD_BLOCK_LENGTH_OFFSET, writePosition); this.writer.writeBlock(segment); this.bytesBeforeSegment += writePosition - HEADER_LENGTH; } }
public BinaryArrayWriter(BinaryArray array, int numElements, int elementSize) { this.nullBitsSizeInBytes = BinaryArray.calculateHeaderInBytes(numElements); this.fixedSize = roundNumberOfBytesToNearestWord( nullBitsSizeInBytes + elementSize * numElements); this.cursor = fixedSize; this.numElements = numElements; this.segment = MemorySegmentFactory.wrap(new byte[fixedSize]); this.segment.putInt(0, numElements); this.array = array; }
@Override public void setInt(int pos, int value) { assertIndexIsValid(pos); setNotNullAt(pos); segment.putInt(getFieldOffset(pos), value); }
private void writeSegment(MemorySegment segment, int writePosition, boolean lastSegment) throws IOException { segment.putShort(0, HEADER_MAGIC_NUMBER); segment.putShort(HEADER_FLAGS_OFFSET, lastSegment ? FLAG_LAST_BLOCK : 0); segment.putInt(HEAD_BLOCK_LENGTH_OFFSET, writePosition); this.writer.writeBlock(segment); this.bytesBeforeSegment += writePosition - HEADER_LENGTH; } }
private void writeSegment(MemorySegment segment, int writePosition, boolean lastSegment) throws IOException { segment.putShort(0, HEADER_MAGIC_NUMBER); segment.putShort(HEADER_FLAGS_OFFSET, lastSegment ? FLAG_LAST_BLOCK : 0); segment.putInt(HEAD_BLOCK_LENGTH_OFFSET, writePosition); this.writer.writeBlock(segment); this.bytesBeforeSegment += writePosition - HEADER_LENGTH; } }
public static BinaryMap valueOf(BinaryArray key, BinaryArray value) { checkArgument(key.getSegments().length == 1 && value.getSegments().length == 1); byte[] bytes = new byte[4 + key.getSizeInBytes() + value.getSizeInBytes()]; MemorySegment segment = MemorySegmentFactory.wrap(bytes); segment.putInt(0, key.getSizeInBytes()); key.getSegments()[0].copyTo(key.getBaseOffset(), segment, 4, key.getSizeInBytes()); value.getSegments()[0].copyTo( value.getBaseOffset(), segment, 4 + key.getSizeInBytes(), value.getSizeInBytes()); BinaryMap map = new BinaryMap(); map.pointTo(segment, 0, bytes.length); return map; }