/** * Reads a character value (16 bit, 2 bytes) from 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 #getChar(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 #getChar(int)} is the preferable choice. * * @param index The position from which the value will be read. * @return The character value at the given position. * * @throws IndexOutOfBoundsException Thrown, if the index is negative, or larger then the segment size minus 2. */ public final char getCharLittleEndian(int index) { if (LITTLE_ENDIAN) { return getChar(index); } else { return Character.reverseBytes(getChar(index)); } }
/** * Reads a character value (16 bit, 2 bytes) from 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 #getChar(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 #getChar(int)} is the preferable choice. * * @param index The position from which the value will be read. * @return The character value at the given position. * * @throws IndexOutOfBoundsException Thrown, if the index is negative, or larger then the segment size minus 2. */ public final char getCharBigEndian(int index) { if (LITTLE_ENDIAN) { return Character.reverseBytes(getChar(index)); } else { return getChar(index); } }
segment.getChar(-1); fail("IndexOutOfBoundsException expected"); segment.getChar(pageSize); fail("IndexOutOfBoundsException expected"); segment.getChar(Integer.MIN_VALUE); fail("IndexOutOfBoundsException expected"); segment.getChar(Integer.MAX_VALUE); fail("IndexOutOfBoundsException expected"); segment.getChar(Integer.MAX_VALUE - 1); fail("IndexOutOfBoundsException expected"); assertEquals((char) (random.nextInt(Character.MAX_VALUE)), segment.getChar(i)); assertEquals((char) (random.nextInt(Character.MAX_VALUE)), segment.getChar(pos));
segment.getChar(0); fail("Should fail with an exception"); segment.getChar(-1); fail("Should fail with an exception"); segment.getChar(1); fail("Should fail with an exception"); segment.getChar(segment.size()); fail("Should fail with an exception"); segment.getChar(-segment.size()); fail("Should fail with an exception"); segment.getChar(Integer.MAX_VALUE); fail("Should fail with an exception"); segment.getChar(Integer.MIN_VALUE); fail("Should fail with an exception");
/** * Reads a character value (16 bit, 2 bytes) from 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 #getChar(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 #getChar(int)} is the preferable choice. * * @param index The position from which the value will be read. * @return The character value at the given position. * * @throws IndexOutOfBoundsException Thrown, if the index is negative, or larger then the segment size minus 2. */ public final char getCharLittleEndian(int index) { if (LITTLE_ENDIAN) { return getChar(index); } else { return Character.reverseBytes(getChar(index)); } }
segment.getChar(0); fail("IndexOutOfBoundsException expected"); segment.getChar(1); fail("IndexOutOfBoundsException expected"); segment.getChar(-1); fail("IndexOutOfBoundsException expected"); segment.getChar(8); fail("IndexOutOfBoundsException expected"); segment.getChar(-8); fail("IndexOutOfBoundsException expected"); segment.getChar(Integer.MAX_VALUE); fail("IndexOutOfBoundsException expected"); segment.getChar(Integer.MIN_VALUE); fail("IndexOutOfBoundsException expected");
/** * Reads a character value (16 bit, 2 bytes) from 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 #getChar(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 #getChar(int)} is the preferable choice. * * @param index The position from which the value will be read. * @return The character value at the given position. * * @throws IndexOutOfBoundsException Thrown, if the index is negative, or larger then the segment size minus 2. */ public final char getCharLittleEndian(int index) { if (LITTLE_ENDIAN) { return getChar(index); } else { return Character.reverseBytes(getChar(index)); } }
/** * Reads a character value (16 bit, 2 bytes) from 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 #getChar(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 #getChar(int)} is the preferable choice. * * @param index The position from which the value will be read. * @return The character value at the given position. * * @throws IndexOutOfBoundsException Thrown, if the index is negative, or larger then the segment size minus 2. */ public final char getCharBigEndian(int index) { if (LITTLE_ENDIAN) { return Character.reverseBytes(getChar(index)); } else { return getChar(index); } }
/** * Reads a character value (16 bit, 2 bytes) from 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 #getChar(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 #getChar(int)} is the preferable choice. * * @param index The position from which the value will be read. * @return The character value at the given position. * * @throws IndexOutOfBoundsException Thrown, if the index is negative, or larger then the segment size minus 2. */ public final char getCharBigEndian(int index) { if (LITTLE_ENDIAN) { return Character.reverseBytes(getChar(index)); } else { return getChar(index); } }
public static char getChar(MemorySegment[] segments, int offset) { if (segments.length == 1) { return segments[0].getChar(offset); } else { return getCharMultiSeg(segments, offset); } }
@Override public char getChar(int pos) { assertIndexIsValid(pos); return segment.getChar(getFieldOffset(pos)); }
private static char getCharMultiSeg(MemorySegment[] segments, int offset) { int segSize = segments[0].size(); int segIndex = offset / segSize; int segOffset = offset - segIndex * segSize; // equal to % if (segOffset < segSize - 1) { return segments[segIndex].getChar(segOffset); } else { return (char) get2ByteSlowly(segments, segSize, segIndex, segOffset); } }