Example usage for io.netty.buffer ByteBuf release

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

Introduction

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

Prototype

boolean release();

Source Link

Document

Decreases the reference count by 1 and deallocates this object if the reference count reaches at 0 .

Usage

From source file:divconq.ctp.stream.UntarStream.java

License:Open Source License

@Override
public ReturnOption handle(FileDescriptor file, ByteBuf data) {
    if (file == FileDescriptor.FINAL)
        return this.downstream.handle(file, data);

    ByteBuf in = data;

    if (in != null) {
        while (in.isReadable()) {
            switch (this.tstate) {
            case RECORD:
                // starting a new record
                if (in.readableBytes() < TarConstants.DEFAULT_RCDSIZE - this.partialLength) {
                    int offset = this.partialLength;

                    this.partialLength += in.readableBytes();

                    in.readBytes(this.header_buffer, offset, in.readableBytes());

                    continue;
                }//from   ww w .j a va 2 s  .c o  m

                in.readBytes(this.header_buffer, this.partialLength,
                        TarConstants.DEFAULT_RCDSIZE - this.partialLength);

                this.partialLength = 0;

                //in.readBytes(this.header_buffer, 0, this.header_buffer.length);

                boolean hasHitEOF = this.isEOFRecord(this.header_buffer);

                // if we hit this twice in a row we are at the end - however, source will send FINAL anyway so we don't really care
                if (hasHitEOF) {
                    this.currEntry = null;
                    continue;
                }

                try {
                    this.currEntry = new TarArchiveEntry(this.header_buffer, this.encoding);
                } catch (Exception x) {
                    OperationContext.get().getTaskRun().kill("Error detected parsing the header: " + x);
                    in.release();
                    return ReturnOption.DONE;
                }

                this.tstate = UntarState.XTRAS;
            case XTRAS:
                if (!in.isReadable())
                    continue;

                // TODO support long names and such - see org.apache.commons.compress.archivers.tar.TarArchiveInputStream
                if (this.currEntry.isGNULongLinkEntry()) {
                    /* 
                      byte[] longLinkData = getLongNameData();
                      if (longLinkData == null) {
                          // Bugzilla: 40334
                          // Malformed tar file - long link entry name not followed by
                          // entry
                          return null;
                      }
                      currEntry.setLinkName(encoding.decode(longLinkData));
                      */

                    OperationContext.get().getTaskRun().kill("long link currently not supported");
                    in.release();
                    return ReturnOption.DONE;
                }

                if (this.currEntry.isGNULongNameEntry()) {
                    /* 
                      byte[] longNameData = getLongNameData();
                      if (longNameData == null) {
                          // Bugzilla: 40334
                          // Malformed tar file - long entry name not followed by
                          // entry
                          return null;
                      }
                      currEntry.setName(encoding.decode(longNameData));
                      */

                    OperationContext.get().getTaskRun().kill("long name currently not supported");
                    in.release();
                    return ReturnOption.DONE;
                }

                if (this.currEntry.isPaxHeader()) {
                    // Process Pax headers
                    /* 
                      paxHeaders();
                      */

                    OperationContext.get().getTaskRun().kill("pax currently not supported");
                    in.release();
                    return ReturnOption.DONE;
                }

                if (this.currEntry.isGNUSparse()) {
                    // Process sparse files
                    /* 
                      readGNUSparse();
                      */

                    OperationContext.get().getTaskRun().kill("sparse currently not supported");
                    in.release();
                    return ReturnOption.DONE;
                }

                this.tstate = UntarState.PREP;
            case PREP:
                if (!in.isReadable())
                    continue;

                // TODO remove
                System.out.println("name: " + this.currEntry.getName());
                System.out.println("size: " + this.currEntry.getSize());
                System.out.println("modified: " + this.currEntry.getModTime());

                // If the size of the next element in the archive has changed
                // due to a new size being reported in the posix header
                // information, we update entrySize here so that it contains
                // the correct value.
                long entrySize = this.currEntry.getSize();
                this.remainContent = entrySize;

                long numRecords = (entrySize / this.header_buffer.length) + 1;
                this.remainSkip = (numRecords * this.header_buffer.length) - entrySize;

                // grab as much as we can from the current buffer
                int readSize = (int) Math.min(this.remainContent, in.readableBytes());
                this.remainContent -= readSize;

                // handle empty files too
                if ((readSize > 0) || (this.remainContent == 0)) {
                    System.out.println("reading content: " + readSize);

                    ByteBuf out = in.copy(in.readerIndex(), readSize);

                    int skipSize = (int) Math.min(this.remainSkip, in.readableBytes() - readSize);
                    this.remainSkip -= skipSize;

                    in.skipBytes(readSize + skipSize);

                    this.nextMessage(out);
                }

                this.tstate = UntarState.CONTENT;
            case CONTENT:
                if (!in.isReadable())
                    continue;

                // check if there is still content left in the entry we were last reading from
                if (this.remainContent > 0) {
                    readSize = (int) Math.min(this.remainContent, in.readableBytes());
                    this.remainContent -= readSize;

                    //System.out.println("reading content: " + readSize);

                    //ByteBuf out = Hub.instance.getBufferAllocator().heapBuffer((int) readSize);

                    ByteBuf out = in.copy(in.readerIndex(), readSize);

                    int skipSize = (int) Math.min(this.remainSkip, in.readableBytes() - readSize);
                    this.remainSkip -= skipSize;

                    //System.out.println("skipping content: " + skipSize);

                    in.skipBytes(readSize + skipSize);

                    this.nextMessage(out);
                }

                if (this.remainContent > 0)
                    continue;

                this.currEntry = null;

                this.tstate = UntarState.SKIP;
            case SKIP:
                if (!in.isReadable())
                    continue;

                // check if there is still padding left in the entry we were last reading from
                if (this.remainSkip > 0) {
                    int skipSize = (int) Math.min(this.remainSkip, in.readableBytes());
                    this.remainSkip -= skipSize;

                    //System.out.println("skipping content: " + skipSize);

                    in.skipBytes((int) skipSize);
                }

                if (this.remainSkip > 0)
                    continue;

                this.tstate = UntarState.RECORD;
            }
        }

        in.release();
    }

    // write all messages in the queue
    while (this.outlist.size() > 0) {
        ReturnOption ret = this.downstream.handle(this.outlist.remove(0), this.outbuf.remove(0));

        if (ret != ReturnOption.CONTINUE)
            return ret;
    }

    return ReturnOption.CONTINUE;
}

