public UncheckedNativeBytes(@NotNull Bytes<Underlying> underlyingBytes)
     throws IllegalStateException {
   underlyingBytes.reserve();
   this.bytesStore = (NativeBytesStore<Underlying>) underlyingBytes.bytesStore();
   assert bytesStore.start() == 0;
   writePosition = underlyingBytes.writePosition();
   writeLimit = underlyingBytes.writeLimit();
   readPosition = underlyingBytes.readPosition();
   capacity = bytesStore.capacity();
 }
예제 #2
0
 public void write(long position, @NotNull CharSequence str, int offset, int length)
     throws BufferOverflowException, IllegalArgumentException, IORuntimeException {
   // todo optimise
   if (str instanceof String) {
     char[] chars = ((String) str).toCharArray();
     ensureCapacity(position + length);
     NativeBytesStore nbs = (NativeBytesStore) bytesStore;
     nbs.write8bit(position, chars, offset, length);
   } else {
     throw new UnsupportedOperationException();
   }
 }
 @Override
 @ForceInline
 public boolean compareAndSwapLong(long offset, long expected, long value)
     throws BufferOverflowException {
   writeCheckOffset(offset, 8);
   return bytesStore.compareAndSwapLong(offset, expected, value);
 }
 @Override
 @ForceInline
 public void write(long offsetInRDO, ByteBuffer bytes, int offset, int length)
     throws BufferOverflowException {
   writeCheckOffset(offsetInRDO, length);
   bytesStore.write(offsetInRDO, bytes, offset, length);
 }
 @NotNull
 @Override
 @ForceInline
 public Bytes<Underlying> writeOrderedInt(long offset, int i) throws BufferOverflowException {
   writeCheckOffset(offset, 4);
   bytesStore.writeOrderedInt(offset, i);
   return this;
 }
 @NotNull
 @Override
 @ForceInline
 public Bytes<Underlying> writeDouble(long offset, double d) throws BufferOverflowException {
   writeCheckOffset(offset, 8);
   bytesStore.writeDouble(offset, d);
   return this;
 }
 @NotNull
 @Override
 @ForceInline
 public Bytes<Underlying> prewriteLong(long i64) {
   long offset = prewriteOffsetPositionMoved(8);
   bytesStore.writeLong(offset, i64);
   return this;
 }
 @NotNull
 @Override
 @ForceInline
 public Bytes<Underlying> writeFloat(float f) {
   long offset = writeOffsetPositionMoved(4);
   bytesStore.writeFloat(offset, f);
   return this;
 }
 @NotNull
 @Override
 @ForceInline
 public Bytes<Underlying> prewriteInt(int i) {
   long offset = prewriteOffsetPositionMoved(4);
   bytesStore.writeInt(offset, i);
   return this;
 }
 @NotNull
 @Override
 @ForceInline
 public Bytes<Underlying> prewrite(byte[] bytes) {
   long offsetInRDO = prewriteOffsetPositionMoved(bytes.length);
   bytesStore.write(offsetInRDO, bytes);
   return this;
 }
 @NotNull
 @Override
 @ForceInline
 public Bytes<Underlying> writeOrderedLong(long i) {
   long offset = writeOffsetPositionMoved(8);
   bytesStore.writeOrderedLong(offset, i);
   return this;
 }
 @NotNull
 @Override
 @ForceInline
 public Bytes<Underlying> prewriteShort(short i16) {
   long offset = prewriteOffsetPositionMoved(2);
   bytesStore.writeShort(offset, i16);
   return this;
 }
 @NotNull
 @Override
 @ForceInline
 public Bytes<Underlying> writeVolatileLong(long offset, long i64) throws BufferOverflowException {
   writeCheckOffset(offset, 8);
   bytesStore.writeVolatileLong(offset, i64);
   return this;
 }
 @NotNull
 @Override
 @ForceInline
 public Bytes<Underlying> writeDouble(double d) {
   long offset = writeOffsetPositionMoved(8);
   bytesStore.writeDouble(offset, d);
   return this;
 }
 @NotNull
 @Override
 @ForceInline
 public Bytes<Underlying> write(byte[] bytes, int offset, int length) {
   long offsetInRDO = writeOffsetPositionMoved(length);
   bytesStore.write(offsetInRDO, bytes, offset, length);
   return this;
 }
 @NotNull
 @Override
 @ForceInline
 public Bytes<Underlying> write(long offsetInRDO, RandomDataInput bytes, long offset, long length)
     throws IORuntimeException, BufferUnderflowException, BufferOverflowException {
   writeCheckOffset(offsetInRDO, length);
   bytesStore.write(offsetInRDO, bytes, offset, length);
   return this;
 }
 @NotNull
 @Override
 @ForceInline
 public Bytes<Underlying> write(long offsetInRDO, byte[] bytes, int offset, int length)
     throws BufferOverflowException {
   writeCheckOffset(offsetInRDO, length);
   bytesStore.write(offsetInRDO, bytes, offset, length);
   return this;
 }
 @NotNull
 @Override
 @ForceInline
 public Bytes<Underlying> writeVolatileShort(long offset, short i16)
     throws BufferOverflowException {
   writeCheckOffset(offset, 2);
   bytesStore.writeVolatileShort(offset, i16);
   return this;
 }
 @NotNull
 @Override
 @ForceInline
 public Bytes<Underlying> write(@NotNull ByteBuffer buffer) {
   bytesStore.write(writePosition, buffer, buffer.position(), buffer.limit());
   writePosition += buffer.remaining();
   assert writePosition <= writeLimit();
   return this;
 }
  @Override
  @ForceInline
  public int peekUnsignedByte() {
    try {
      return readRemaining() > 0 ? bytesStore.readUnsignedByte(readPosition) : -1;

    } catch (BufferUnderflowException | IORuntimeException e) {
      return -1;
    }
  }
