Java Code Examples for java.util.zip.GZIPInputStream#close()

The following examples show how to use java.util.zip.GZIPInputStream#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WorldDecorationHolder.java    From dsworkbench with Apache License 2.0 6 votes vote down vote up
private static void loadWorld() throws Exception {
  try {
    GZIPInputStream fin = new GZIPInputStream(new FileInputStream("world.dat.gz"));
    ByteBuffer bb = ByteBuffer.allocate(1000000);
    byte[] d = new byte[1024];
    int c = 0;
    while ((c = fin.read(d)) != -1) {
      bb.put(d, 0, c);
    }
    decoration = bb.array();
    fin.close();
  } catch (Exception e) {
    logger.error("Failed to read decoration file");
    throw new Exception("Unable to read decoration file", e);
  }
  loadTextures();
}
 
Example 2
Source File: Utils.java    From jstorm with Apache License 2.0 6 votes vote down vote up
public static byte[] gunzip(byte[] data) {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        GZIPInputStream in = new GZIPInputStream(bis);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = in.read(buffer)) >= 0) {
            bos.write(buffer, 0, len);
        }
        in.close();
        bos.close();
        return bos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 3
Source File: DocumentAPIItemBinaryExample.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
private static String uncompressString(ByteBuffer input) throws IOException {
    byte[] bytes = input.array();
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPInputStream is = new GZIPInputStream(bais);

    int chunkSize = 1024;
    byte[] buffer = new byte[chunkSize];
    int length = 0;
    while ((length = is.read(buffer, 0, chunkSize)) != -1) {
        baos.write(buffer, 0, length);
    }

    String result = new String(baos.toByteArray(), "UTF-8");

    is.close();
    baos.close();
    bais.close();

    return result;
}
 
Example 4
Source File: GzipUtils.java    From panama with MIT License 6 votes vote down vote up
/**
 *  gzip解密
 * @param data
 * @return
 * @throws Exception
 * byte[]
 */
public static byte[] ungzip(byte[] data) throws Exception {  
    ByteArrayInputStream bis = new ByteArrayInputStream(data);
    GZIPInputStream gzip = new GZIPInputStream(bis);
    byte[] buf = new byte[1024];  
    int num = -1;  
    ByteArrayOutputStream bos = new ByteArrayOutputStream();  
    while ((num = gzip.read(buf, 0, buf.length)) != -1) {  
        bos.write(buf, 0, num);  
    }  
    gzip.close();  
    bis.close();  
    byte[] ret = bos.toByteArray();  
    bos.flush();  
    bos.close();  
    return ret;  
}
 
Example 5
Source File: JoH.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
public static byte[] decompressBytesToBytes(byte[] bytes) {
    try {
        Log.d(TAG, "Decompressing  bytes size: " + bytes.length);
        byte[] buffer = new byte[8192];
        int bytes_read;
        ByteArrayInputStream input = new ByteArrayInputStream(bytes);
        ByteArrayOutputStream output = new ByteArrayOutputStream(bytes.length);
        GZIPInputStream gzipped_data = new GZIPInputStream(input, buffer.length);
        while ((bytes_read = gzipped_data.read(buffer)) != -1) {
            output.write(buffer, 0, bytes_read);
        }
        gzipped_data.close();
        input.close();
        // output.close();
        return output.toByteArray();
    } catch (Exception e) {
        Log.e(TAG, "Exception in decompress: " + e.toString());
        return new byte[0];
    }
}
 
Example 6
Source File: IOUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Decompress.
 * 
 * @param unSealmessage
 *            the un sealmessage
 * @return the byte[]
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static byte[] decompress(byte[] unSealmessage) throws IOException {
	long size = unSealmessage.length;
	ByteArrayInputStream inputstream = new ByteArrayInputStream(unSealmessage);
	GZIPInputStream input = new GZIPInputStream(inputstream);
	int i;
	ByteArrayOutputStream o = new ByteArrayOutputStream();

	byte buffer[] = new byte[1024];
	while ((i = input.read(buffer)) > 0) {
		o.write(buffer, 0, i);
	}
	o.flush();
	input.close();
	inputstream.close();
	o.close();
	byte[] ret = o.toByteArray();
	LOG.debug("Decompression of data from " + size + " bytes to " + ret.length + " bytes");
	return ret;
}
 