From source file:divconq.http.multipart.AbstractDiskHttpData.java

License:Apache License

@Override
public void setContent(ByteBuf buffer) throws IOException {
    if (buffer == null) {
        throw new NullPointerException("buffer");
    }//from   www  .  j a v  a2s  . c  o  m
    try {
        size = buffer.readableBytes();
        checkSize(size);
        if (definedSize > 0 && definedSize < size) {
            throw new IOException("Out of size: " + size + " > " + definedSize);
        }
        if (file == null) {
            file = tempFile();
        }
        if (buffer.readableBytes() == 0) {
            // empty file
            if (!file.createNewFile()) {
                throw new IOException("file exists already: " + file);
            }
            return;
        }
        FileOutputStream outputStream = new FileOutputStream(file);
        FileChannel localfileChannel = outputStream.getChannel();
        ByteBuffer byteBuffer = buffer.nioBuffer();
        int written = 0;
        while (written < size) {
            written += localfileChannel.write(byteBuffer);
        }
        buffer.readerIndex(buffer.readerIndex() + written);
        localfileChannel.force(false);
        localfileChannel.close();
        outputStream.close();
        setCompleted();
    } finally {
        // Release the buffer as it was retained before and we not need a reference to it at all
        // See https://github.com/netty/netty/issues/1516
        buffer.release();
    }
}

From source file:divconq.http.multipart.AbstractDiskHttpData.java

License:Apache License

@Override
public void addContent(ByteBuf buffer, boolean last) throws IOException {
    if (buffer != null) {
        try {// ww  w. j  a  va 2  s .  c  o  m
            int localsize = buffer.readableBytes();
            checkSize(size + localsize);
            if (definedSize > 0 && definedSize < size + localsize) {
                throw new IOException("Out of size: " + (size + localsize) + " > " + definedSize);
            }
            ByteBuffer byteBuffer = buffer.nioBufferCount() == 1 ? buffer.nioBuffer()
                    : buffer.copy().nioBuffer();
            int written = 0;
            if (file == null) {
                file = tempFile();
            }
            if (fileChannel == null) {
                FileOutputStream outputStream = new FileOutputStream(file);
                fileChannel = outputStream.getChannel();
            }
            while (written < localsize) {
                written += fileChannel.write(byteBuffer);
            }
            size += localsize;
            buffer.readerIndex(buffer.readerIndex() + written);
        } finally {
            // Release the buffer as it was retained before and we not need a reference to it at all
            // See https://github.com/netty/netty/issues/1516
            buffer.release();
        }
    }
    if (last) {
        if (file == null) {
            file = tempFile();
        }
        if (fileChannel == null) {
            FileOutputStream outputStream = new FileOutputStream(file);
            fileChannel = outputStream.getChannel();
        }
        fileChannel.force(false);
        fileChannel.close();
        fileChannel = null;
        setCompleted();
    } else {
        if (buffer == null) {
            throw new NullPointerException("buffer");
        }
    }
}

