Example usage for io.netty.buffer ByteBuf setBytes

List of usage examples for io.netty.buffer ByteBuf setBytes

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf setBytes.

Prototype

public abstract int setBytes(int index, FileChannel in, long position, int length) throws IOException;

Source Link

Document

Transfers the content of the specified source channel starting at the given file position to this buffer starting at the specified absolute index .

Usage

From source file:com.talent.mysql.packet.request.AuthPacket.java

License:Open Source License

public void decodeBody(ByteBuf _byteBuf) {
    byte[] bs = new byte[] { -117, -93, 2, 0, 0, 0, 0, 64, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 114, 111, 111, 116, 0, 20, -19, -111, -3, 39, -46, -116, -128, -44, -112, -26,
            -48, 42, 70, -85, 8, 83, 83, 100, 103, 68, 116, 97, 108, 101, 110, 116, 95, 98, 97, 115, 101, 119,
            101, 98, 50, 48, 49, 0 };// w w  w. j a  v  a  2s. c  om
    ByteBuf byteBuf = Unpooled.buffer(bs.length);
    byteBuf = byteBuf.order(ByteOrder.LITTLE_ENDIAN);
    byteBuf.setBytes(0, bs, 0, bs.length);

    int _index = byteBuf.readerIndex();
    int index = _index;

    clientFlags = byteBuf.getInt(index); //172939
    index += 4;

    maxPacketSize = byteBuf.getInt(index); //1073741824
    index += 4;

    charsetIndex = byteBuf.getByte(index); //33
    index += 1;

    index += extra.length;

    int len = 0;
    while (byteBuf.getByte(index + len) != 0) {
        len++;
    }
    user = new byte[len];
    byteBuf.getBytes(index, user, 0, len);
    index += len;
    index++;

    passwordLen = byteBuf.getByte(index);
    index += 1;

    password = new byte[passwordLen];
    byteBuf.getBytes(index, password, 0, passwordLen);

    len = 0;
    while (byteBuf.getByte(index + len) != 0) {
        len++;
    }
    database = new byte[len];
    byteBuf.getBytes(index, database, 0, len);
    index += len;
    index++;

}

From source file:io.nodyn.fs.Fs.java

License:Apache License

public static int read(POSIX posix, int fd, ByteBuf buf, int offset, int length) {
    byte[] input = new byte[length];
    int read = posix.read(fd, input, length);
    if (read != -1) {
        buf.setBytes(offset, input, 0, read);
        buf.writerIndex(Math.max(buf.writerIndex(), offset + read));
    }/*from  w  w  w  . j a v  a  2  s  .  c om*/
    return read;
}

From source file:io.nodyn.fs.Fs.java

License:Apache License

public static int pread(POSIX posix, int fd, ByteBuf buf, int offset, int length, int position) {
    byte[] input = new byte[length];
    int read = posix.pread(fd, input, length, position);
    if (read != -1) {
        buf.setBytes(offset, input, 0, read);
        buf.writerIndex(Math.max(buf.writerIndex(), offset + read));
    }//from w w  w  .j a  v a  2  s .  c o m
    return read;
}

From source file:io.nodyn.zlib.NodeZlib.java

License:Apache License

private static void gunzip(NodeZlib ctx, int flush, byte[] chunk, int inOffset, int inLen, ByteBuf buffer,
        int outOffset, int outLen) throws IOException {
    GZIPInputStream inputStream = new GZIPInputStream(new ByteArrayInputStream(chunk));
    byte[] result = new byte[outLen];
    int bytesRead = inputStream.read(result, inOffset, Math.min(inLen, result.length));
    inputStream.close();/*w w w  .  j  av  a2s  .c om*/
    buffer.setBytes(outOffset, result, 0, bytesRead);
    after(ctx, result, 0, outLen - bytesRead);
}

From source file:io.nodyn.zlib.NodeZlib.java

License:Apache License