Example 7
Source File: AbstractTextEmbeddingIterator.java    From wekaDeeplearning4j with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Load wordVectors from a gzipped csv file
 */
private void loadGZipped() {
  try {
    GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(wordVectorLocation));
    File tmpFile =
        Paths.get(System.getProperty("java.io.tmpdir"), "wordmodel-tmp.csv").toFile();
    tmpFile.delete();
    FileOutputStream fos = new FileOutputStream(tmpFile);
    int length;
    byte[] buffer = new byte[1024];
    while ((length = gzis.read(buffer)) > 0) {
      fos.write(buffer, 0, length);
    }
    fos.close();
    gzis.close();

    // Try loading decompressed CSV file
    boolean success = loadEmbeddingFromCSV(tmpFile);
    if (!success) {
      throw new RuntimeException("Could not load the word vector file.");
    }
  } catch (IOException e) {
    e.printStackTrace();
  }
}
 
Example 8
Source File: IOUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Decompress.
 * 
 * @param unSealmessage
 *            the un sealmessage
 * @return the byte[]
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static byte[] decompress(byte[] unSealmessage) throws IOException {
	long size = unSealmessage.length;
	ByteArrayInputStream inputstream = new ByteArrayInputStream(unSealmessage);
	GZIPInputStream input = new GZIPInputStream(inputstream);
	int i;
	ByteArrayOutputStream o = new ByteArrayOutputStream();

	byte buffer[] = new byte[1024];
	while ((i = input.read(buffer)) > 0) {
		o.write(buffer, 0, i);
	}
	o.flush();
	input.close();
	inputstream.close();
	o.close();
	byte[] ret = o.toByteArray();
	LOG.debug("Decompression of data from " + size + " bytes to " + ret.length + " bytes");
	return ret;
}
 
Example 9
Source File: CommonUtil.java    From Cynthia with GNU General Public License v2.0 6 votes vote down vote up
public static String gzip2str(byte[] byteArray, String code)
		throws Exception {
	GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(
			byteArray));
	ByteArrayOutputStream baos = new ByteArrayOutputStream();

	byte[] buf = new byte[1024];

	while (true) {
		int len = gzis.read(buf);
		if (len == -1) {
			break;
		}

		baos.write(buf, 0, len);
	}

	gzis.close();
	baos.close();

	return baos.toString(code);
}
 
