use of java.util.zip.Deflater in project inbot-utils by Inbot.
the class CompressionUtils method compress.
public static byte[] compress(byte[] uncompressed) {
Deflater d = new Deflater();
d.setLevel(Deflater.BEST_COMPRESSION);
d.setInput(uncompressed);
d.finish();
ByteArrayOutputStream bos = new ByteArrayOutputStream(uncompressed.length);
// Compress the data
byte[] buf = new byte[1024];
while (!d.finished()) {
int count = d.deflate(buf);
bos.write(buf, 0, count);
}
try {
bos.close();
} catch (IOException e) {
throw new IllegalStateException("could not compress");
}
return bos.toByteArray();
}
use of java.util.zip.Deflater in project hive by apache.
the class InputJobInfo method writeObject.
/**
* Serialize this object, compressing the partitions which can exceed the
* allowed jobConf size.
* @see <a href="https://issues.apache.org/jira/browse/HCATALOG-453">HCATALOG-453</a>
*/
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
Deflater def = new Deflater(Deflater.BEST_COMPRESSION);
ObjectOutputStream partInfoWriter = new ObjectOutputStream(new DeflaterOutputStream(oos, def));
partInfoWriter.writeObject(partitions);
partInfoWriter.close();
}
use of java.util.zip.Deflater in project cxf by apache.
the class CompressionUtils method deflate.
public static byte[] deflate(byte[] tokenBytes, int level, boolean nowrap) {
Deflater compresser = new Deflater(level, nowrap);
compresser.setInput(tokenBytes);
compresser.finish();
byte[] output = new byte[tokenBytes.length * 2];
int compressedDataLength = compresser.deflate(output);
byte[] result = new byte[compressedDataLength];
System.arraycopy(output, 0, result, 0, compressedDataLength);
return result;
}
use of java.util.zip.Deflater in project FlareBot by FlareBot.
the class ApiRequester method getRequest.
private static Request getRequest(ApiRoute route, String bodyContent, MediaType type, boolean compressed) {
Request.Builder request = new Request.Builder().url(route.getFullUrl());
request.addHeader("Authorization", FlareBot.instance().getApiKey());
request.addHeader("User-Agent", "Mozilla/5.0 FlareBot");
RequestBody body = RequestBody.create(JSON, bodyContent);
if (compressed && !bodyContent.isEmpty()) {
request.addHeader("Content-Encoding", "gzip");
logger.debug("Starting compression: " + System.currentTimeMillis());
byte[] output = new byte[100];
Deflater deflater = new Deflater();
deflater.setInput(bodyContent.getBytes());
deflater.finish();
deflater.deflate(output);
deflater.end();
logger.debug("Finished compression: " + System.currentTimeMillis());
body = RequestBody.create(type, output);
}
logger.debug("Sending " + route.getRoute() + ", Type: " + type.toString() + ", Compressed: " + compressed);
switch(route.getMethod()) {
case GET:
request = request.get();
break;
case PATCH:
request = request.patch(body);
break;
case POST:
request = request.post(body);
break;
case PUT:
request = request.put(body);
break;
case DELETE:
request = request.delete(body);
break;
default:
throw new IllegalArgumentException("The route " + route.name() + " is using an unsupported method! Method: " + route.getMethod().name());
}
return request.build();
}
use of java.util.zip.Deflater in project bnd by bndtools.
the class CAFS method write.
/**
* Store an input stream in the CAFS while calculating and returning the
* SHA-1 code.
*
* @param in The input stream to store.
* @return The SHA-1 code.
* @throws Exception if anything goes wrong
*/
public SHA1 write(InputStream in) throws Exception {
Deflater deflater = new Deflater();
MessageDigest md = MessageDigest.getInstance(ALGORITHM);
DigestInputStream din = new DigestInputStream(in, md);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DeflaterOutputStream dout = new DeflaterOutputStream(bout, deflater);
copy(din, dout);
synchronized (store) {
// First check if it already exists
SHA1 sha1 = new SHA1(md.digest());
long search = index.search(sha1.digest());
if (search > 0)
return sha1;
byte[] compressed = bout.toByteArray();
// we need to append this file to our store,
// which requires a lock. However, we are in a race
// so others can get the lock between us getting
// the length and someone else getting the lock.
// So we must verify after we get the lock that the
// length was unchanged.
FileLock lock = null;
try {
long insertPoint;
int recordLength = compressed.length + HEADERLENGTH;
while (true) {
insertPoint = store.length();
lock = channel.lock(insertPoint, recordLength, false);
if (store.length() == insertPoint)
break;
// We got the wrong lock, someone else
// got in between reading the length
// and locking
lock.release();
}
int totalLength = deflater.getTotalIn();
store.seek(insertPoint);
update(sha1.digest(), compressed, totalLength);
index.insert(sha1.digest(), insertPoint);
return sha1;
} finally {
if (lock != null)
lock.release();
}
}
}
Aggregations