List of usage examples for java.util.zip GZIPInputStream close
public void close() throws IOException
From source file:es.mityc.firmaJava.libreria.utilidades.Base64.java
/** * Decodes data from Base64 notation, automatically * detecting gzip-compressed data and decompressing it. * * @param s the string to decode//from ww w . j av a 2 s . c o m * @param options encode options such as URL_SAFE * @return the decoded data * @since 1.4 */ public static byte[] decode(String s, int options) { byte[] bytes; try { bytes = s.getBytes(PREFERRED_ENCODING); } // end try catch (UnsupportedEncodingException uee) { bytes = s.getBytes(); } // end catch //</change> // Decode bytes = decode(bytes, 0, bytes.length, options); // Check to see if it's gzip-compressed // GZIP Magic Two-Byte Number: 0x8b1f (35615) int l = bytes.length; if (bytes != null && l >= 4) { int head = ((int) bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00); if (GZIPInputStream.GZIP_MAGIC == head) { ByteArrayInputStream bais = null; GZIPInputStream gzis = null; ByteArrayOutputStream baos = null; byte[] buffer = new byte[2048]; int length = 0; try { baos = new ByteArrayOutputStream(); bais = new ByteArrayInputStream(bytes); gzis = new GZIPInputStream(bais); int longitud = gzis.read(buffer); while ((length = longitud) >= 0) { baos.write(buffer, 0, length); longitud = gzis.read(buffer); } // end while: reading input // No error? Get new bytes. bytes = baos.toByteArray(); } // end try catch (IOException e) { // Just return originally-decoded bytes } // end catch finally { try { baos.close(); } catch (Exception e) { log.error(e); } try { gzis.close(); } catch (Exception e) { log.error(e); } try { bais.close(); } catch (Exception e) { log.error(e); } } // end finally } // end if: gzipped } // end if: bytes.length >= 2 return bytes; }
From source file:it.uniroma2.sag.kelp.wordspace.Wordspace.java
/** * Loads the word-vector pairs stored in the file whose path is <code>filename</code> * The file can be a plain text file or a .gz archive. * <p> //from w w w .j av a 2s. c om * The expected format is: </br> * number_of_vectors space_dimensionality</br> * word_i [TAB] 1.0 [TAB] 0 [TAB] vector values comma separated * </code> </br></br> Example: </br></br> <code> * 3 5</br> * dog::n [TAB] 1.0 [TAB] 0 [TAB] 2.1,4.1,1.4,2.3,0.9</br> * cat::n [TAB] 1.0 [TAB] 0 [TAB] 3.2,4.3,1.2,2.2,0.8</br> * mouse::n [TAB] 1.0 [TAB] 0 [TAB] 2.4,4.4,2.4,1.3,0.92</br> * * * @param filename the path of the file containing the word-vector pairs * @throws IOException */ private void populate(String filename) throws IOException { BufferedReader br = null; GZIPInputStream gzis = null; if (filename.endsWith(".gz")) { gzis = new GZIPInputStream(new FileInputStream(filename)); InputStreamReader reader = new InputStreamReader(gzis, "UTF8"); br = new BufferedReader(reader); } else { br = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "UTF8")); } String line; ArrayList<String> split; String label; String[] vSplit; Pattern iPattern = Pattern.compile(","); float[] v = null; while ((line = br.readLine()) != null) { if (!line.contains("\t")) continue; float norm2 = 0; split = mySplit(line); label = split.get(0); vSplit = iPattern.split(split.get(3), 0); if (v == null) v = new float[vSplit.length]; for (int i = 0; i < v.length; i++) { v[i] = Float.parseFloat(vSplit[i]); norm2 += v[i] * v[i]; } float norm = (float) Math.sqrt(norm2); for (int i = 0; i < v.length; i++) { v[i] /= norm; } DenseMatrix64F featureVector = new DenseMatrix64F(1, v.length); for (int i = 0; i < v.length; i++) { featureVector.set(0, i, (double) v[i]); } DenseVector denseFeatureVector = new DenseVector(featureVector); addWordVector(label, denseFeatureVector); } if (filename.endsWith(".gz")) { gzis.close(); } br.close(); }
From source file:iracing.webapi.IracingWebApi.java
private String getResponseText(URLConnection conn) throws IOException { byte[] bytes; // NOTE: The response is read into memory first before unzipping // because it resulted in a 200% performance increase // as opposed to unzipping directly from the connection inputstream // Read the response in to memory (GZipped or not) InputStream is = conn.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {/*from w ww . j a v a2 s . co m*/ byte[] b = new byte[1024]; int bytesRead; while ((bytesRead = is.read(b)) != -1) { baos.write(b, 0, bytesRead); } bytes = baos.toByteArray(); } finally { baos.close(); is.close(); } // System.err.println(conn.getURL().toString()); this.bytesReceived += bytes.length; // Unzip the response if necessary if ("gzip".equals(conn.getContentEncoding())) { // System.err.println("compressed : " + bytesReceived.length + " bytes"); GZIPInputStream is2 = null; try { is2 = new GZIPInputStream(new ByteArrayInputStream(bytes)); baos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int bytesRead; while ((bytesRead = is2.read(b)) != -1) { baos.write(b, 0, bytesRead); } bytes = baos.toByteArray(); } finally { baos.close(); is2.close(); } } // System.err.println("uncompressed : " + bytesReceived.length + " bytes"); return new String(bytes); }
From source file:com.android.tradefed.util.FileUtil.java
public static void extractTarGzip(File tarGzipFile, File destDir) throws FileNotFoundException, IOException, ArchiveException { GZIPInputStream gzipIn = null; ArchiveInputStream archivIn = null;/* w ww. j a v a2 s.c o m*/ BufferedInputStream buffIn = null; BufferedOutputStream buffOut = null; try { gzipIn = new GZIPInputStream(new BufferedInputStream(new FileInputStream(tarGzipFile))); archivIn = new ArchiveStreamFactory().createArchiveInputStream("tar", gzipIn); buffIn = new BufferedInputStream(archivIn); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) archivIn.getNextEntry()) != null) { String entryName = entry.getName(); String[] engtryPart = entryName.split("/"); StringBuilder fullPath = new StringBuilder(); fullPath.append(destDir.getAbsolutePath()); for (String e : engtryPart) { fullPath.append(File.separator); fullPath.append(e); } File destFile = new File(fullPath.toString()); if (entryName.endsWith("/")) { if (!destFile.exists()) destFile.mkdirs(); } else { if (!destFile.exists()) destFile.createNewFile(); buffOut = new BufferedOutputStream(new FileOutputStream(destFile)); byte[] buf = new byte[8192]; int len = 0; while ((len = buffIn.read(buf)) != -1) { buffOut.write(buf, 0, len); } buffOut.flush(); } } } finally { if (buffOut != null) { buffOut.close(); } if (buffIn != null) { buffIn.close(); } if (archivIn != null) { archivIn.close(); } if (gzipIn != null) { gzipIn.close(); } } }
From source file:com.taobao.diamond.sdkapi.impl.DiamondSDKManagerImpl.java
/** * Response//from w w w .j a v a 2 s .c om * * @param httpMethod * @return */ String getContent(HttpMethod httpMethod) throws UnsupportedEncodingException { StringBuilder contentBuilder = new StringBuilder(); if (isZipContent(httpMethod)) { // InputStream is = null; GZIPInputStream gzin = null; InputStreamReader isr = null; BufferedReader br = null; try { is = httpMethod.getResponseBodyAsStream(); gzin = new GZIPInputStream(is); isr = new InputStreamReader(gzin, ((HttpMethodBase) httpMethod).getResponseCharSet()); // br = new BufferedReader(isr); char[] buffer = new char[4096]; int readlen = -1; while ((readlen = br.read(buffer, 0, 4096)) != -1) { contentBuilder.append(buffer, 0, readlen); } } catch (Exception e) { log.error("", e); } finally { try { br.close(); } catch (Exception e1) { // ignore } try { isr.close(); } catch (Exception e1) { // ignore } try { gzin.close(); } catch (Exception e1) { // ignore } try { is.close(); } catch (Exception e1) { // ignore } } } else { // String content = null; try { content = httpMethod.getResponseBodyAsString(); } catch (Exception e) { log.error("", e); } if (null == content) { return null; } contentBuilder.append(content); } return StringEscapeUtils.unescapeHtml(contentBuilder.toString()); }
From source file:com.alcatel_lucent.nz.wnmsextract.reader.FileUtilities.java
public boolean decompressGZipFile(File input) { // Open the compressed file GZIPInputStream in = null; // If the file is of zero length delete it if (input.length() == 0) { deleteFile(input);// w w w .ja va2s . c o m return true; } try { in = new GZIPInputStream(new FileInputStream(input)); } catch (FileNotFoundException e) { jlog.fatal(".gz file not found: " + input); e.printStackTrace(); return false; } catch (IOException e) { jlog.fatal("IO exception on: " + input); e.printStackTrace(); return false; } // Open the output file String target = input.getAbsolutePath().replace(".gz", ".xml"); OutputStream out = null; try { out = new FileOutputStream(target); } catch (FileNotFoundException e) { jlog.fatal("File not found: " + target); e.printStackTrace(); return false; } // Transfer bytes from the compressed file to the output file byte[] buf = new byte[1024]; int len; try { while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } } catch (IOException e) { jlog.fatal("IO exception when writing to " + target); e.printStackTrace(); return false; } finally { // Close the file and stream try { in.close(); } catch (IOException e) { jlog.fatal("Can't close input stream"); e.printStackTrace(); return false; } try { out.close(); } catch (IOException e) { jlog.fatal("Can't close output stream"); e.printStackTrace(); return false; } } // Check if the gzip file size == 0 File outputFile = new File(target); // If the file is of zero length something bad happened if (outputFile.length() == 0) { return false; } // Delete the file deleteFile(input); return true; }
From source file:cn.leancloud.diamond.sdkapi.impl.DiamondSDKManagerImpl.java
/** * ?Response??/*from w w w .j a va 2 s. c o m*/ * * @param httpMethod * @return */ String getContent(HttpMethod httpMethod) throws UnsupportedEncodingException { StringBuilder contentBuilder = new StringBuilder(); if (isZipContent(httpMethod)) { // ??? InputStream is = null; GZIPInputStream gzin = null; InputStreamReader isr = null; BufferedReader br = null; try { is = httpMethod.getResponseBodyAsStream(); gzin = new GZIPInputStream(is); isr = new InputStreamReader(gzin, ((HttpMethodBase) httpMethod).getResponseCharSet()); // ????? br = new BufferedReader(isr); char[] buffer = new char[4096]; int readlen = -1; while ((readlen = br.read(buffer, 0, 4096)) != -1) { contentBuilder.append(buffer, 0, readlen); } } catch (Exception e) { log.error("", e); } finally { try { br.close(); } catch (Exception e1) { // ignore } try { isr.close(); } catch (Exception e1) { // ignore } try { gzin.close(); } catch (Exception e1) { // ignore } try { is.close(); } catch (Exception e1) { // ignore } } } else { // ??? String content = null; try { content = httpMethod.getResponseBodyAsString(); } catch (Exception e) { log.error("???", e); } if (null == content) { return null; } contentBuilder.append(content); } return StringEscapeUtils.unescapeHtml(contentBuilder.toString()); }
From source file:com.taobao.diamond.client.impl.DefaultDiamondSubscriber.java
/** * Response//from w w w .j a v a 2s . c o m * * @param httpMethod * @return */ String getContent(HttpMethod httpMethod) { StringBuilder contentBuilder = new StringBuilder(); if (isZipContent(httpMethod)) { // InputStream is = null; GZIPInputStream gzin = null; InputStreamReader isr = null; BufferedReader br = null; try { is = httpMethod.getResponseBodyAsStream(); gzin = new GZIPInputStream(is); isr = new InputStreamReader(gzin, ((HttpMethodBase) httpMethod).getResponseCharSet()); // br = new BufferedReader(isr); char[] buffer = new char[4096]; int readlen = -1; while ((readlen = br.read(buffer, 0, 4096)) != -1) { contentBuilder.append(buffer, 0, readlen); } } catch (Exception e) { log.error("", e); } finally { try { br.close(); } catch (Exception e1) { // ignore } try { isr.close(); } catch (Exception e1) { // ignore } try { gzin.close(); } catch (Exception e1) { // ignore } try { is.close(); } catch (Exception e1) { // ignore } } } else { // String content = null; try { content = httpMethod.getResponseBodyAsString(); } catch (Exception e) { log.error("", e); } if (null == content) { return null; } contentBuilder.append(content); } return contentBuilder.toString(); }
From source file:cn.leancloud.diamond.client.impl.DefaultDiamondSubscriber.java
/** * ?Response??/*from w w w. ja va2 s . c om*/ * * @param httpMethod * @return */ String getContent(HttpMethod httpMethod) { StringBuilder contentBuilder = new StringBuilder(); if (isZipContent(httpMethod)) { // ??? InputStream is = null; GZIPInputStream gzin = null; InputStreamReader isr = null; BufferedReader br = null; try { is = httpMethod.getResponseBodyAsStream(); gzin = new GZIPInputStream(is); isr = new InputStreamReader(gzin, ((HttpMethodBase) httpMethod).getResponseCharSet()); // ????? br = new BufferedReader(isr); char[] buffer = new char[4096]; int readlen = -1; while ((readlen = br.read(buffer, 0, 4096)) != -1) { contentBuilder.append(buffer, 0, readlen); } } catch (Exception e) { log.error("", e); } finally { try { br.close(); } catch (Exception e1) { // ignore } try { isr.close(); } catch (Exception e1) { // ignore } try { gzin.close(); } catch (Exception e1) { // ignore } try { is.close(); } catch (Exception e1) { // ignore } } } else { // ??? String content = null; try { content = httpMethod.getResponseBodyAsString(); } catch (Exception e) { log.error("???", e); } if (null == content) { return null; } contentBuilder.append(content); } return contentBuilder.toString(); }
From source file:org.openmrs.module.odkconnector.serialization.serializer.openmrs.PatientSerializerTest.java
@Test public void serialize_shouldSerializePatientInformation() throws Exception { File file = File.createTempFile("PatientSerialization", "Example"); GZIPOutputStream outputStream = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(file))); log.info("Writing to: " + file.getAbsolutePath()); Cohort cohort = new Cohort(); cohort.addMember(6);// w ww.jav a2 s . c om cohort.addMember(7); cohort.addMember(8); List<Patient> patients = new ArrayList<Patient>(); List<Obs> observations = new ArrayList<Obs>(); List<Form> forms = new ArrayList<Form>(); for (Integer patientId : cohort.getMemberIds()) { Patient patient = Context.getPatientService().getPatient(patientId); observations.addAll(Context.getObsService().getObservationsByPerson(patient)); patients.add(patient); } Serializer serializer = HandlerUtil.getPreferredHandler(Serializer.class, List.class); serializer.write(outputStream, patients); serializer.write(outputStream, observations); serializer.write(outputStream, forms); outputStream.close(); GZIPInputStream inputStream = new GZIPInputStream(new BufferedInputStream(new FileInputStream(file))); DataInputStream dataInputStream = new DataInputStream(inputStream); // total number of patients Integer patientCounter = dataInputStream.readInt(); System.out.println("Patient Counter: " + patientCounter); for (int i = 0; i < patientCounter; i++) { System.out.println("=================Patient====================="); System.out.println("Patient Id: " + dataInputStream.readInt()); System.out.println("Family Name: " + dataInputStream.readUTF()); System.out.println("Middle Name: " + dataInputStream.readUTF()); System.out.println("Last Name: " + dataInputStream.readUTF()); System.out.println("Gender: " + dataInputStream.readUTF()); System.out.println("Birth Date: " + dataInputStream.readUTF()); System.out.println("Identifier" + dataInputStream.readUTF()); } Integer obsCounter = dataInputStream.readInt(); for (int j = 0; j < obsCounter; j++) { System.out.println("==================Observation================="); System.out.println("Patient Id: " + dataInputStream.readInt()); System.out.println("Concept Name: " + dataInputStream.readUTF()); byte type = dataInputStream.readByte(); if (type == ObsSerializer.TYPE_STRING) System.out.println("Value: " + dataInputStream.readUTF()); else if (type == ObsSerializer.TYPE_INT) System.out.println("Value: " + dataInputStream.readInt()); else if (type == ObsSerializer.TYPE_DOUBLE) System.out.println("Value: " + dataInputStream.readDouble()); else if (type == ObsSerializer.TYPE_DATE) System.out.println("Value: " + dataInputStream.readUTF()); System.out.println("Time: " + dataInputStream.readUTF()); } Integer formCounter = dataInputStream.readInt(); for (int j = 0; j < formCounter; j++) { System.out.println("==================Form================="); System.out.println("Form Id: " + dataInputStream.readInt()); } System.out.println(); inputStream.close(); }