java.nio.ByteBuffer.asCharBuffer()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.1k)|赞(0)|评价(0)|浏览(148)

本文整理了Java中java.nio.ByteBuffer.asCharBuffer()方法的一些代码示例,展示了ByteBuffer.asCharBuffer()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuffer.asCharBuffer()方法的具体详情如下:
包路径:java.nio.ByteBuffer
类名称:ByteBuffer
方法名:asCharBuffer

ByteBuffer.asCharBuffer介绍

[英]Returns a char buffer which is based on the remaining content of this byte buffer.

The new buffer's position is zero, its limit and capacity is the number of remaining bytes divided by two, and its mark is not set. The new buffer's read-only property and byte order are the same as this buffer's. The new buffer is direct if this byte buffer is direct.

The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the other. The two buffer's position, limit and mark are independent.
[中]返回基于此字节缓冲区剩余内容的字符缓冲区。
新缓冲区的位置为零,其限制和容量为剩余字节数除以2,其标记未设置。新缓冲区的只读属性和字节顺序与此缓冲区的相同。如果此字节缓冲区是直接的,则新缓冲区是直接的。
新缓冲区与此缓冲区共享其内容,这意味着任何一个缓冲区的内容更改都将对另一个缓冲区可见。两个缓冲器的位置、极限和标记是独立的。

代码示例

代码示例来源:origin: libgdx/libgdx

public static CharBuffer newCharBuffer (int numChars) {
  ByteBuffer buffer = ByteBuffer.allocateDirect(numChars * 2);
  buffer.order(ByteOrder.nativeOrder());
  return buffer.asCharBuffer();
}

代码示例来源:origin: libgdx/libgdx

public static CharBuffer newCharBuffer (int numChars) {
  ByteBuffer buffer = ByteBuffer.allocateDirect(numChars * 2);
  buffer.order(ByteOrder.nativeOrder());
  return buffer.asCharBuffer();
}

代码示例来源:origin: ehcache/ehcache3

public static void putStringAsCharArray(ByteBuffer byteBuffer, String s) {
 byteBuffer.asCharBuffer().put(s);
 byteBuffer.position(byteBuffer.position() + (s.length() * 2));
}

代码示例来源:origin: bytedeco/javacpp

/** @return {@code asByteBuffer().asCharBuffer()} */
  @Override public final CharBuffer asBuffer() {
    return asByteBuffer().asCharBuffer();
  }
}

代码示例来源:origin: neo4j/neo4j

buffer.asCharBuffer().get( charArray, offset, length );
offset += length;
buffer.clear();

代码示例来源:origin: neo4j/neo4j

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 );
}

代码示例来源:origin: robovm/robovm

@MarshalsArray
public static CharBuffer toCharBuffer(Class<?> cls, long handle, long flags, int d1) {
  return VM.newDirectByteBuffer(handle, d1 << 1).order(ByteOrder.nativeOrder()).asCharBuffer();
}
@MarshalsArray

代码示例来源:origin: robovm/robovm

/**
 * 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();
}

代码示例来源:origin: org.postgresql/postgresql

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();
}

代码示例来源:origin: stackoverflow.com

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." );
  }
}

代码示例来源:origin: stackoverflow.com

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' );
  }

}

代码示例来源:origin: killme2008/xmemcached

/**
 * {@inheritDoc}
 */
@Override
public final CharBuffer asCharBuffer() {
 return buf().asCharBuffer();
}

代码示例来源:origin: hector-client/hector

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");
}

代码示例来源:origin: com.liferay.portal/com.liferay.portal.kernel

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();
}

代码示例来源:origin: i2p/i2p.i2p

.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();

代码示例来源:origin: espertechinc/esper

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());
}

代码示例来源:origin: org.apache.mina/mina-core

/**
 * {@inheritDoc}
 */
@Override
public final CharBuffer asCharBuffer() {
  return buf().asCharBuffer();
}

代码示例来源:origin: org.eclipse.jdt/org.eclipse.jdt.core

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);
}

代码示例来源:origin: com.esotericsoftware/kryo-shaded

/** 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);
}

代码示例来源:origin: com.esotericsoftware/kryo

/** 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);
}

相关文章