예제 #21
0
 @Override
 public boolean equalBytes(BytesStore bytesStore, long length)
     throws BufferUnderflowException, IORuntimeException {
   if (this.bytesStore instanceof NativeBytesStore
       && bytesStore instanceof VanillaBytes
       && bytesStore.bytesStore() instanceof NativeBytesStore) {
     VanillaBytes b2 = (VanillaBytes) bytesStore;
     NativeBytesStore nbs0 = (NativeBytesStore) this.bytesStore;
     NativeBytesStore nbs2 = (NativeBytesStore) b2.bytesStore();
     long i = 0;
     for (; i < length - 7; i++) {
       long addr0 = nbs0.address + readPosition() - nbs0.start() + i;
       long addr2 = nbs2.address + b2.readPosition() - nbs2.start() + i;
       long l0 = nbs0.memory.readLong(addr0);
       long l2 = nbs2.memory.readLong(addr2);
       if (l0 != l2) return false;
     }
     for (; i < length; i++) {
       long offset2 = readPosition() + i - nbs0.start();
       long offset21 = b2.readPosition() + i - nbs2.start();
       byte b0 = nbs0.memory.readByte(nbs0.address + offset2);
       byte b1 = nbs2.memory.readByte(nbs2.address + offset21);
       if (b0 != b1) return false;
     }
     return true;
   } else {
     return super.equalBytes(bytesStore, length);
   }
 }
예제 #22
0
 @Override
 public boolean isEqual(String s) {
   if (s == null || s.length() != readRemaining()) return false;
   char[] chars = StringUtils.extractChars(s);
   if (bytesStore instanceof NativeBytesStore) {
     NativeBytesStore bs = (NativeBytesStore) this.bytesStore;
     long address = bs.address + bs.translate(readPosition);
     Memory memory = bs.memory;
     for (int i = 0; i < chars.length; i++) {
       int b = memory.readByte(address + i) & 0xFF;
       if (b != chars[i]) return false;
     }
   } else {
     try {
       for (int i = 0; i < chars.length; i++) {
         int b = bytesStore.readByte(readPosition + i) & 0xFF;
         if (b != chars[i]) return false;
       }
     } catch (IORuntimeException e) {
       throw new AssertionError(e);
     }
   }
   return true;
 }
  @NotNull
  public Bytes<Underlying> append8bit(@NotNull CharSequence cs)
      throws BufferOverflowException, BufferUnderflowException, IORuntimeException {
    if (cs instanceof BytesStore) {
      return write((BytesStore) cs);
    }
    int length = cs.length();
    long offset = writeOffsetPositionMoved(length);
    long address = bytesStore.address(offset);
    Memory memory = bytesStore.memory;
    assert memory != null;
    for (int i = 0; i < length; i++) {
      char c = cs.charAt(i);
      if (c > 255) c = '?';
      memory.writeByte(address + i, (byte) c);
    }

    return this;
  }
  @Test
  public void testCompareUTF() {
    NativeBytesStore<Void> bs = NativeBytesStore.nativeStore(32);
    bs.writeUtf8(0, "test");
    assertTrue(BytesInternal.compareUtf8(bs, 0, "test"));
    assertFalse(BytesInternal.compareUtf8(bs, 0, null));

    bs.writeUtf8(0, null);
    assertTrue(BytesInternal.compareUtf8(bs, 0, null));
    assertFalse(BytesInternal.compareUtf8(bs, 0, "test"));

    bs.writeUtf8(1, "£€");
    StringBuilder sb = new StringBuilder();
    bs.readUtf8(1, sb);
    assertEquals("£€", sb.toString());
    assertTrue(BytesInternal.compareUtf8(bs, 1, "£€"));
    assertFalse(BytesInternal.compareUtf8(bs, 1, "£"));
    assertFalse(BytesInternal.compareUtf8(bs, 1, "£€$"));
  }
예제 #25
0
 /** @return an elastic wrapper for a direct ByteBuffer which will be resized as required. */
 static Bytes<ByteBuffer> elasticByteBuffer() {
   return NativeBytesStore.elasticByteBuffer().bytesForWrite();
 }
예제 #26
0
 /**
  * Allocate a fixed size buffer read for writing.
  *
  * @param capacity minimum to allocate
  * @return a new Bytes ready for writing.
  */
 static VanillaBytes<Void> allocateDirect(long capacity) throws IllegalArgumentException {
   return NativeBytesStore.nativeStoreWithFixedCapacity(capacity).bytesForWrite();
 }
 @Override
 @ForceInline
 public void nativeWrite(long address, long position, long size) {
   bytesStore.nativeWrite(address, position, size);
 }
 @Override
 @ForceInline
 public void nativeRead(long position, long address, long size) {
   bytesStore.nativeRead(position, address, size);
 }
 @Override
 @ForceInline
 public void nativeWrite(long address, long size) {
   bytesStore.nativeWrite(address, writePosition(), size);
   writeSkip(size);
 }
 @Override
 @ForceInline
 public void nativeRead(long address, long size) {
   bytesStore.nativeRead(readPosition(), address, size);
   readSkip(size);
 }