use of java.util.zip.GZIPOutputStream in project uavstack by uavorg.
the class CompressHelper method compressByteArrWithGZIP.
/**
* Compress a byte array with GZIP
*
* @param byteArr
* a byte array
* @return a compressed byte array
* @throws IOException
*/
public static byte[] compressByteArrWithGZIP(byte[] byteArr) throws IOException {
ByteArrayOutputStream bos = null;
GZIPOutputStream gos = null;
try {
bos = new ByteArrayOutputStream();
gos = new GZIPOutputStream(bos);
gos.write(byteArr);
gos.flush();
} finally {
closeStream(gos);
closeStream(bos);
}
return bos.toByteArray();
}
use of java.util.zip.GZIPOutputStream in project gocd by gocd.
the class StageStorage method save.
// File writes are not atomic in nature. There can be cases where the file is not completely written to disk.
// This is why we write to a tmp file and move it over to the actual file. File moves are atomic operations.
// If rename failed because target file already exists, we just ignore it because some other thread may have generated it.
public void save(Graph graph) {
StageIdentifier identifier = extractStageIdentifier(graph);
if (isStageStored(identifier)) {
return;
}
synchronized (stageKey(identifier)) {
if (isStageStored(identifier)) {
return;
}
File file = new File(tmpStagePath(identifier));
file.getParentFile().mkdirs();
try {
OutputStream os = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(file)));
graph.persistToTurtle(os);
os.flush();
os.close();
file.renameTo(new File(stagePath(identifier)));
} catch (IOException e) {
throw new ShineRuntimeException(e);
} finally {
file.delete();
}
}
}
use of java.util.zip.GZIPOutputStream in project felix by apache.
the class Utils method merge.
public static void merge(File targetIndex, File target, File sourceIndex, File source) throws IOException {
List targetFiles = readIndex(targetIndex);
List sourceFiles = readIndex(sourceIndex);
List result = new ArrayList(targetFiles);
File manifestFile = new File(source, (String) sourceFiles.remove(0));
Manifest resultManifest = Utils.readManifest(manifestFile);
resultManifest.getMainAttributes().remove(new Name(Constants.DEPLOYMENTPACKAGE_FIXPACK));
for (Iterator i = result.iterator(); i.hasNext(); ) {
String targetFile = (String) i.next();
if (!MANIFEST_NAME.equals(targetFile) && !resultManifest.getEntries().containsKey(targetFile)) {
i.remove();
}
}
for (Iterator iter = sourceFiles.iterator(); iter.hasNext(); ) {
String path = (String) iter.next();
File from = new File(source, path);
File to = new File(target, path);
if (targetFiles.contains(path)) {
if (!to.delete()) {
throw new IOException("Could not delete " + to);
}
} else {
result.add(path);
}
if (!rename(from, to)) {
throw new IOException("Could not rename " + from + " to " + to);
}
}
targetFiles.removeAll(sourceFiles);
for (Iterator iter = resultManifest.getEntries().keySet().iterator(); iter.hasNext(); ) {
String path = (String) iter.next();
Attributes sourceAttribute = (Attributes) resultManifest.getEntries().get(path);
if ("true".equals(sourceAttribute.remove(new Name(Constants.DEPLOYMENTPACKAGE_MISSING)))) {
targetFiles.remove(path);
}
}
for (Iterator iter = targetFiles.iterator(); iter.hasNext(); ) {
String path = (String) iter.next();
File targetFile = new File(target, path);
if (!targetFile.delete()) {
throw new IOException("Could not delete " + targetFile);
}
}
GZIPOutputStream outputStream = new GZIPOutputStream(new FileOutputStream(new File(target, MANIFEST_NAME)));
try {
resultManifest.write(outputStream);
} finally {
outputStream.close();
}
writeIndex(targetIndex, result);
}
use of java.util.zip.GZIPOutputStream in project felix by apache.
the class GogoPlugin method doPost.
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
String encoding = request.getHeader("Accept-Encoding");
boolean supportsGzip = (encoding != null && encoding.toLowerCase().indexOf("gzip") > -1);
SessionTerminal st = terminalManager.getSessionTerminal(request);
if (st != null) {
String str = request.getParameter("k");
String f = request.getParameter("f");
String dump = st.handle(str, f != null && f.length() > 0);
if (dump != null) {
if (supportsGzip) {
response.setHeader("Content-Encoding", "gzip");
response.setHeader("Content-Type", "text/html; charset=UTF-8");
try {
GZIPOutputStream gzos = new GZIPOutputStream(response.getOutputStream());
gzos.write(dump.getBytes("UTF-8"));
gzos.close();
} catch (IOException ie) {
// handle the error here
ie.printStackTrace();
}
} else {
response.setContentType("text/html; charset=UTF-8");
response.getOutputStream().write(dump.getBytes("UTF-8"));
}
}
} else {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
response.flushBuffer();
}
use of java.util.zip.GZIPOutputStream in project openremote by openremote.
the class JSAPIWriter method writeJavaScript.
public void writeJavaScript(String base, HttpServletRequest req, HttpServletResponse resp, Map<String, ServiceRegistry> serviceRegistries) throws IOException {
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Starting JS API client");
}
// RESTEASY-776
// before writing generated javascript, we generate Etag and compare it with client request.
// If nothing changed, we send back 304 Not Modified for client browser to use cached js.
String ifNoneMatch = req.getHeader("If-None-Match");
String etag = generateEtag(serviceRegistries);
resp.setHeader("Etag", etag);
if (ifNoneMatch != null && ifNoneMatch.equals(etag)) {
resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
for (Map.Entry<String, ServiceRegistry> entry : serviceRegistries.entrySet()) {
String uri = base;
if (entry.getKey() != null)
uri += entry.getKey();
StringWriter stringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(new BufferedWriter(stringWriter));
writeJavaScript(uri, writer, entry.getValue());
writer.flush();
writer.close();
if (clientIsGzipSupported(req)) {
ByteArrayOutputStream compressedContent = new ByteArrayOutputStream();
GZIPOutputStream gzipstream = new GZIPOutputStream(compressedContent);
gzipstream.write(stringWriter.toString().getBytes(Charset.forName("utf-8")));
gzipstream.finish();
// get the compressed content
byte[] compressedBytes = compressedContent.toByteArray();
// set appropriate HTTP headers
resp.setContentLength(compressedBytes.length);
resp.addHeader("Content-Encoding", "gzip");
ServletOutputStream output = resp.getOutputStream();
output.write(compressedBytes);
output.flush();
output.close();
} else {
ServletOutputStream output = resp.getOutputStream();
byte[] bytes = stringWriter.toString().getBytes(Charset.forName("utf-8"));
resp.setContentLength(bytes.length);
output.write(bytes);
output.flush();
output.close();
}
}
}
Aggregations