From source file:divconq.http.multipart.HttpPostRequestEncoder.java

License:Apache License

/**
 * Finalize the request by preparing the Header in the request and returns the request ready to be sent.<br>
 * Once finalized, no data must be added.<br>
 * If the request does not need chunk (isChunked() == false), this request is the only object to send to the remote
 * server.//  w ww .  j a v  a 2s . c o  m
 *
 * @return the request object (chunked or not according to size of body)
 * @throws ErrorDataEncoderException
 *             if the encoding is in error or if the finalize were already done
 */
public HttpRequest finalizeRequest() throws ErrorDataEncoderException {
    // Finalize the multipartHttpDatas
    if (!headerFinalized) {
        if (isMultipart) {
            InternalAttribute internal = new InternalAttribute(charset);
            if (duringMixedMode) {
                internal.addValue("\r\n--" + multipartMixedBoundary + "--");
            }
            internal.addValue("\r\n--" + multipartDataBoundary + "--\r\n");
            multipartHttpDatas.add(internal);
            multipartMixedBoundary = null;
            currentFileUpload = null;
            duringMixedMode = false;
            globalBodySize += internal.size();
        }
        headerFinalized = true;
    } else {
        throw new ErrorDataEncoderException("Header already encoded");
    }

    HttpHeaders headers = request.headers();
    List<String> contentTypes = headers.getAll(HttpHeaders.Names.CONTENT_TYPE);
    List<String> transferEncoding = headers.getAll(HttpHeaders.Names.TRANSFER_ENCODING);
    if (contentTypes != null) {
        headers.remove(HttpHeaders.Names.CONTENT_TYPE);
        for (String contentType : contentTypes) {
            // "multipart/form-data; boundary=--89421926422648"
            if (contentType.toLowerCase().startsWith(HttpHeaders.Values.MULTIPART_FORM_DATA.toString())) {
                // ignore
            } else if (contentType.toLowerCase()
                    .startsWith(HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED.toString())) {
                // ignore
            } else {
                headers.add(HttpHeaders.Names.CONTENT_TYPE, contentType);
            }
        }
    }
    if (isMultipart) {
        String value = HttpHeaders.Values.MULTIPART_FORM_DATA + "; " + HttpHeaders.Values.BOUNDARY + '='
                + multipartDataBoundary;
        headers.add(HttpHeaders.Names.CONTENT_TYPE, value);
    } else {
        // Not multipart
        headers.add(HttpHeaders.Names.CONTENT_TYPE, HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED);
    }
    // Now consider size for chunk or not
    long realSize = globalBodySize;
    if (isMultipart) {
        iterator = multipartHttpDatas.listIterator();
    } else {
        realSize -= 1; // last '&' removed
        iterator = multipartHttpDatas.listIterator();
    }
    headers.set(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(realSize));
    if (realSize > HttpPostBodyUtil.chunkSize || isMultipart) {
        isChunked = true;
        if (transferEncoding != null) {
            headers.remove(HttpHeaders.Names.TRANSFER_ENCODING);
            for (String v : transferEncoding) {
                if (HttpHeaders.equalsIgnoreCase(v, HttpHeaders.Values.CHUNKED)) {
                    // ignore
                } else {
                    headers.add(HttpHeaders.Names.TRANSFER_ENCODING, v);
                }
            }
        }
        HttpHeaders.setTransferEncodingChunked(request);

        // wrap to hide the possible content
        return new WrappedHttpRequest(request);
    } else {
        // get the only one body and set it to the request
        HttpContent chunk = nextChunk();
        if (request instanceof FullHttpRequest) {
            FullHttpRequest fullRequest = (FullHttpRequest) request;
            ByteBuf chunkContent = chunk.content();
            if (fullRequest.content() != chunkContent) {
                fullRequest.content().clear().writeBytes(chunkContent);
                chunkContent.release();
            }
            return fullRequest;
        } else {
            return new WrappedFullHttpRequest(request, chunk);
        }
    }
}

