@Override public CharSequence subSequence(int start, int end) { int subStart = position + start; if (subStart < position || subStart > limit) throw new IndexOutOfBoundsException(); int subEnd = position + end; if (subEnd < subStart || subEnd > limit) throw new IndexOutOfBoundsException(); if (start == end) return ""; return new ByteBufferBytes(buffer(), subStart, end - start); }
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 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 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 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); }
public static IByteBufferBytes wrap(ByteBuffer buffer) { if (buffer instanceof DirectBuffer) { return new DirectByteBufferBytes(buffer); } return new ByteBufferBytes(buffer.slice()); }
@Override public ByteBufferBytes bytes() { return new ByteBufferBytes(buffer(), start, capacity - start); }
public static IByteBufferBytes wrap(ByteBuffer buffer) { if (buffer instanceof DirectBuffer) { return new DirectByteBufferBytes(buffer); } return new ByteBufferBytes(buffer.slice()); }
@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); }
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 CharSequence subSequence(int start, int end) { int subStart = position + start; if (subStart < position || subStart > limit) throw new IndexOutOfBoundsException(); int subEnd = position + end; if (subEnd < subStart || subEnd > limit) throw new IndexOutOfBoundsException(); if (start == end) return ""; return new ByteBufferBytes(buffer(), subStart, end - start); }
@Override public ByteBufferBytes bytes() { return new ByteBufferBytes(buffer(), start, capacity - start); }
@Override public ByteBufferBytes slice() { return new ByteBufferBytes(buffer(), position, limit - position); }
@Override public ByteBufferBytes slice() { return new ByteBufferBytes(buffer(), position, limit - position); }
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()));
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");