private static void gzip(final NodeZlib ctx, int flush, byte[] chunk, int inOffset, int inLen, ByteBuf buffer,
        int outOffset, int outLen) throws IOException {
    final ByteArrayOutputStream output = new ByteArrayOutputStream(outLen);
    GZIPOutputStream outputStream = new GZIPOutputStream(output) {
        {/*from www  .j  a v a  2  s .  c om*/
            this.def.setLevel(Level.mapDeflaterLevel(ctx.level));
            this.def.setStrategy(ctx.strategy);
        }
    };
    outputStream.write(chunk, inOffset, inLen);
    outputStream.finish();
    final byte[] bytes = output.toByteArray();
    buffer.setBytes(outOffset, bytes, 0, Math.min(bytes.length, outLen));
    after(ctx, bytes, 0, outLen - bytes.length);
}

From source file:io.nodyn.zlib.NodeZlib.java

License:Apache License

private static void inflate(NodeZlib ctx, int flush, byte[] chunk, int inOffset, int inLen, ByteBuf buffer,
        int outOffset, int outLen, boolean raw) throws DataFormatException {
    Inflater inflater = new Inflater(raw);
    inflater.setInput(chunk, inOffset, inLen);
    byte[] output = new byte[chunk.length * 2];

    int inflatedLen = inflater.inflate(output);
    if (inflater.needsDictionary()) {
        if (ctx.dictionary == null) {
            ctx.emit("error", CallbackResult.createError(new RuntimeException("Missing dictionary")));
            return;
        } else {/* ww  w  . ja va2s . co m*/
            try {
                inflater.setDictionary(ctx.dictionary);
                inflatedLen = inflater.inflate(output);
            } catch (Throwable t) {
                ctx.emit("error", CallbackResult.createError(new RuntimeException("Bad dictionary")));
            }
        }
    }
    inflater.end();
    buffer.setBytes(outOffset, output, 0, inflatedLen);
    after(ctx, Arrays.copyOf(output, inflatedLen), 0, outLen - inflatedLen);
}

From source file:io.nodyn.zlib.NodeZlib.java

License:Apache License

private static void deflate(NodeZlib ctx, int flush, byte[] chunk, int inOffset, int inLen, ByteBuf buffer,
        int outOffset, int outLen, boolean raw) {
    Deflater deflater = new Deflater(Level.mapDeflaterLevel(ctx.level), raw);
    deflater.setStrategy(ctx.strategy);/*from   w  w w.j  a  v a 2  s  . c o  m*/
    deflater.setLevel(Level.mapDeflaterLevel(ctx.level));
    if (ctx.dictionary != null)
        deflater.setDictionary(ctx.dictionary);
    deflater.setInput(chunk, inOffset, inLen);
    deflater.finish();
    byte[] output = new byte[chunk.length];
    int compressedLength = deflater.deflate(output, 0, output.length, Flush.mapFlush(flush));
    deflater.end();
    buffer.setBytes(outOffset, output, 0, compressedLength);
    after(ctx, Arrays.copyOf(output, compressedLength), 0, outLen - compressedLength);
}

From source file:net.tomp2p.storage.AlternativeCompositeByteBuf.java

License:Apache License

@Override
public AlternativeCompositeByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
    checkSrcIndex(index, length, srcIndex, src.capacity());
    if (length == 0) {
        return this;
    }/*from   w  w  w. ja v  a2  s .  co  m*/

    int i = findIndex(index);
    while (length > 0) {
        Component c = components.get(i);
        ByteBuf s = c.buf;
        int adjustment = c.offset;
        int localLength = Math.min(length, s.writableBytes());
        s.setBytes(index - adjustment, src, srcIndex, localLength);
        index += localLength;
        srcIndex += localLength;
        length -= localLength;
        i++;
    }
    return this;
}

From source file:net.tomp2p.storage.AlternativeCompositeByteBuf.java

License:Apache License

@Override
public AlternativeCompositeByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
    checkSrcIndex(index, length, srcIndex, src.length);
    if (length == 0) {
        return this;
    }//from   w w w . ja  va2  s .  com

    int i = findIndex(index);
    while (length > 0) {
        Component c = components.get(i);
        ByteBuf s = c.buf;
        int adjustment = c.offset;
        int localLength = Math.min(length, s.writableBytes());
        s.setBytes(index - adjustment, src, srcIndex, localLength);
        index += localLength;
        srcIndex += localLength;
        length -= localLength;
        i++;
    }
    return this;
}