Example 10
Source File: SimpleXQueryTest.java    From vxquery with Apache License 2.0 6 votes vote down vote up
private static String gunzip(String dir, String filename) {
    try {
        GZIPInputStream in = new GZIPInputStream(
                new BufferedInputStream(new FileInputStream(new File(dir + filename + ".gz"))));
        File temp = File.createTempFile("vxquery", filename);
        temp.deleteOnExit();
        FileOutputStream out = new FileOutputStream(temp);
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
        return temp.getCanonicalPath();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 11
Source File: CompressUtil.java    From Ak47 with Apache License 2.0 6 votes vote down vote up
/**
 * GZIP decompression
 * 
 * GZIP解压
 * 
 * @param compressed                    sources bytes to be decompressed
 * @return                              decompressed bytes
 * @throws IOException                  i/o wrong
 */
public static byte[] decompressGzip(byte[] compressed) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ByteArrayInputStream bis = new ByteArrayInputStream(compressed);
    GZIPInputStream gis = new GZIPInputStream(bis);
    try {
        byte[] buffer = new byte[READ_BUFFER_SIZE];
        int read = 0;
        while ((read = gis.read(buffer)) != -1) {
            bos.write(buffer, 0, read);
        }
    } finally {
        gis.close();
    }

    return bos.toByteArray();
}
 
Example 12
Source File: GunzippingOutputStream.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
@Override
public Void call() throws IOException {
  GZIPInputStream in = new GZIPInputStream(mIn);
  try {
    StreamUtil.copy(in, mOut, new byte[1024]);
  } finally {
    in.close();
    mOut.close();
  }
  return null;
}
 
Example 13
Source File: GZipUtils.java    From util4j with Apache License 2.0 5 votes vote down vote up
/**
 * 数据解压缩
 * @param is
 * @param os
 * @throws Exception
 */
public static void decompress(InputStream is, OutputStream os) throws Exception {
	GZIPInputStream gis = new GZIPInputStream(is);
	try {
		int count;
		byte data[] = new byte[BUFFER];
		while ((count = gis.read(data, 0, BUFFER)) != -1) {
			os.write(data, 0, count);
		}
	}finally {
		gis.close();
	}
}
 
Example 14
Source File: GZipUtils.java    From Xndroid with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 数据解压缩
 *
 * @param is
 * @param os
 * @throws Exception
 */
public static void decompress(InputStream is, OutputStream os)
        throws Exception {

    GZIPInputStream gis = new GZIPInputStream(is);

    int count;
    byte data[] = new byte[BUFFER];
    while ((count = gis.read(data, 0, BUFFER)) != -1) {
        os.write(data, 0, count);
    }

    gis.close();
}
 
Example 15
Source File: TestHDFSCompressedDataStream.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
@Test
public void testGzipDurabilityWithSerializer() throws Exception {
  Context context = new Context();
  context.put("serializer", "AVRO_EVENT");

  HDFSCompressedDataStream writer = new HDFSCompressedDataStream();
  writer.configure(context);

  writer.open(fileURI, factory.getCodec(new Path(fileURI)),
      SequenceFile.CompressionType.BLOCK);

  String[] bodies = { "yarf!", "yarfing!" };
  writeBodies(writer, bodies);

  int found = 0;
  int expected = bodies.length;
  List<String> expectedBodies = Lists.newArrayList(bodies);

  GZIPInputStream cmpIn = new GZIPInputStream(new FileInputStream(file));
  DatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>();
  DataFileStream<GenericRecord> avroStream =
      new DataFileStream<GenericRecord>(cmpIn, reader);
  GenericRecord record = new GenericData.Record(avroStream.getSchema());
  while (avroStream.hasNext()) {
    avroStream.next(record);
    CharsetDecoder decoder = Charsets.UTF_8.newDecoder();
    String bodyStr = decoder.decode((ByteBuffer) record.get("body"))
        .toString();
    expectedBodies.remove(bodyStr);
    found++;
  }
  avroStream.close();
  cmpIn.close();

  Assert.assertTrue("Found = " + found + ", Expected = " + expected
      + ", Left = " + expectedBodies.size() + " " + expectedBodies,
      expectedBodies.size() == 0);
}
 
Example 16
Source File: JAXRSClientServerBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetBookQueryGZIP() throws Exception {
    String address = "http://localhost:" + PORT + "/bookstore/";
    WebClient wc = WebClient.create(address);
    wc.acceptEncoding("gzip,deflate");
    wc.encoding("gzip");
    InputStream r = wc.get(InputStream.class);
    assertNotNull(r);
    GZIPInputStream in = new GZIPInputStream(r);
    String s = IOUtils.toString(in);
    in.close();
    assertTrue(s, s.contains("id>124"));
}
 
Example 17
Source File: Trans.java    From pdfxtk with Apache License 2.0 5 votes vote down vote up
public static byte[] unzip(byte[] zipped) 
  throws IOException {
    GZIPInputStream zin = new GZIPInputStream(new ByteArrayInputStream(zipped));
    byte[] len          = new byte[4];
    zin.read(len);
    byte[] result = new byte[byte2int(len)];      
    zin.read(result);
    zin.close();
    return result;
}
 
Example 18
Source File: CustomURLServer.java    From BurpSuite-Team-Extension with GNU General Public License v3.0 4 votes vote down vote up
private String decompress(byte[] compressed) throws IOException {
    try {
        ByteArrayInputStream bis = new ByteArrayInputStream(compressed);
        GZIPInputStream gis = new GZIPInputStream(bis);
        BufferedReader br = new BufferedReader(new InputStreamReader(gis, StandardCharsets.UTF_8));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append("\n");
        }
        br.close();
        gis.close();
        bis.close();
        String strippedJson = sb.toString();

        this.sharedValues.getCallbacks().printOutput(
                "StrippedJson: " + strippedJson);
        String[] strippedJsonByDelimiter =
                strippedJson.split(new String(new byte[]{127}));
        for (String jsonPiece : strippedJsonByDelimiter) {
            this.sharedValues.getCallbacks().printOutput("Piece: "+ jsonPiece);
        }
        JsonObject httpService = new JsonObject();
        httpService.addProperty("host",strippedJsonByDelimiter[2].trim());
        httpService.addProperty("port",strippedJsonByDelimiter[3].trim());
        httpService.addProperty("protocol",strippedJsonByDelimiter[4].trim());
        JsonObject mainJson = new JsonObject();
        mainJson.add("request",
                this.sharedValues.getGson().newBuilder().create().toJsonTree(fromBytesToString(strippedJsonByDelimiter[0].getBytes())));
        mainJson.add("response",
                this.sharedValues.getGson().newBuilder().create().toJsonTree(fromBytesToString(strippedJsonByDelimiter[1].getBytes())));
        mainJson.add("httpService",httpService);

        return mainJson.toString();

    } catch (NumberFormatException e){
        sharedValues.getCallbacks().printError("Decompress: " +
                e.getMessage());
        return "";
    }

}
 
