Java Code Examples for java.nio.CharBuffer#slice()

The following examples show how to use java.nio.CharBuffer#slice() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestUtil.java    From RoaringBitmap with Apache License 2.0 6 votes vote down vote up
@Test
public void testCopy() {
  CharBuffer sb = CharBuffer.allocate(64);
  sb.position(32);
  CharBuffer slice = sb.slice();
  CharBuffer dest = CharBuffer.allocate(64);
  for(int k = 0; k < 32; ++k)
    slice.put(k, (char) k);
  BufferUtil.arraycopy(slice, 16, dest, 16, 16);
  for(int k = 16; k < 32; ++k)
    assertEquals((char)k,dest.get(k));
  BufferUtil.arraycopy(slice, 0, dest, 16, 16);
  for(int k = 16; k < 32; ++k)
    assertEquals((char)(k-16),dest.get(k));
  BufferUtil.arraycopy(slice, 16, dest, 0, 16);
  for(int k = 0; k < 16; ++k)
    assertEquals((char)(k+16),dest.get(k));

}
 
Example 2
Source File: Unpooled.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new big-endian buffer whose content is a subregion of
 * the specified {@code string} encoded in the specified {@code charset}.
 * The new buffer's {@code readerIndex} and {@code writerIndex} are
 * {@code 0} and the length of the encoded string respectively.创建一个新的big-endian缓冲区,其内容是指定字符集中编码的指定字符串的子区域。新缓冲区的readerIndex和writerIndex分别为0和编码字符串的长度。
 */
public static ByteBuf copiedBuffer(
        CharSequence string, int offset, int length, Charset charset) {
    if (string == null) {
        throw new NullPointerException("string");
    }
    if (length == 0) {
        return EMPTY_BUFFER;
    }

    if (string instanceof CharBuffer) {
        CharBuffer buf = (CharBuffer) string;
        if (buf.hasArray()) {
            return copiedBuffer(
                    buf.array(),
                    buf.arrayOffset() + buf.position() + offset,
                    length, charset);
        }

        buf = buf.slice();
        buf.limit(length);
        buf.position(offset);
        return copiedBuffer(buf, charset);
    }

    return copiedBuffer(CharBuffer.wrap(string, offset, offset + length), charset);
}
 
Example 3
Source File: Utf8Test.java    From incubator-datasketches-memory with Apache License 2.0 5 votes vote down vote up
private static void assertRoundTrips(String refStr, int refSubCharLen, int offsetBytes,
    int utf8LengthBytes, byte[] refByteArr, Memory refMem, WritableMemory dstMem)
        throws IOException {
  StringBuilder sb = new StringBuilder();

  int charPos = refMem.getCharsFromUtf8(offsetBytes, utf8LengthBytes, sb);
  checkStrings(sb.toString(), new String(refByteArr, offsetBytes, utf8LengthBytes, UTF_8));
  assertEquals(charPos, refSubCharLen);

  CharBuffer cb = CharBuffer.allocate(refByteArr.length + 1);
  cb.position(1);
  // Make CharBuffer 1-based, to check correct offset handling
  cb = cb.slice();
  refMem.getCharsFromUtf8(offsetBytes, utf8LengthBytes, cb);
  cb.flip();
  checkStrings(cb.toString(), new String(refByteArr, offsetBytes, utf8LengthBytes, UTF_8));

  long encodedUtf8Bytes = dstMem.putCharsToUtf8(0, refStr); //encodes entire refStr
  assertEquals(encodedUtf8Bytes, refByteArr.length); //compares bytes length
  //compare the actual bytes encoded
  assertEquals(0, dstMem.compareTo(0, refByteArr.length, refMem, 0, refByteArr.length));

  // Test write overflow
  WritableMemory writeMem2 = WritableMemory.allocate(refByteArr.length - 1);
  try {
    writeMem2.putCharsToUtf8(0, refStr);
    fail();
  } catch (Utf8CodingException e) {
    // Expected.
  }
}
 
Example 4
Source File: Unpooled.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new big-endian buffer whose content is a subregion of
 * the specified {@code string} encoded in the specified {@code charset}.
 * The new buffer's {@code readerIndex} and {@code writerIndex} are
 * {@code 0} and the length of the encoded string respectively.
 */
public static ByteBuf copiedBuffer(
        CharSequence string, int offset, int length, Charset charset) {
    if (string == null) {
        throw new NullPointerException("string");
    }
    if (length == 0) {
        return EMPTY_BUFFER;
    }

    if (string instanceof CharBuffer) {
        CharBuffer buf = (CharBuffer) string;
        if (buf.hasArray()) {
            return copiedBuffer(
                    buf.array(),
                    buf.arrayOffset() + buf.position() + offset,
                    length, charset);
        }

        buf = buf.slice();
        buf.limit(length);
        buf.position(offset);
        return copiedBuffer(buf, charset);
    }

    return copiedBuffer(CharBuffer.wrap(string, offset, offset + length), charset);
}
 
