List of usage examples for java.util.zip GZIPOutputStream GZIPOutputStream
public GZIPOutputStream(OutputStream out) throws IOException
From source file:edu.harvard.i2b2.fhir.Utils.java
public static byte[] compress(final String data, final String encoding) throws IOException { if (data == null || data.length() == 0) { return null; } else {//www .j a v a 2s. c o m byte[] bytes = data.getBytes(encoding); ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream os = new GZIPOutputStream(baos); os.write(bytes, 0, bytes.length); os.close(); byte[] result = baos.toByteArray(); return result; } }
From source file:io.druid.storage.hdfs.HdfsDataSegmentPullerTest.java
@Test public void testGZ() throws IOException, SegmentLoadingException { final Path zipPath = new Path("/tmp/testZip.gz"); final File outTmpDir = com.google.common.io.Files.createTempDir(); final File outFile = new File(outTmpDir, "testZip"); outFile.delete();//from ww w . ja v a 2 s. c om final URI uri = URI.create(uriBase.toString() + zipPath.toString()); try (final OutputStream outputStream = miniCluster.getFileSystem().create(zipPath); final OutputStream gzStream = new GZIPOutputStream(outputStream); final InputStream inputStream = new ByteArrayInputStream(pathByteContents)) { ByteStreams.copy(inputStream, gzStream); } try { Assert.assertFalse(outFile.exists()); puller.getSegmentFiles(new Path(uri), outTmpDir); Assert.assertTrue(outFile.exists()); Assert.assertArrayEquals(pathByteContents, Files.readAllBytes(outFile.toPath())); } finally { if (outFile.exists()) { outFile.delete(); } if (outTmpDir.exists()) { outTmpDir.delete(); } } }
From source file:io.druid.segment.loading.HdfsDataSegmentPullerTest.java
@Test public void testGZ() throws IOException, SegmentLoadingException { final Path zipPath = new Path("/tmp/testZip.gz"); final File outTmpDir = com.google.common.io.Files.createTempDir(); final File outFile = new File(outTmpDir, "testZip"); outFile.delete();//from w w w. j a v a2 s . c o m final URI uri = URI.create(uriBase.toString() + zipPath.toString()); try (final OutputStream outputStream = miniCluster.getFileSystem().create(zipPath); final OutputStream gzStream = new GZIPOutputStream(outputStream); final InputStream inputStream = new ByteArrayInputStream(pathByteContents)) { ByteStreams.copy(inputStream, gzStream); } try { Assert.assertFalse(outFile.exists()); puller.getSegmentFiles(uri, outTmpDir); Assert.assertTrue(outFile.exists()); Assert.assertArrayEquals(pathByteContents, Files.readAllBytes(outFile.toPath())); } finally { if (outFile.exists()) { outFile.delete(); } if (outTmpDir.exists()) { outTmpDir.delete(); } } }
From source file:com.sonicle.webtop.core.app.servlet.response.GZippableOutputStream.java
protected void writeToGZip(byte[] b, int off, int len) throws IOException { if (gzipOutputStream == null) { gzipOutputStream = new GZIPOutputStream(outputStream); ServletUtils.setCompressedContentHeader(response); }/*from w w w. j av a 2 s.c o m*/ gzipOutputStream.write(b, off, len); }
From source file:de.tudarmstadt.lt.lm.app.Ngrams.java
@Override public void run() { _num_ngrams = 0l;// w ww . j a va 2 s .co m _ngram = new FixedSizeFifoLinkedList<>(_order_to); _pout = System.out; if (!"-".equals(_out.trim())) { try { if (_out.endsWith(".gz")) _pout = new PrintStream(new GZIPOutputStream(new FileOutputStream(new File(_out)))); else _pout = new PrintStream(new FileOutputStream(new File(_out), true)); } catch (IOException e) { LOG.error("Could not open ouput file '{}' for writing.", _out, e); System.exit(1); } } try { if (_prvdr == null) { _prvdr = StartLM.getStringProviderInstance(_provider_type); _prvdr.setLanguageModel(new DummyLM<>(_order_to)); } } catch (Exception e) { LOG.error("Could not initialize Ngram generator. {}: {}", e.getClass(), e.getMessage(), e); } if ("-".equals(_file.trim())) { LOG.info("Processing text from stdin ('{}').", _file); try { run(new InputStreamReader(System.in, "UTF-8"), _file); } catch (Exception e) { LOG.error("Could not generate ngram from from file '{}'.", _file, e); } } else { File f_or_d = new File(_file); if (!f_or_d.exists()) throw new Error(String.format("File or directory '%s' not found.", _file)); if (f_or_d.isFile()) { LOG.info("Processing file '{}'.", f_or_d.getAbsolutePath()); try { run(new InputStreamReader(new FileInputStream(f_or_d), "UTF-8"), _file); } catch (Exception e) { LOG.error("Could not generate ngrams from file '{}'.", f_or_d.getAbsolutePath(), e); } } if (f_or_d.isDirectory()) { File[] txt_files = f_or_d.listFiles(new FileFilter() { @Override public boolean accept(File f) { return f.isFile() && f.getName().endsWith(".txt"); } }); for (int i = 0; i < txt_files.length; i++) { File f = txt_files[i]; LOG.info("Processing file '{}' ({}/{}).", f.getAbsolutePath(), i + 1, txt_files.length); try { run(new InputStreamReader(new FileInputStream(f), "UTF-8"), f.getAbsolutePath()); } catch (Exception e) { LOG.error("Could not generate ngrams from file '{}'.", f.getAbsolutePath(), e); } } } } LOG.info("Generated {} ngrams.", _num_ngrams); if (!"-".equals(_out.trim())) _pout.close(); }
From source file:org.runnerup.export.RunningAHEADSynchronizer.java
@Override public Status upload(SQLiteDatabase db, final long mID) { Status s;//from w w w . ja v a2s . c o m if ((s = connect()) != Status.OK) { return s; } String URL = IMPORT_URL + "?access_token=" + access_token; TCX tcx = new TCX(db); HttpURLConnection conn = null; Exception ex = null; try { StringWriter writer = new StringWriter(); tcx.export(mID, writer); conn = (HttpURLConnection) new URL(URL).openConnection(); conn.setDoOutput(true); conn.setRequestMethod(RequestMethod.POST.name()); conn.addRequestProperty("Content-Encoding", "gzip"); OutputStream out = new GZIPOutputStream(new BufferedOutputStream(conn.getOutputStream())); out.write(writer.toString().getBytes()); out.flush(); out.close(); int responseCode = conn.getResponseCode(); String amsg = conn.getResponseMessage(); Log.e(getName(), "code: " + responseCode + ", amsg: " + amsg); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); JSONObject obj = SyncHelper.parse(in); JSONObject data = obj.getJSONObject("data"); boolean found = false; if (found == false) { try { found = data.getJSONArray("workoutIds").length() == 1; } catch (JSONException e) { } } if (found == false) { try { found = data.getJSONArray("ids").length() == 1; } catch (JSONException e) { } } if (!found) { Log.e(getName(), "Unhandled response from RunningAHEADSynchronizer: " + obj); } if (responseCode == HttpStatus.SC_OK && found) { conn.disconnect(); return Status.OK; } ex = new Exception(amsg); } catch (IOException e) { ex = e; } catch (JSONException e) { ex = e; } s = Synchronizer.Status.ERROR; s.ex = ex; if (ex != null) { ex.printStackTrace(); } return s; }
From source file:net.myrrix.common.io.IOUtils.java
/** * @param delegate {@link OutputStream} to wrap * @return a {@link GZIPOutputStream} wrapping the given {@link OutputStream}. It attempts to use the new * Java 7 version that actually responds to {@link OutputStream#flush()} as expected. If not available, * uses the previous version ({@link GZIPOutputStream#GZIPOutputStream(OutputStream)}) *//*from www . j a va 2 s .c om*/ public static GZIPOutputStream buildGZIPOutputStream(OutputStream delegate) throws IOException { // In Java 7, GZIPOutputStream's flush() behavior can be made more as expected. Use it if possible // but fall back if not to the usual version try { return ClassUtils.loadInstanceOf(GZIPOutputStream.class, new Class<?>[] { OutputStream.class, boolean.class }, new Object[] { delegate, true }); } catch (IllegalStateException ignored) { return new GZIPOutputStream(delegate); } }
From source file:org.wso2.esb.integration.common.utils.clients.SimpleHttpClient.java
/** * Send a HTTP PATCH request to the specified URL * * @param url Target endpoint URL * @param headers Any HTTP headers that should be added to the request * @param payload Content payload that should be sent * @param contentType Content-type of the request * @return Returned HTTP response/*w w w. java 2s . co m*/ * @throws IOException If an error occurs while making the invocation */ public HttpResponse doPatch(String url, final Map<String, String> headers, final String payload, String contentType) throws IOException { HttpUriRequest request = new HttpPatch(url); setHeaders(headers, request); HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request; final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING)); EntityTemplate ent = new EntityTemplate(new ContentProducer() { public void writeTo(OutputStream outputStream) throws IOException { OutputStream out = outputStream; if (zip) { out = new GZIPOutputStream(outputStream); } out.write(payload.getBytes()); out.flush(); out.close(); } }); ent.setContentType(contentType); if (zip) { ent.setContentEncoding("gzip"); } entityEncReq.setEntity(ent); return client.execute(request); }
From source file:com.netflix.iep.http.RxHttp.java
private static HttpClientRequest<ByteBuf> compress(ClientConfig clientCfg, HttpClientRequest<ByteBuf> req, byte[] entity) { if (entity.length >= MIN_COMPRESS_SIZE && clientCfg.gzipEnabled()) { req.withHeader(HttpHeaders.Names.CONTENT_ENCODING, HttpHeaders.Values.GZIP); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (GZIPOutputStream gzip = new GZIPOutputStream(baos)) { gzip.write(entity);/*ww w . j a va2 s .c o m*/ } catch (IOException e) { // This isn't expected to occur throw new RuntimeException("failed to gzip request payload", e); } req.withContent(baos.toByteArray()); } else { req.withContent(entity); } return req; }
From source file:com.hichinaschool.flashcards.libanki.sync.BasicHttpSyncer.java
public HttpResponse req(String method, InputStream fobj, int comp, boolean hkey, JSONObject registerData, Connection.CancelCallback cancelCallback) { File tmpFileBuffer = null;//from w w w.j ava2 s . co m try { String bdry = "--" + BOUNDARY; StringWriter buf = new StringWriter(); HashMap<String, Object> vars = new HashMap<String, Object>(); // compression flag and session key as post vars vars.put("c", comp != 0 ? 1 : 0); if (hkey) { vars.put("k", mHKey); vars.put("s", mSKey); } for (String key : vars.keySet()) { buf.write(bdry + "\r\n"); buf.write(String.format(Locale.US, "Content-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n", key, vars.get(key))); } tmpFileBuffer = File.createTempFile("syncer", ".tmp", new File(AnkiDroidApp.getCacheStorageDirectory())); FileOutputStream fos = new FileOutputStream(tmpFileBuffer); BufferedOutputStream bos = new BufferedOutputStream(fos); GZIPOutputStream tgt; // payload as raw data or json if (fobj != null) { // header buf.write(bdry + "\r\n"); buf.write( "Content-Disposition: form-data; name=\"data\"; filename=\"data\"\r\nContent-Type: application/octet-stream\r\n\r\n"); buf.close(); bos.write(buf.toString().getBytes("UTF-8")); // write file into buffer, optionally compressing int len; BufferedInputStream bfobj = new BufferedInputStream(fobj); byte[] chunk = new byte[65536]; if (comp != 0) { tgt = new GZIPOutputStream(bos); while ((len = bfobj.read(chunk)) >= 0) { tgt.write(chunk, 0, len); } tgt.close(); bos = new BufferedOutputStream(new FileOutputStream(tmpFileBuffer, true)); } else { while ((len = bfobj.read(chunk)) >= 0) { bos.write(chunk, 0, len); } } bos.write(("\r\n" + bdry + "--\r\n").getBytes("UTF-8")); } else { buf.close(); bos.write(buf.toString().getBytes("UTF-8")); } bos.flush(); bos.close(); // connection headers String url = Collection.SYNC_URL; if (method.equals("register")) { url = url + "account/signup" + "?username=" + registerData.getString("u") + "&password=" + registerData.getString("p"); } else if (method.startsWith("upgrade")) { url = url + method; } else { url = url + "sync/" + method; } HttpPost httpPost = new HttpPost(url); HttpEntity entity = new ProgressByteEntity(tmpFileBuffer); // body httpPost.setEntity(entity); httpPost.setHeader("Content-type", "multipart/form-data; boundary=" + BOUNDARY); // HttpParams HttpParams params = new BasicHttpParams(); params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30); params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30)); params.setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false); params.setParameter(CoreProtocolPNames.USER_AGENT, "AnkiDroid-" + AnkiDroidApp.getPkgVersionName()); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpConnectionParams.setSoTimeout(params, Connection.CONN_TIMEOUT); // Registry SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", new EasySSLSocketFactory(), 443)); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, registry); if (cancelCallback != null) { cancelCallback.setConnectionManager(cm); } try { HttpClient httpClient = new DefaultHttpClient(cm, params); return httpClient.execute(httpPost); } catch (SSLException e) { Log.e(AnkiDroidApp.TAG, "SSLException while building HttpClient", e); return null; } } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } catch (IOException e) { Log.e(AnkiDroidApp.TAG, "BasicHttpSyncer.sync: IOException", e); return null; } catch (JSONException e) { throw new RuntimeException(e); } finally { if (tmpFileBuffer != null && tmpFileBuffer.exists()) { tmpFileBuffer.delete(); } } }