Example usage for io.netty.buffer ByteBuf array

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

Introduction

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

Prototype

public abstract byte[] array();

Source Link

Document

Returns the backing byte array of this buffer.

Usage

From source file:org.starnub.starbounddata.ByteBufferUtilities.java

License:Open Source License

public static void print(ByteBuf in, boolean stringDump) {
    byte[] bytes = in.array();
    System.err.println(Arrays.toString(bytes));
    if (stringDump) {
        System.err.println(new String(bytes, Charset.forName("UTF-8")));
    }/*from w  w w  .  j  a  v a2s  .  c  o  m*/
}

From source file:org.starnub.starbounddata.ByteBufferUtilities.java

License:Open Source License

public static void search(ByteBuf in, byte[] byteArray) {
    byte[] searchable = in.array();
    int searchableBytesSize = searchable.length;
    int searchBytesSize = byteArray.length;
    byte[] vlqByteArray = VLQ.writeUnsignedVLQNoObject(searchBytesSize);
    int vlqLength = vlqByteArray.length;
    System.err.println(/*from  w  w w.  j  a  v a  2s .c om*/
            "Searchable Bytes Size: " + searchableBytesSize + ". Search Bytes Size: " + searchBytesSize);
    System.err.println("VLQ Byte Pattern: " + Arrays.toString(vlqByteArray) + ". VLQ Length: " + vlqLength
            + ". VLQ Value: " + searchBytesSize);
    System.err.println("Search Bytes: " + Arrays.toString(byteArray));
    System.err.println("Searchable Byte Array: " + Arrays.toString(searchable));
    int searchBytesStartIndex = 0;
    int searchBytesEndIndex = 0;
    int matched = 0;
    int tempIndex = 0;
    for (byte byteSearched : searchable) {
        byte searchByte = byteArray[matched];
        if (byteSearched == searchByte) {
            matched++;
            if (matched == searchBytesSize) {
                searchBytesEndIndex = tempIndex;
                searchBytesStartIndex = searchBytesEndIndex - searchBytesSize + 1;
                break;
            }
        } else {
            matched = 0;
        }
        tempIndex++;
    }
    int indexVLQEndIndex = searchBytesStartIndex - 1;
    int indexVLQStartIndex = (indexVLQEndIndex - vlqLength) + 1;
    System.err.println("VLQ Start: " + indexVLQStartIndex);
    System.err.println("VLQ End: " + indexVLQEndIndex);
    System.err.println("Byte Array Match Start: " + searchBytesStartIndex);
    System.err.println("Byte Array Match End: " + searchBytesEndIndex);
}

From source file:org.waarp.common.digest.FilesystemBasedDigest.java

License:Open Source License

/**
 * Update the digest with new buffer/*from   ww  w  .  ja v  a2  s. co  m*/
 */
public void Update(ByteBuf buffer) {
    byte[] bytes = null;
    int start = 0;
    int length = buffer.readableBytes();
    if (buffer.hasArray()) {
        start = buffer.arrayOffset();
        bytes = buffer.array();
    } else {
        if (reusableBytes == null || reusableBytes.length != length) {
            reusableBytes = new byte[length];
        }
        bytes = reusableBytes;
        buffer.getBytes(buffer.readerIndex(), bytes);
    }
    Update(bytes, start, length);
}

From source file:org.waarp.common.digest.FilesystemBasedDigest.java

License:Open Source License

/**
 * Get hash with given {@link ByteBuf} (from Netty)
 * //from w w  w .ja  v  a  2  s. c o m
 * @param buffer
 *            this buffer will not be changed
 * @param algo
 * @return the hash
 * @throws IOException
 */
public static byte[] getHash(ByteBuf buffer, DigestAlgo algo) throws IOException {
    Checksum checksum = null;
    byte[] bytes = null;
    int start = 0;
    int length = buffer.readableBytes();
    if (buffer.hasArray()) {
        start = buffer.arrayOffset();
        bytes = buffer.array();
    } else {
        bytes = new byte[length];
        buffer.getBytes(buffer.readerIndex(), bytes);
    }
    switch (algo) {
    case ADLER32:
        checksum = new Adler32();
    case CRC32:
        if (checksum == null) { // not ADLER32
            checksum = new CRC32();
        }
        checksum.update(bytes, start, length);
        bytes = null;
        bytes = Long.toOctalString(checksum.getValue()).getBytes(UTF8);
        checksum = null;
        return bytes;
    case MD5:
        if (useFastMd5) {
            MD5 md5 = new MD5();
            md5.Update(bytes, start, length);
            bytes = md5.Final();
            md5 = null;
            return bytes;
        }
    case MD2:
    case SHA1:
    case SHA256:
    case SHA384:
    case SHA512:
        String algoname = algo.name;
        MessageDigest digest = null;
        try {
            digest = MessageDigest.getInstance(algoname);
        } catch (NoSuchAlgorithmException e) {
            throw new IOException(algoname + " Algorithm not supported by this JVM", e);
        }
        digest.update(bytes, start, length);
        bytes = digest.digest();
        digest = null;
        return bytes;
    default:
        throw new IOException(algo.name + " Algorithm not supported by this JVM");
    }
}

From source file:org.waarp.common.digest.MD5.java

License:Open Source License

/**
 * Updates hash with given {@link ByteBuf} (from Netty)
 * /* w  w w .  j a v  a 2  s.c  o m*/
 * @param buffer
 *            ByteBuf to use for updating the hash and this buffer will not be changed
 */