From source file:divconq.net.ByteToMessageDecoder.java

License:Apache License

@Override
public final void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
    ByteBuf buf = internalBuffer();
    int readable = buf.readableBytes();
    if (buf.isReadable()) {
        ByteBuf bytes = buf.readBytes(readable);
        buf.release();
        ctx.fireChannelRead(bytes);/*from   w w  w. j a va 2 s. c  om*/
    } else {
        buf.release();
    }
    this.cumulation = null;
    ctx.fireChannelReadComplete();
    this.handlerRemoved0(ctx);
}

From source file:divconq.net.ByteToMessageDecoder.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof ByteBuf) {
        RecyclableArrayList out = RecyclableArrayList.newInstance();

        try {//from ww w .  j  av a  2s  .  co  m
            ByteBuf data = (ByteBuf) msg;
            this.first = this.cumulation == null;

            if (this.first) {
                this.cumulation = data;
            } else {
                if (this.cumulation.writerIndex() > this.cumulation.maxCapacity() - data.readableBytes()
                        || this.cumulation.refCnt() > 1) {
                    // Expand cumulation (by replace it) when either there is not more room in the buffer
                    // or if the refCnt is greater then 1 which may happen when the user use slice().retain() or
                    // duplicate().retain().
                    //
                    // See:
                    // - https://github.com/netty/netty/issues/2327
                    // - https://github.com/netty/netty/issues/1764
                    this.expandCumulation(ctx, data.readableBytes());
                }

                this.cumulation.writeBytes(data);
                data.release();
            }

            this.callDecode(ctx, this.cumulation, out);
        } catch (DecoderException e) {
            throw e;
        } catch (Throwable t) {
            throw new DecoderException(t);
        } finally {
            if (this.cumulation != null && !this.cumulation.isReadable()) {
                this.cumulation.release();
                this.cumulation = null;
            }
            int size = out.size();
            this.decodeWasNull = size == 0;

            for (int i = 0; i < size; i++) {
                ctx.fireChannelRead(out.get(i));
            }
            out.recycle();
        }
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:divconq.net.ByteToMessageDecoder.java

License:Apache License

private void expandCumulation(ChannelHandlerContext ctx, int readable) {
    ByteBuf oldCumulation = this.cumulation;
    this.cumulation = ctx.alloc().buffer(oldCumulation.readableBytes() + readable);
    this.cumulation.writeBytes(oldCumulation);
    oldCumulation.release();
}

From source file:divconq.net.ssl.JdkSslClientContext.java

License:Apache License

public JdkSslClientContext(File certChainFile, TrustManagerFactory trustManagerFactory,
        Iterable<String> ciphers, Iterable<String> nextProtocols, long sessionCacheSize, long sessionTimeout)
        throws SSLException {

    super(ciphers);

    if (nextProtocols != null && nextProtocols.iterator().hasNext()) {
        throw new SSLException("NPN/ALPN unsupported: " + nextProtocols);
    } else {/*w ww. j  a v a2 s.  c om*/
        this.nextProtocols = Collections.emptyList();
    }

    try {
        if (certChainFile == null) {
            ctx = SSLContext.getInstance(PROTOCOL);
            if (trustManagerFactory == null) {
                ctx.init(null, null, null);
            } else {
                trustManagerFactory.init((KeyStore) null);
                ctx.init(null, trustManagerFactory.getTrustManagers(), null);
            }
        } else {
            KeyStore ks = KeyStore.getInstance("JKS");
            ks.load(null, null);
            CertificateFactory cf = CertificateFactory.getInstance("X.509");

            ByteBuf[] certs = PemReader.readCertificates(certChainFile);
            try {
                for (ByteBuf buf : certs) {
                    X509Certificate cert = (X509Certificate) cf
                            .generateCertificate(new ByteBufInputStream(buf));
                    X500Principal principal = cert.getSubjectX500Principal();
                    ks.setCertificateEntry(principal.getName("RFC2253"), cert);
                }
            } finally {
                for (ByteBuf buf : certs) {
                    buf.release();
                }
            }

            // Set up trust manager factory to use our key store.
            if (trustManagerFactory == null) {
                trustManagerFactory = TrustManagerFactory
                        .getInstance(TrustManagerFactory.getDefaultAlgorithm());
            }
            trustManagerFactory.init(ks);

            // Initialize the SSLContext to work with the trust managers.
            ctx = SSLContext.getInstance(PROTOCOL);
            ctx.init(null, trustManagerFactory.getTrustManagers(), null);
        }

        SSLSessionContext sessCtx = ctx.getClientSessionContext();
        if (sessionCacheSize > 0) {
            sessCtx.setSessionCacheSize((int) Math.min(sessionCacheSize, Integer.MAX_VALUE));
        }
        if (sessionTimeout > 0) {
            sessCtx.setSessionTimeout((int) Math.min(sessionTimeout, Integer.MAX_VALUE));
        }
    } catch (Exception e) {
        throw new SSLException("failed to initialize the server-side SSL context", e);
    }
}

From source file:divconq.net.ssl.JdkSslServerContext.java

License:Apache License

public JdkSslServerContext(File certChainFile, File keyFile, String keyPassword, Iterable<String> ciphers,
        Iterable<String> nextProtocols, long sessionCacheSize, long sessionTimeout) throws SSLException {

    super(ciphers);

    if (certChainFile == null) {
        throw new NullPointerException("certChainFile");
    }//from w w w .  j  a  v  a2s.  c  o m
    if (keyFile == null) {
        throw new NullPointerException("keyFile");
    }

    if (keyPassword == null) {
        keyPassword = "";
    }

    if (nextProtocols != null && nextProtocols.iterator().hasNext()) {
        throw new SSLException("NPN/ALPN unsupported: " + nextProtocols);
    } else {
        this.nextProtocols = Collections.emptyList();
    }

    String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
    if (algorithm == null) {
        algorithm = "SunX509";
    }

    try {
        KeyStore ks = KeyStore.getInstance("JKS");
        ks.load(null, null);
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        KeyFactory rsaKF = KeyFactory.getInstance("RSA");
        KeyFactory dsaKF = KeyFactory.getInstance("DSA");

        ByteBuf encodedKeyBuf = PemReader.readPrivateKey(keyFile);
        byte[] encodedKey = new byte[encodedKeyBuf.readableBytes()];
        encodedKeyBuf.readBytes(encodedKey).release();

        char[] keyPasswordChars = keyPassword.toCharArray();
        PKCS8EncodedKeySpec encodedKeySpec = generateKeySpec(keyPasswordChars, encodedKey);

        PrivateKey key;
        try {
            key = rsaKF.generatePrivate(encodedKeySpec);
        } catch (InvalidKeySpecException ignore) {
            key = dsaKF.generatePrivate(encodedKeySpec);
        }

        List<Certificate> certChain = new ArrayList<Certificate>();
        ByteBuf[] certs = PemReader.readCertificates(certChainFile);
        try {
            for (ByteBuf buf : certs) {
                certChain.add(cf.generateCertificate(new ByteBufInputStream(buf)));
            }
        } finally {
            for (ByteBuf buf : certs) {
                buf.release();
            }
        }

        ks.setKeyEntry("key", key, keyPasswordChars, certChain.toArray(new Certificate[certChain.size()]));

        // Set up key manager factory to use our key store
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
        kmf.init(ks, keyPasswordChars);

        // Initialize the SSLContext to work with our key managers.
        ctx = SSLContext.getInstance(PROTOCOL);
        ctx.init(kmf.getKeyManagers(), null, null);

        SSLSessionContext sessCtx = ctx.getServerSessionContext();
        if (sessionCacheSize > 0) {
            sessCtx.setSessionCacheSize((int) Math.min(sessionCacheSize, Integer.MAX_VALUE));
        }
        if (sessionTimeout > 0) {
            sessCtx.setSessionTimeout((int) Math.min(sessionTimeout, Integer.MAX_VALUE));
        }
    } catch (Exception e) {
        throw new SSLException("failed to initialize the server-side SSL context", e);
    }
}

From source file:divconq.net.ssl.PemReader.java

License:Apache License

static ByteBuf[] readCertificates(File file) throws CertificateException {
    String content;//  www  .j av a  2 s  . com
    try {
        content = readContent(file);
    } catch (IOException e) {
        throw new CertificateException("failed to read a file: " + file, e);
    }

    List<ByteBuf> certs = new ArrayList<ByteBuf>();
    Matcher m = CERT_PATTERN.matcher(content);
    int start = 0;
    for (;;) {
        if (!m.find(start)) {
            break;
        }

        ByteBuf base64 = Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII);
        ByteBuf der = Base64.decode(base64);
        base64.release();
        certs.add(der);

        start = m.end();
    }

    if (certs.isEmpty()) {
        throw new CertificateException("found no certificates: " + file);
    }

    return certs.toArray(new ByteBuf[certs.size()]);
}