@Override public ByteBufferBytes slice(long offset, long length) { long sliceStart = position + offset; assert sliceStart >= start && sliceStart < capacity; long sliceEnd = sliceStart + length; assert sliceEnd > sliceStart && sliceEnd <= capacity; return new ByteBufferBytes(buffer(), (int) sliceStart, (int) length); }
@Override public long readVolatileLong() { readBarrier(); return readLong(); }
@Override public void readFully(long offset, byte[] bytes, int off, int len) { checkArrayOffs(bytes.length, off, len); long left = remaining(); if (left < len) throw new IllegalStateException(new EOFException()); for (int i = 0; i < len; i++) bytes[off + i] = readByte(offset + i); }
ByteBuffer wrap = ByteBuffer.allocate(1024); ByteBufferBytes bufferBytes = new ByteBufferBytes(wrap); byte[] bytes = "BAC,12.32,12.54,12.56,232443".getBytes(); int runs = 10000000; long start = System.nanoTime(); for (int i = 0; i < runs; i++) { bufferBytes.reset(); // read the next message. bufferBytes.write(bytes); bufferBytes.position(0); // decode message String word = bufferBytes.parseUTF(StopCharTesters.COMMA_STOP); double low = bufferBytes.parseDouble(); double curr = bufferBytes.parseDouble(); double high = bufferBytes.parseDouble(); long sequence = bufferBytes.parseLong(); if (i == 0) { assertEquals("BAC", word); assertEquals(12.32, low, 0.0); assertEquals(12.54, curr, 0.0); assertEquals(12.56, high, 0.0); assertEquals(232443, sequence); } } long time = System.nanoTime() - start; System.out.println("Average time was " + time / runs + " nano-seconds");
File tmpFile = new File(System.getProperty("java.io.tmpdir"), "lock-test.dat"); FileChannel fc = new RandomAccessFile(tmpFile, "rw").getChannel(); MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0, RECORDS * RECORD_SIZE); ByteBufferBytes bytes = new ByteBufferBytes(mbb.order(ByteOrder.nativeOrder())); // the lock and toggle. bytes.busyLockLong(recordOffset + LOCK); try { boolean flag = bytes.readBoolean(recordOffset + FLAG); if (flag == toggleTo) { if (t % 100 == 0) System.out.println("j: " + j + " is " + flag); continue; } bytes.writeBoolean(recordOffset + FLAG, toggleTo); break; } finally { bytes.unlockLong(recordOffset + LOCK); }
ByteBufferBytes bbb = new ByteBufferBytes(fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).order(ByteOrder.nativeOrder())); while (bbb.remaining() > 0) { long index = bbb.readLong(); if (index == 0) { pw.println(count + ": unset");
public static IByteBufferBytes wrap(ByteBuffer buffer, int start, int capacity) { if (buffer instanceof DirectBuffer) { return new DirectByteBufferBytes(buffer, start, capacity); } return new ByteBufferBytes(buffer.slice(), start, capacity); }
@Override public void readFully(@NotNull char[] data, int off, int len) { checkArrayOffs(data.length, off, len); long left = remaining(); if (left < len * 2L) throw new IllegalStateException(new EOFException()); for (int i = 0; i < len; i++) data[off + i] = readChar(); }
public static String asString(ByteBuffer bb) { IByteBufferBytes wrap = ByteBufferBytes.wrap(bb); wrap.position(bb.position()); wrap.limit(bb.limit()); return wrap.toDebugString(); }
@Override public void checkEndOfBuffer() throws IndexOutOfBoundsException { if (position < start || position > limit()) { throw new IndexOutOfBoundsException(); } }
@Override public Bytes zeroOut() { clear(); int i = start; for (; i < capacity - 7; i++) buffer.putLong(i, 0L); for (; i < capacity; i++) buffer.put(i, (byte) 0); return this; }
public static IByteBufferBytes wrap(ByteBuffer buffer) { if (buffer instanceof DirectBuffer) { return new DirectByteBufferBytes(buffer); } return new ByteBufferBytes(buffer.slice()); }
@Override public void readFully(@NotNull char[] data, int off, int len) { checkArrayOffs(data.length, off, len); long left = remaining(); if (left < len * 2L) throw new IllegalStateException(new EOFException()); for (int i = 0; i < len; i++) data[off + i] = readChar(); }
Bytes bytes = ByteBufferBytes.wrap(mbb);
@Override public void checkEndOfBuffer() throws IndexOutOfBoundsException { if (position < start || position > limit()) { throw new IndexOutOfBoundsException(); } }
@Override public Bytes zeroOut() { clear(); int i = start; for (; i < capacity - 7; i++) buffer.putLong(i, 0L); for (; i < capacity; i++) buffer.put(i, (byte) 0); return this; }
@Override public ByteBufferBytes bytes(long offset, long length) { long sliceStart = start + offset; assert sliceStart >= start && sliceStart < capacity; long sliceEnd = sliceStart + length; assert sliceEnd > sliceStart && sliceEnd <= capacity; return new ByteBufferBytes(buffer(), (int) sliceStart, (int) length); }
@Override public void readFully(long offset, byte[] bytes, int off, int len) { checkArrayOffs(bytes.length, off, len); long left = remaining(); if (left < len) throw new IllegalStateException(new EOFException()); for (int i = 0; i < len; i++) bytes[off + i] = readByte(offset + i); }
@Override public long readVolatileLong() { readBarrier(); return readLong(); }
public static IByteBufferBytes wrap(ByteBuffer buffer, int start, int capacity) { if (buffer instanceof DirectBuffer) { return new DirectByteBufferBytes(buffer, start, capacity); } return new ByteBufferBytes(buffer.slice(), start, capacity); }