Search in sources :

Example 21 with ByteBuffer

use of java.nio.ByteBuffer in project hadoop by apache.

the class ZlibDecompressor method inflateDirect.

int inflateDirect(ByteBuffer src, ByteBuffer dst) throws IOException {
    assert (this instanceof ZlibDirectDecompressor);
    ByteBuffer presliced = dst;
    if (dst.position() > 0) {
        presliced = dst;
        dst = dst.slice();
    }
    Buffer originalCompressed = compressedDirectBuf;
    Buffer originalUncompressed = uncompressedDirectBuf;
    int originalBufferSize = directBufferSize;
    compressedDirectBuf = src;
    compressedDirectBufOff = src.position();
    compressedDirectBufLen = src.remaining();
    uncompressedDirectBuf = dst;
    directBufferSize = dst.remaining();
    int n = 0;
    try {
        n = inflateBytesDirect();
        presliced.position(presliced.position() + n);
        if (compressedDirectBufLen > 0) {
            src.position(compressedDirectBufOff);
        } else {
            src.position(src.limit());
        }
    } finally {
        compressedDirectBuf = originalCompressed;
        uncompressedDirectBuf = originalUncompressed;
        compressedDirectBufOff = 0;
        compressedDirectBufLen = 0;
        directBufferSize = originalBufferSize;
    }
    return n;
}
Also used : Buffer(java.nio.Buffer) ByteBuffer(java.nio.ByteBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 22 with ByteBuffer

use of java.nio.ByteBuffer in project hadoop by apache.

the class Text method set.

/** Set to contain the contents of a string. 
   */
public void set(String string) {
    try {
        ByteBuffer bb = encode(string, true);
        bytes = bb.array();
        length = bb.limit();
    } catch (CharacterCodingException e) {
        throw new RuntimeException("Should not have happened ", e);
    }
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Example 23 with ByteBuffer

use of java.nio.ByteBuffer in project hadoop by apache.

the class Text method encode.

/**
   * Converts the provided String to bytes using the
   * UTF-8 encoding. If <code>replace</code> is true, then
   * malformed input is replaced with the
   * substitution character, which is U+FFFD. Otherwise the
   * method throws a MalformedInputException.
   * @return ByteBuffer: bytes stores at ByteBuffer.array() 
   *                     and length is ByteBuffer.limit()
   */
public static ByteBuffer encode(String string, boolean replace) throws CharacterCodingException {
    CharsetEncoder encoder = ENCODER_FACTORY.get();
    if (replace) {
        encoder.onMalformedInput(CodingErrorAction.REPLACE);
        encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    }
    ByteBuffer bytes = encoder.encode(CharBuffer.wrap(string.toCharArray()));
    if (replace) {
        encoder.onMalformedInput(CodingErrorAction.REPORT);
        encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    }
    return bytes;
}
Also used : CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Example 24 with ByteBuffer

use of java.nio.ByteBuffer in project hadoop by apache.

the class Text method writeString.

/** Write a UTF8 encoded string to out
   */
public static int writeString(DataOutput out, String s) throws IOException {
    ByteBuffer bytes = encode(s);
    int length = bytes.limit();
    WritableUtils.writeVInt(out, length);
    out.write(bytes.array(), 0, length);
    return length;
}
Also used : ByteBuffer(java.nio.ByteBuffer)

Example 25 with ByteBuffer

use of java.nio.ByteBuffer in project hadoop by apache.

the class Text method writeString.

/** Write a UTF8 encoded string with a maximum size to out
   */
public static int writeString(DataOutput out, String s, int maxLength) throws IOException {
    ByteBuffer bytes = encode(s);
    int length = bytes.limit();
    if (length > maxLength) {
        throw new IOException("string was too long to write!  Expected " + "less than or equal to " + maxLength + " bytes, but got " + length + " bytes.");
    }
    WritableUtils.writeVInt(out, length);
    out.write(bytes.array(), 0, length);
    return length;
}
Also used : IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBuffer (java.nio.ByteBuffer)8537 Test (org.junit.Test)1905 IOException (java.io.IOException)972 ArrayList (java.util.ArrayList)430 File (java.io.File)216 FileChannel (java.nio.channels.FileChannel)204 MappedByteBuffer (java.nio.MappedByteBuffer)190 HashMap (java.util.HashMap)172 CharBuffer (java.nio.CharBuffer)144 ByteArrayOutputStream (java.io.ByteArrayOutputStream)138 InetSocketAddress (java.net.InetSocketAddress)137 Random (java.util.Random)119 InputStream (java.io.InputStream)115 FileInputStream (java.io.FileInputStream)114 Map (java.util.Map)113 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)99 Test (org.testng.annotations.Test)96 IntBuffer (java.nio.IntBuffer)93 SocketChannel (java.nio.channels.SocketChannel)92 FileOutputStream (java.io.FileOutputStream)87