Example usage for io.netty.buffer ByteBuf writableBytes

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

Introduction

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

Prototype

public abstract int writableBytes();

Source Link

Document

Returns the number of writable bytes which is equal to (this.capacity - this.writerIndex) .

Usage

From source file:io.grpc.alts.internal.AltsChannelCrypter.java

License:Apache License

@Override
public void encrypt(ByteBuf outBuf, List<ByteBuf> plainBufs) throws GeneralSecurityException {
    checkArgument(outBuf.nioBufferCount() == 1);
    // Copy plaintext buffers into outBuf for in-place encryption on single direct buffer.
    ByteBuf plainBuf = outBuf.slice(outBuf.writerIndex(), outBuf.writableBytes());
    plainBuf.writerIndex(0);/*from   w ww  .java  2  s  . c  o m*/
    for (ByteBuf inBuf : plainBufs) {
        plainBuf.writeBytes(inBuf);
    }

    verify(outBuf.writableBytes() == plainBuf.readableBytes() + TAG_LENGTH);
    ByteBuffer out = outBuf.internalNioBuffer(outBuf.writerIndex(), outBuf.writableBytes());
    ByteBuffer plain = out.duplicate();
    plain.limit(out.limit() - TAG_LENGTH);

    byte[] counter = incrementOutCounter();
    int outPosition = out.position();
    aeadCrypter.encrypt(out, plain, counter);
    int bytesWritten = out.position() - outPosition;
    outBuf.writerIndex(outBuf.writerIndex() + bytesWritten);
    verify(!outBuf.isWritable());
}

From source file:io.grpc.alts.internal.AltsChannelCrypter.java

License:Apache License

@Override
public void decrypt(ByteBuf out, ByteBuf tag, List<ByteBuf> ciphertextBufs) throws GeneralSecurityException {

    ByteBuf cipherTextAndTag = out.slice(out.writerIndex(), out.writableBytes());
    cipherTextAndTag.writerIndex(0);//w  ww . j a v  a2s. c  o m

    for (ByteBuf inBuf : ciphertextBufs) {
        cipherTextAndTag.writeBytes(inBuf);
    }
    cipherTextAndTag.writeBytes(tag);

    decrypt(out, cipherTextAndTag);
}

From source file:io.grpc.alts.internal.AltsChannelCrypter.java

License:Apache License

@Override
public void decrypt(ByteBuf out, ByteBuf ciphertextAndTag) throws GeneralSecurityException {
    int bytesRead = ciphertextAndTag.readableBytes();
    checkArgument(bytesRead == out.writableBytes());

    checkArgument(out.nioBufferCount() == 1);
    ByteBuffer outBuffer = out.internalNioBuffer(out.writerIndex(), out.writableBytes());

    checkArgument(ciphertextAndTag.nioBufferCount() == 1);
    ByteBuffer ciphertextAndTagBuffer = ciphertextAndTag.nioBuffer(ciphertextAndTag.readerIndex(), bytesRead);

    byte[] counter = incrementInCounter();
    int outPosition = outBuffer.position();
    aeadCrypter.decrypt(outBuffer, ciphertextAndTagBuffer, counter);
    int bytesWritten = outBuffer.position() - outPosition;
    out.writerIndex(out.writerIndex() + bytesWritten);
    ciphertextAndTag.readerIndex(out.readerIndex() + bytesRead);
    verify(out.writableBytes() == TAG_LENGTH);
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtector.java

License:Apache License

private static ByteBuf writeSlice(ByteBuf in, int len) {
    checkArgument(len <= in.writableBytes());
    ByteBuf out = in.slice(in.writerIndex(), len);
    in.writerIndex(in.writerIndex() + len);
    return out.writerIndex(0);
}

From source file:io.grpc.alts.internal.ByteBufTestUtils.java

License:Apache License

static ByteBuf writeSlice(ByteBuf in, int len) {
    Preconditions.checkArgument(len <= in.writableBytes());
    ByteBuf out = in.slice(in.writerIndex(), len);
    in.writerIndex(in.writerIndex() + len);
    return out.writerIndex(0);
}

From source file:io.nodyn.buffer.Buffer.java

License:Apache License

public static long asciiWrite(JSObject object, String str, int offset, int len) {
    ByteBuf b = extract(object);
    int origWriter = b.writerIndex();
    byte[] bytes = str.getBytes(ASCII);
    len = Math.min(bytes.length, Math.min(len, b.writableBytes()));
    b.writeBytes(bytes, 0, len);//from   w  w  w.j a va  2  s  . c o m
    b.writerIndex(Math.max(b.writerIndex(), origWriter));
    return len;
}

From source file:io.nodyn.buffer.Buffer.java

License:Apache License

public static long ucs2Write(JSObject object, String str, int offset, int len) {
    ByteBuf b = extract(object);
    int origWriter = b.writerIndex();
    byte[] bytes = str.getBytes(UCS2);
    len = Math.min(bytes.length, Math.min(len, b.writableBytes()));
    b.writeBytes(bytes, 0, len);// w  w w . j a  v  a  2  s.c  o  m
    b.writerIndex(Math.max(b.writerIndex(), origWriter));
    return len;
}

From source file:io.nodyn.buffer.Buffer.java

License:Apache License

public static long hexWrite(JSObject object, String str, int offset, int len) {
    ByteBuf b = extract(object);
    int origWriter = b.writerIndex();
    byte[] bytes = Hex.decode(str);
    b.writerIndex(offset);/*from   w  ww .j av a 2  s .co  m*/
    len = Math.min(bytes.length, Math.min(len, b.writableBytes()));
    b.writeBytes(bytes, 0, len);
    b.writerIndex(Math.max(b.writerIndex(), origWriter));
    return len;
}

From source file:io.nodyn.buffer.Buffer.java

License:Apache License

public static long base64Write(JSObject object, String str, int offset, int len) {
    ByteBuf b = extract(object);
    int origWriter = b.writerIndex();
    byte[] bytes = Base64.decode(str);
    b.writerIndex(offset);//from   w  ww .j  ava 2s.c o m
    len = Math.min(bytes.length, Math.min(len, b.writableBytes()));
    b.writeBytes(bytes, 0, len);
    b.writerIndex(Math.max(b.writerIndex(), origWriter));
    return len;
}

From source file:io.nodyn.buffer.Buffer.java

License:Apache License

public static long binaryWrite(JSObject object, String str, int offset, int len) {
    ByteBuf b = extract(object);
    int origWriter = b.writerIndex();
    byte[] bytes = str.getBytes(BINARY);
    len = Math.min(bytes.length, Math.min(len, b.writableBytes()));
    b.writeBytes(bytes, 0, len);/*from   w ww  .j  a v a2  s  . c om*/
    b.writerIndex(Math.max(b.writerIndex(), origWriter));
    return len;
}