public void Update(ByteBuf buffer) {
    byte[] bytes;
    int start = 0;
    int len = buffer.readableBytes();
    if (buffer.hasArray()) {
        start = buffer.arrayOffset();
        bytes = buffer.array();
    } else {
        if (reusableBytes == null || reusableBytes.length != len) {
            reusableBytes = new byte[len];
        }
        bytes = reusableBytes;
        buffer.getBytes(start, bytes);
    }
    Update(state, bytes, start, len);
}

From source file:org.waarp.common.file.filesystembased.FilesystemBasedFileImpl.java

License:Open Source License

/**
 * Write the current FileInterface with the given ByteBuf. The file is not limited to 2^32
 * bytes since this write operation is in add mode.
 * /*from w w  w.j a v  a  2s .c  o m*/
 * In case of error, the current already written blocks are maintained and the position is not
 * changed.
 * 
 * @param buffer
 *            added to the file
 * @throws FileTransferException
 */
private void writeBlock(ByteBuf buffer) throws FileTransferException {
    if (!isReady) {
        throw new FileTransferException("No file is ready");
    }
    // An empty buffer is allowed
    if (buffer == null) {
        return;// could do FileEndOfTransfer ?
    }
    if (fileOutputStream == null) {
        // rafOut = getRandomFile();
        fileOutputStream = getFileOutputStream(position > 0);
    }
    if (fileOutputStream == null) {
        throw new FileTransferException("Internal error, file is not ready");
    }
    int bufferSize = buffer.readableBytes();
    int start = 0;
    byte[] newbuf;
    if (buffer.hasArray()) {
        start = buffer.arrayOffset();
        newbuf = buffer.array();
        buffer.readerIndex(buffer.readerIndex() + bufferSize);
    } else {
        if (reusableBytes == null || reusableBytes.length != bufferSize) {
            reusableBytes = new byte[bufferSize];
        }
        newbuf = reusableBytes;
        buffer.readBytes(newbuf);
    }
    try {
        fileOutputStream.write(newbuf, start, bufferSize);
    } catch (IOException e2) {
        logger.error("Error during write:", e2);
        try {
            closeFile();
        } catch (CommandAbstractException e1) {
        }
        // NO this.realFile.delete(); NO DELETE SINCE BY BLOCK IT CAN BE
        // REDO
        throw new FileTransferException("Internal error, file is not ready");
    }
    position += bufferSize;
}

From source file:org.waarp.ftp.core.data.handler.FtpSeekAheadData.java

License:Open Source License

/**
 * @param buffer/*from w ww . j  av a 2  s.c  om*/
 */
FtpSeekAheadData(ByteBuf buffer) throws SeekAheadNoBackArrayException {
    if (!buffer.hasArray()) {
        throw new SeekAheadNoBackArrayException();
    }
    this.buffer = buffer;
    this.bytes = buffer.array();
    this.pos = this.readerIndex = buffer.arrayOffset() + buffer.readerIndex();
    this.limit = buffer.arrayOffset() + buffer.writerIndex();
}

From source file:org.wso2.carbon.mss.internal.router.TestHandler.java

License:Open Source License

@Path("/stream/upload/fail")
@PUT/*from  ww w .java2 s .c  o m*/
public HttpStreamHandler streamUploadFailure() {
    final int fileSize = 30 * 1024 * 1024;

    return new HttpStreamHandler() {
        int count = 0;
        ByteBuffer offHeapBuffer = ByteBuffer.allocateDirect(fileSize);

        @Override
        public void chunk(ByteBuf request, HttpResponder responder) {
            Preconditions.checkState(count == 1, "chunk error");
            offHeapBuffer.put(request.array());
        }

        @Override
        public void finished(ByteBuf request, HttpResponder responder) {
            int bytesUploaded = offHeapBuffer.position();
            responder.sendString(HttpResponseStatus.OK, "Uploaded:" + bytesUploaded);
        }

        @Override
        public void error(Throwable cause) {
            offHeapBuffer = null;
        }
    };
}

From source file:ratpack.session.clientside.internal.DefaultCrypto.java

License:Apache License

@Override
public byte[] encrypt(ByteBuf message) {
    return Exceptions.uncheck(() -> {
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        ByteBuf messageBuf = Unpooled.wrappedBuffer(new byte[cipher.getOutputSize(message.readableBytes())]);
        cipher.update(message.nioBuffer(), messageBuf.nioBuffer());

        byte[] payload = cipher.doFinal();
        if (isInitializationVectorRequired) {
            byte[] ivBytes = cipher.getIV();
            messageBuf.release();/*  w w  w.j av  a 2  s.  co m*/

            int outputLength = 1 + ivBytes.length + payload.length;
            ByteBuf output = Unpooled.wrappedBuffer(new byte[outputLength]).resetWriterIndex()
                    .writeByte(ivBytes.length).writeBytes(ivBytes).writeBytes(payload);

            payload = output.array();

            output.release();
        }

        return payload;
    });
}

From source file:ratpack.session.clientside.internal.DefaultCrypto.java

License:Apache License

@Override
public byte[] decrypt(ByteBuf message) {
    return Exceptions.uncheck(() -> {
        Cipher cipher = Cipher.getInstance(algorithm);

        if (isInitializationVectorRequired) {
            int ivByteLength = message.readByte();
            ByteBuf ivBytes = message.readBytes(ivByteLength);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes.array());
            ivBytes.release();/*from w ww .j a v a  2s.co m*/

            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
        } else {
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        }

        int messageLength = message.readableBytes();
        ByteBuf output = Unpooled.wrappedBuffer(new byte[cipher.getOutputSize(messageLength)]);
        cipher.update(message.readBytes(messageLength).nioBuffer(), output.nioBuffer());

        byte[] decrypted = cipher.doFinal();
        output.release();

        return decrypted;
    });
}