public static CharBuffer newCharBuffer (int numChars) { ByteBuffer buffer = ByteBuffer.allocateDirect(numChars * 2); buffer.order(ByteOrder.nativeOrder()); return buffer.asCharBuffer(); }
public static CharBuffer newCharBuffer (int numChars) { ByteBuffer buffer = ByteBuffer.allocateDirect(numChars * 2); buffer.order(ByteOrder.nativeOrder()); return buffer.asCharBuffer(); }
public static void putStringAsCharArray(ByteBuffer byteBuffer, String s) { byteBuffer.asCharBuffer().put(s); byteBuffer.position(byteBuffer.position() + (s.length() * 2)); }
/** @return {@code asByteBuffer().asCharBuffer()} */ @Override public final CharBuffer asBuffer() { return asByteBuffer().asCharBuffer(); } }
buffer.asCharBuffer().get( charArray, offset, length ); offset += length; buffer.clear();
private static void writeChars( StoreChannel channel, ByteBuffer buffer, char[] chars ) throws IOException { int position = 0; do { buffer.clear(); int leftToWrite = chars.length - position; if ( leftToWrite * 2 < buffer.capacity() ) { buffer.asCharBuffer().put( chars, position, leftToWrite ); buffer.limit( leftToWrite * 2); channel.write( buffer ); position += leftToWrite; } else { int length = buffer.capacity() / 2; buffer.asCharBuffer().put( chars, position, length ); buffer.limit( length * 2 ); channel.write( buffer ); position += length; } } while ( position < chars.length ); }
@MarshalsArray public static CharBuffer toCharBuffer(Class<?> cls, long handle, long flags, int d1) { return VM.newDirectByteBuffer(handle, d1 << 1).order(ByteOrder.nativeOrder()).asCharBuffer(); } @MarshalsArray
/** * Returns a {@link CharBuffer} which reads and writes to the same memory * location pointed to by this {@link CharPtr}. * * @param n the maximum number of chars the {@link CharBuffer} can * read/write. This will be the {@link CharBuffer}'s * <code>capacity</code>. * @return the {@link ShortBuffer}. */ public CharBuffer asCharBuffer(int n) { return as(BytePtr.class).asByteBuffer(n << 1).order(ByteOrder.nativeOrder()).asCharBuffer(); }
private void advance() throws IOException { assert !endOfInput; assert !bbuf.hasRemaining() : "advance() should be called when output byte buffer is empty. bbuf: " + bbuf + ", as string: " + bbuf.asCharBuffer().toString(); assert cbuf.remaining() < 2; // given that bbuf.capacity = 3 x cbuf.capacity, the only time that we should have a // remaining char is if the last char read was the 1st half of a surrogate pair if (cbuf.remaining() == 0) { cbuf.clear(); } else { cbuf.compact(); } int n = reader.read(cbuf); // read #1 cbuf.flip(); CoderResult result; endOfInput = n == -1; bbuf.clear(); result = encoder.encode(cbuf, bbuf, endOfInput); checkEncodeResult(result); if (endOfInput) { result = encoder.flush(bbuf); checkEncodeResult(result); } bbuf.flip(); }
public class Server { public static void main( String[] args ) throws Throwable { File f = new File( FILE_NAME ); FileChannel channel = FileChannel.open( f.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE ); MappedByteBuffer b = channel.map( MapMode.READ_WRITE, 0, 4096 ); CharBuffer charBuf = b.asCharBuffer(); char[] string = "Hello client\0".toCharArray(); charBuf.put( string ); System.out.println( "Waiting for client." ); while( charBuf.get( 0 ) != '\0' ); System.out.println( "Finished waiting." ); } }
public class Client { public static void main( String[] args ) throws Throwable { File f = new File( FILE_NAME ); FileChannel channel = FileChannel.open( f.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE ); MappedByteBuffer b = channel.map( MapMode.READ_WRITE, 0, 4096 ); CharBuffer charBuf = b.asCharBuffer(); // Prints 'Hello server' char c; while( ( c = charBuf.get() ) != 0 ) { System.out.print( c ); } System.out.println(); charBuf.put( 0, '\0' ); } }
/** * {@inheritDoc} */ @Override public final CharBuffer asCharBuffer() { return buf().asCharBuffer(); }
private Object convertColValueToDiscType(DiscriminatorType discType, byte[] value) { switch (discType) { case STRING: return new String(value); case CHAR: return ByteBuffer.wrap(value).asCharBuffer().get(); case INTEGER: return IntegerSerializer.get().fromBytes(value); } throw new RuntimeException("must have added a new discriminator type, " + discType + ", because don't know how to convert db value - cannot continue"); }
public String readString() { detectBufferUnderflow(5); boolean asciiCode = BigEndianCodec.getBoolean(buffer, index++); int length = BigEndianCodec.getInt(buffer, index); index += 4; if (asciiCode) { detectBufferUnderflow(length); String s = new String(buffer, index, length); index += length; return s; } length <<= 1; detectBufferUnderflow(length); ByteBuffer byteBuffer = ByteBuffer.wrap(buffer, index, length); index += length; CharBuffer charBuffer = byteBuffer.asCharBuffer(); return charBuffer.toString(); }
.order(ByteOrder.BIG_ENDIAN).put((byte) 0).put((byte) 0).position(1).limit(2)).slice(); final CharBuffer bigEndianReadOnlyDirectCharBuffer = ByteBuffer.allocateDirect(1) .order(ByteOrder.BIG_ENDIAN).asReadOnlyBuffer().asCharBuffer(); final CharBuffer bigEndianReadWriteDirectCharBuffer = ByteBuffer.allocateDirect(1) .order(ByteOrder.BIG_ENDIAN).asCharBuffer(); final DoubleBuffer bigEndianReadOnlyDirectDoubleBuffer = ByteBuffer.allocateDirect(1) .order(ByteOrder.BIG_ENDIAN).asReadOnlyBuffer().asDoubleBuffer(); .order(ByteOrder.LITTLE_ENDIAN).put((byte) 0).put((byte) 0).position(1).limit(2)).slice(); final CharBuffer littleEndianReadOnlyDirectCharBuffer = ByteBuffer.allocateDirect(1) .order(ByteOrder.LITTLE_ENDIAN).asReadOnlyBuffer().asCharBuffer(); final CharBuffer littleEndianReadWriteDirectCharBuffer = ByteBuffer.allocateDirect(1) .order(ByteOrder.LITTLE_ENDIAN).asCharBuffer(); final DoubleBuffer littleEndianReadOnlyDirectDoubleBuffer = ByteBuffer.allocateDirect(1) .order(ByteOrder.LITTLE_ENDIAN).asReadOnlyBuffer().asDoubleBuffer();
public void toByteBuffer(ByteBuffer b) { //symbol CharBuffer cb = b.asCharBuffer(); cb.put(ticker); //we know ticker is a fixed length string b.position(b.position() + cb.position() * 2); //price, volume b.putDouble(price); b.putInt(volume); // current time ms for end to end latency b.putLong(System.currentTimeMillis()); }
/** * {@inheritDoc} */ @Override public final CharBuffer asCharBuffer() { return buf().asCharBuffer(); }
public void getChars(final long offset, final char[] result, int start, int len) { final ByteBuffer buf= ByteBuffer.wrap(this.fBuffer); buf.position(recPtrToIndex(offset)); buf.asCharBuffer().get(result, start, len); }
/** Bulk input of a char array. */ public char[] readChars (int length) throws KryoException { if (capacity - position >= length * 2 && isNativeOrder()) { char[] array = new char[length]; CharBuffer buf = niobuffer.asCharBuffer(); buf.get(array); position += length * 2; niobuffer.position(position); return array; } else return super.readChars(length); }
/** Bulk output of a char array. */ public void writeChars (char[] object) throws KryoException { if (capacity - position >= object.length * 2 && isNativeOrder()) { CharBuffer buf = niobuffer.asCharBuffer(); buf.put(object); position += object.length * 2; } else super.writeChars(object); }