Example 5
Source File: CharsetDecoderTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testBufferWithNonZeroOffset() {
    CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
    CharBuffer cb = CharBuffer.allocate(128);
    cb.position(42);
    CharBuffer out = cb.slice();
    CoderResult cr = decoder.decode(
            ByteBuffer.wrap(new byte[] { 'h', 'e', 'l', 'l', 'o'}), out, false);
    assertTrue(cr.isUnderflow());
    assertEquals(5, out.position());
}
 
Example 6
Source File: ChannelBuffers.java    From simple-netty-source with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new buffer with the specified {@code endianness} whose
 * content is a subregion of the specified {@code string} encoded in the
 * specified {@code charset}.  The new buffer's {@code readerIndex} and
 * {@code writerIndex} are {@code 0} and the length of the encoded string
 * respectively.
 */
public static ChannelBuffer copiedBuffer(
        ByteOrder endianness, CharSequence string, int offset, int length, Charset charset) {
    if (string == null) {
        throw new NullPointerException("string");
    }
    if (length == 0) {
        return EMPTY_BUFFER;
    }

    if (string instanceof CharBuffer) {
        CharBuffer buf = (CharBuffer) string;
        if (buf.hasArray()) {
            return copiedBuffer(
                    endianness,
                    buf.array(),
                    buf.arrayOffset() + buf.position() + offset,
                    length, charset);
        }

        buf = buf.slice();
        buf.limit(length);
        buf.position(offset);
        return copiedBuffer(endianness, buf, charset);
    }

    return copiedBuffer(
            endianness, CharBuffer.wrap(string, offset, offset + length),
            charset);
}
 
Example 7
Source File: ChannelBuffers.java    From android-netty with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new buffer with the specified {@code endianness} whose
 * content is a subregion of the specified {@code string} encoded in the
 * specified {@code charset}.  The new buffer's {@code readerIndex} and
 * {@code writerIndex} are {@code 0} and the length of the encoded string
 * respectively.
 */
public static ChannelBuffer copiedBuffer(
        ByteOrder endianness, CharSequence string, int offset, int length, Charset charset) {
    if (string == null) {
        throw new NullPointerException("string");
    }
    if (length == 0) {
        return EMPTY_BUFFER;
    }

    if (string instanceof CharBuffer) {
        CharBuffer buf = (CharBuffer) string;
        if (buf.hasArray()) {
            return copiedBuffer(
                    endianness,
                    buf.array(),
                    buf.arrayOffset() + buf.position() + offset,
                    length, charset);
        }

        buf = buf.slice();
        buf.limit(length);
        buf.position(offset);
        return copiedBuffer(endianness, buf, charset);
    }

    return copiedBuffer(
            endianness, CharBuffer.wrap(string, offset, offset + length),
            charset);
}
 
Example 8
Source File: Bug6856817.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 * @param args
 */
public static void main(String args[]) {
    CharBuffer charBuffer = CharBuffer.allocate(BUF_SIZE);
    File file = new File("temp.txt");
    file.deleteOnExit();

    charBuffer.put(str);
    charBuffer.flip();
    checkFileContent(charBuffer, file, str);
    charBuffer.position(10);
    checkFileContent(charBuffer, file, str.substring(10));
    charBuffer.position(charBuffer.limit());
    checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));

    char arr[] = new char[BUF_SIZE];
    charBuffer = CharBuffer.wrap(arr);
    charBuffer.put(str);
    charBuffer.flip();
    checkFileContent(charBuffer, file, str);
    charBuffer.position(10);
    checkFileContent(charBuffer, file, str.substring(10));
    charBuffer.position(charBuffer.limit());
    checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));

    char secArr[] = new char[BUF_SIZE];
    charBuffer = CharBuffer.wrap(secArr);
    charBuffer.put(str);
    charBuffer.position(5);
    charBuffer.limit(str.length() - 7);
    charBuffer = charBuffer.slice();
    checkFileContent(charBuffer, file, str.substring(5, (str.length() - 7)));
    charBuffer.position(10);
    checkFileContent(charBuffer, file, str.substring(15, (str.length() - 7)));
    charBuffer.position(charBuffer.limit());
    checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));

    charBuffer = ByteBuffer.allocate(BUF_SIZE).asCharBuffer();
    charBuffer.put(str);
    charBuffer.flip();
    checkFileContent(charBuffer, file, str);
    charBuffer.position(10);
    checkFileContent(charBuffer, file, str.substring(10));
    charBuffer.position(charBuffer.limit());
    checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));

    charBuffer = ByteBuffer.allocateDirect(1024).asCharBuffer();
    charBuffer.put(str);
    charBuffer.flip();
    checkFileContent(charBuffer, file, str);
    charBuffer.position(10);
    checkFileContent(charBuffer, file, str.substring(10));
    charBuffer.position(charBuffer.limit());
    checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));
}
 
Example 9
Source File: CharBufferStream.java    From antsdb with GNU Lesser General Public License v3.0 4 votes vote down vote up
public CharBufferStream(CharBuffer buf) {
    this.buf = buf.slice();
    this.n = this.buf.limit();
}
 
Example 10
Source File: BufferUtils.java    From morfologik-stemming with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static String toString(CharBuffer buffer) {
  buffer = buffer.slice();
  char [] buf = new char [buffer.remaining()];
  buffer.get(buf);
  return new String(buf);
}