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);
}
}
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);
}
}
Aggregations