Example 19
Source File: SWN3LexiconEvaluator.java    From AffectiveTweets with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void processDict() throws IOException {


	FileInputStream fin = new FileInputStream(this.path);
	GZIPInputStream gzis = new GZIPInputStream(fin);
	InputStreamReader xover = new InputStreamReader(gzis);
	BufferedReader bf = new BufferedReader(xover);


	String line = "";

	// discard comments
	while ((line = bf.readLine()) != null) {
		if (line.startsWith("#") || line.startsWith("				#")) {
			continue;
		}

		String[] data = line.split("\t");

		// Difference between positive and negative score for one particular Synset
		Double polScore = Double.parseDouble(data[2])
				- Double.parseDouble(data[3]);

		// extract all the synset terms
		String[] sysSetTerms = data[4].split(" ");
		for (String w : sysSetTerms) {
			String[] w_n = w.split("#");

			String word=w_n[0];
			// the word's rank, small values indicate a more popular meaning
			// More popular word receive a higher weight
			int rank = Integer.parseInt(w_n[1]);

			if (this.dict.containsKey(word)) {
				Double prevScore=this.dict.get(word);
				this.dict.put(word, prevScore + polScore/(1+rank));
			} else {
				this.dict.put(word, polScore/(1+rank));
			}
		}
	}

	bf.close();
	xover.close();
	gzis.close();
	fin.close();
}
 
Example 20
Source File: TestGzipFilter.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
public void testGzipFilter() throws Exception {
  String path = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;

  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  GZIPOutputStream os = new GZIPOutputStream(bos);
  os.write(VALUE_1);
  os.close();
  byte[] value_1_gzip = bos.toByteArray();

  // input side filter

  Header[] headers = new Header[2];
  headers[0] = new BasicHeader("Content-Type", Constants.MIMETYPE_BINARY);
  headers[1] = new BasicHeader("Content-Encoding", "gzip");
  Response response = client.put(path, headers, value_1_gzip);
  assertEquals(200, response.getCode());

  Table table = TEST_UTIL.getConnection().getTable(TABLE);
  Get get = new Get(Bytes.toBytes(ROW_1));
  get.addColumn(Bytes.toBytes(CFA), Bytes.toBytes("1"));
  Result result = table.get(get);
  byte[] value = result.getValue(Bytes.toBytes(CFA), Bytes.toBytes("1"));
  assertNotNull(value);
  assertTrue(Bytes.equals(value, VALUE_1));

  // output side filter

  headers[0] = new BasicHeader("Accept", Constants.MIMETYPE_BINARY);
  headers[1] = new BasicHeader("Accept-Encoding", "gzip");
  response = client.get(path, headers);
  assertEquals(200, response.getCode());
  ByteArrayInputStream bis = new ByteArrayInputStream(response.getBody());
  GZIPInputStream is = new GZIPInputStream(bis);
  value = new byte[VALUE_1.length];
  is.read(value, 0, VALUE_1.length);
  assertTrue(Bytes.equals(value, VALUE_1));
  is.close();
  table.close();

  testScannerResultCodes();
}