Search in sources :

Example 1 with NetworkResponsePackage

use of org.bboxdb.network.packages.NetworkResponsePackage in project bboxdb by jnidzwetzki.

the class ClientConnectionHandler method flushPendingCompressionPackages.

/**
 * Write all pending compression packages to server, called by the maintainance thread
 */
protected void flushPendingCompressionPackages() {
    final List<NetworkResponsePackage> packagesToWrite = new ArrayList<>();
    synchronized (pendingCompressionPackages) {
        if (pendingCompressionPackages.isEmpty()) {
            return;
        }
        packagesToWrite.addAll(pendingCompressionPackages);
        pendingCompressionPackages.clear();
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Chunk size is: {}", packagesToWrite.size());
    }
    final NetworkResponsePackage compressionEnvelopeRequest = new CompressionEnvelopeResponse(NetworkConst.COMPRESSION_TYPE_GZIP, packagesToWrite);
    try {
        writePackageToSocket(compressionEnvelopeRequest);
    } catch (PackageEncodeException | IOException e) {
        logger.error("Got an exception while write pending compression packages to client", e);
    }
}
Also used : CompressionEnvelopeResponse(org.bboxdb.network.packages.response.CompressionEnvelopeResponse) NetworkResponsePackage(org.bboxdb.network.packages.NetworkResponsePackage) ArrayList(java.util.ArrayList) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) IOException(java.io.IOException)

Example 2 with NetworkResponsePackage

use of org.bboxdb.network.packages.NetworkResponsePackage in project bboxdb by jnidzwetzki.

the class CompressionEnvelopeResponse method writeToOutputStream.

@Override
public long writeToOutputStream(final OutputStream outputStream) throws PackageEncodeException {
    try {
        if (compressionType != NetworkConst.COMPRESSION_TYPE_GZIP) {
            throw new PackageEncodeException("Unknown compression method: " + compressionType);
        }
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final OutputStream os = new GZIPOutputStream(baos);
        // Write packages
        for (final NetworkResponsePackage networkResponsePackage : networkResponsePackages) {
            networkResponsePackage.writeToOutputStream(os);
        }
        os.close();
        final byte[] compressedBytes = baos.toByteArray();
        // Header
        final ByteBuffer bb = ByteBuffer.allocate(4);
        bb.order(Const.APPLICATION_BYTE_ORDER);
        bb.put(compressionType);
        bb.putShort((short) networkResponsePackages.size());
        // Body length
        final long bodyLength = bb.capacity() + compressedBytes.length;
        // Write body length
        final long headerLength = appendResponsePackageHeader(bodyLength, outputStream);
        // Write body
        outputStream.write(bb.array());
        outputStream.write(compressedBytes);
        return headerLength + bodyLength;
    } catch (IOException e) {
        throw new PackageEncodeException("Got exception while converting package into bytes", e);
    }
}
Also used : NetworkResponsePackage(org.bboxdb.network.packages.NetworkResponsePackage) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

IOException (java.io.IOException)2 NetworkResponsePackage (org.bboxdb.network.packages.NetworkResponsePackage)2 PackageEncodeException (org.bboxdb.network.packages.PackageEncodeException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 CompressionEnvelopeResponse (org.bboxdb.network.packages.response.CompressionEnvelopeResponse)1