Java Code Examples for java.util.zip.GZIPInputStream

The following examples show how to use java.util.zip.GZIPInputStream. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: bistoury   Source File: GZFile.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * 主方法
 */
public static void main(String[] args) throws Exception {
    File file = new File("D:\\localhost.2018-10-31.log.gz");
    InputStream stream = new BufferedInputStream(new FileInputStream(file));
    if (isGZipFile(stream)) {
        GZIPInputStream ungzip = new GZIPInputStream(stream);
        InputStream inputStream = ungzip;
        OutputStream fos = new FileOutputStream("D:\\1.txt");
        byte[] b = new byte[1024];
        while (inputStream.read(b, 0, 1024) != -1) {
            fos.write(b, 0, 1024);
        }
        fos.flush();
    } else if (isGZipFile(stream)) {

    }
}
 
Example 2
Source Project: SVG-Android   Source File: FileUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static void unZipGzipFile(File source, File destination) throws IOException {
    if (destination.getParentFile().exists() || destination.getParentFile().mkdirs()) {
        FileInputStream fis = new FileInputStream(source);
        FileOutputStream fos = new FileOutputStream(destination);
        GZIPInputStream gis = new GZIPInputStream(fis);
        int count;
        byte[] data = new byte[1024];
        while ((count = gis.read(data, 0, 1024)) != -1) {
            fos.write(data, 0, count);
        }
        gis.close();
        fis.close();
        fos.flush();
        fos.close();
    }
}
 
Example 3
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request,
                   HttpServletResponse response) throws IOException, ServletException {
    if (!request.getHeader("X-SF-TOKEN").equals(AUTH_TOKEN)) {
        error("Invalid auth token", response, baseRequest);
        return;
    }
    if (!request.getHeader("User-Agent")
            .equals(AbstractHttpReceiverConnection.USER_AGENT)) {
        error("Invalid User agent: " + request.getHeader("User-Agent") + " vs "
                + AbstractHttpReceiverConnection.USER_AGENT, response, baseRequest);
        return;
    }
    SignalFxProtocolBuffers.DataPointUploadMessage all_datapoints =
            SignalFxProtocolBuffers.DataPointUploadMessage.parseFrom(
                    new GZIPInputStream(baseRequest.getInputStream()));
    if (!all_datapoints.getDatapoints(0).getSource().equals("source")) {
        error("Invalid datapoint source", response, baseRequest);
        return;
    }
    response.setStatus(HttpStatus.SC_OK);
    response.getWriter().write("\"OK\"");
    baseRequest.setHandled(true);
}
 
Example 4
Source Project: beihu-boot   Source File: GZIPUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param bytes
 * @param encoding
 * @return
 */
public static String uncompressToString(byte[] bytes, String encoding) {
    if (bytes == null || bytes.length == 0) {
        return null;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
    try {
        GZIPInputStream ungzip = new GZIPInputStream(in);
        byte[] buffer = new byte[256];
        int n;
        while ((n = ungzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
        }
        return out.toString(encoding);
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
 
Example 5
Source Project: tutorials   Source File: SwaggerBasePathRewritingFilterTest.java    License: MIT License 6 votes vote down vote up
@Test
public void run_on_valid_response_gzip() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
    RequestContext context = RequestContext.getCurrentContext();
    context.setRequest(request);

    MockHttpServletResponse response = new MockHttpServletResponse();
    context.setResponseGZipped(true);
    context.setResponse(response);

    context.setResponseDataStream(new ByteArrayInputStream(gzipData("{\"basePath\":\"/\"}")));

    filter.run();

    assertEquals("UTF-8", response.getCharacterEncoding());

    InputStream responseDataStream = new GZIPInputStream(context.getResponseDataStream());
    String responseBody = IOUtils.toString(responseDataStream, StandardCharsets.UTF_8);
    assertEquals("{\"basePath\":\"/service1\"}", responseBody);
}
 
Example 6
public static InputStream maybeDecompress(InputStream input) throws IOException {
  final PushbackInputStream pb = new PushbackInputStream(input, 2);

  int header = pb.read();
  if (header == -1) {
    return pb;
  }

  int firstByte = pb.read();
  if (firstByte == -1) {
    pb.unread(header);
    return pb;
  }

  pb.unread(new byte[]{(byte) header, (byte) firstByte});

  header = (firstByte << 8) | header;

  if (header == GZIPInputStream.GZIP_MAGIC) {
    return new GZIPInputStream(pb);
  } else {
    return pb;
  }
}
 
Example 7
Source Project: database   Source File: GZipCompressor.java    License: GNU General Public License v2.0 6 votes vote down vote up
public ByteBuffer decompress(final InputStream instr) {
	try {
		final GZIPInputStream gzin = new GZIPInputStream(instr);
		final DataInputStream din = new DataInputStream(gzin);
		
		final int length = din.readInt();
		final byte[] xbuf = new byte[length];
		for (int cursor = 0; cursor < length;) {
			final int rdlen = din.read(xbuf, cursor, (length - cursor));
			
			cursor += rdlen;
			
		}
		
		return ByteBuffer.wrap(xbuf);
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example 8
Source Project: cramtools   Source File: IndexAggregate.java    License: Apache License 2.0 6 votes vote down vote up
public static IndexAggregate forDataFile(SeekableStream stream, SAMSequenceDictionary dictionary)
		throws IOException {
	String path = stream.getSource();
	File indexFile = findIndexFileFor(path);
	if (indexFile == null)
		throw new FileNotFoundException("No index found for file: " + path);

	log.info("Using index file: " + indexFile.getAbsolutePath());
	IndexAggregate a = new IndexAggregate();
	if (indexFile.getName().matches("(?i).*\\.bai")) {
		a.bai = new CachingBAMFileIndex(indexFile, dictionary);
		return a;
	}
	if (indexFile.getName().matches("(?i).*\\.crai")) {
		a.crai = CramIndex.readIndex(new GZIPInputStream(new FileInputStream(indexFile)));
		return a;
	}

	throw new FileNotFoundException("No index found for file: " + path);
}
 
Example 9
Source Project: Man-Man   Source File: Man2HtmlTest.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testHtmlOutput() throws IOException {
    //FileInputStream fis = new FileInputStream("/usr/share/man/man1/systemctl.1.gz");
    //FileInputStream fis = new FileInputStream("/usr/share/man/man1/tar.1.gz");
    //FileInputStream fis = new FileInputStream("/usr/share/man/man8/sudo.8.gz");
    FileInputStream fis = new FileInputStream("/usr/share/man/man1/grep.1.gz");
    GZIPInputStream gis = new GZIPInputStream(fis);
    BufferedReader br = new BufferedReader(new InputStreamReader(gis));
    Man2Html m2h = new Man2Html(br);
    String result = m2h.getHtml();
    br.close();

    String homeDir = System.getProperty("user.home");
    FileOutputStream fos = new FileOutputStream(homeDir + File.separator + "test.html");
    OutputStreamWriter osw = new OutputStreamWriter(fos);
    osw.write(result);
    osw.close();

    Runtime.getRuntime().exec("xdg-open " + homeDir + File.separator + "test.html");
}
 
Example 10
Source Project: beatoraja   Source File: ScoreData.java    License: GNU General Public License v3.0 6 votes vote down vote up
public int[] decodeGhost() {
	try {
		if (ghost == null) {
			return null;
		}
		InputStream input = new ByteArrayInputStream(ghost.getBytes());
		InputStream base64 = Base64.getUrlDecoder().wrap(input);
		GZIPInputStream gzip = new GZIPInputStream(base64);
		if (gzip.available() == 0) {
			return null;
		}
		int[] value = new int[notes];
		for (int i=0; i<value.length; i++) {
			int judge = gzip.read();
			value[i] = judge >= 0 ? judge : 4;
		}
		gzip.close();
		return value;
	} catch (IOException e) {
		return null;
	}
}
 
Example 11
Source Project: jeesuite-libs   Source File: CompressUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * GZIP解压缩
 * 
 * @param bytes
 * @return
 */
public static byte[] gzipUncompress(byte[] bytes) {
	if (bytes == null || bytes.length == 0) {
		return null;
	}
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	ByteArrayInputStream in = new ByteArrayInputStream(bytes);
	try {
		GZIPInputStream ungzip = new GZIPInputStream(in);
		byte[] buffer = new byte[256];
		int n;
		while ((n = ungzip.read(buffer)) >= 0) {
			out.write(buffer, 0, n);
		}
	} catch (IOException e) {
	}

	return out.toByteArray();
}
 
Example 12
Source Project: io   Source File: DcResponse.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * This method is used to receive a stream of response body.
 * @param res Response object
 * @return Stream
 * @throws IOException Exception thrown
 */
protected final InputStream getResponseBodyInputStream(final HttpResponse res) throws IOException {
    // GZip 圧縮されていたら解凍する。
    /** thaw if it is GZip compression. */
    Header[] contentEncodingHeaders = res.getHeaders("Content-Encoding");
    if (contentEncodingHeaders.length > 0 && "gzip".equalsIgnoreCase(contentEncodingHeaders[0].getValue())) {
        return new GZIPInputStream(res.getEntity().getContent());
    } else {
        HttpEntity he = res.getEntity();
        if (he != null) {
            return he.getContent();
        } else {
            return null;
        }
    }
}
 
Example 13
Source Project: kourami   Source File: HLA.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private boolean checkHeader(SAMFileHeader header){
List<SAMSequenceRecord> sequences = header.getSequenceDictionary().getSequences();
HashSet<String> map = new HashSet<String>();

//load kourami panel sequence names
BufferedReader br;
try{
    br = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(HLA.MSAFILELOC + File.separator + "All_FINAL_with_Decoy.fa.gz"))));
    String curline = "";
    while((curline = br.readLine())!=null){
	if(curline.charAt(0) == ('>'))
	    map.add(curline.substring(1));
    }
    br.close();
}catch(IOException ioe){
    ioe.printStackTrace();
}

//check if input bam has sequences to kourami panel
for(SAMSequenceRecord ssr : sequences){
    if(!map.contains(ssr.getSequenceName()))
	return false;
}
return true;
   }
 
Example 14
@Test
public void testCompression() throws ServletException, IOException {
	// create an empty mock context
	MockServletContext context = new TestServletContext(true);
	MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.addHeader("Accept-Encoding", "gzip");
	request.setPathInfo("/org/geomajas/servlet/test.js");
	request.setMethod("GET");
	MockHttpServletResponse response = new MockHttpServletResponse();
	ResourceController resourceController = new ResourceController();
	resourceController.setServletContext(context);
	resourceController.getResource(request, response);
	Resource resource = new ClassPathResource("/org/geomajas/servlet/test.js");
	GZIPInputStream gzipInputStream = new GZIPInputStream(
			new ByteArrayInputStream(response.getContentAsByteArray()));
	Assert.assertArrayEquals(IOUtils.toByteArray(resource.getInputStream()), IOUtils.toByteArray(gzipInputStream));
}
 
Example 15
Source Project: RP-DBSCAN   Source File: Methods.java    License: Apache License 2.0 6 votes vote down vote up
public AssignCorePointToCluster(Configuration conf, String pairOutputPath) {
	// TODO Auto-generated constructor stub
	this.conf = conf;
	this.pairOutputPath = pairOutputPath;
	FileSystem fs = null;
	try {
	fs = FileSystem.get(this.conf);
	FileStatus[] status = fs.listStatus(new Path(Conf.metaResult));
	BufferedInputStream bi = new BufferedInputStream(fs.open(status[0].getPath()));
	GZIPInputStream gis = new GZIPInputStream(bi);
	ObjectInputStream ois = new ObjectInputStream(gis);
	this.clusters = (List<Cluster>)ois.readObject();
	ois.close();
	gis.close();
	bi.close();

	} catch (IOException | ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
 
Example 16
@Test
public void writeDirect() throws Exception {
    byte output[] = new byte[data.length];

    try (FileChannel fd = FileChannel.open(file.toPath(), StandardOpenOption.WRITE)) {
        try (FileWriter writer = new GzipWriter(new FileChannelWriter(fd, 0))) {
            ByteBuffer buf = ByteBuffer.allocateDirect(data.length);
            buf.put(data);
            buf.flip();
            while (buf.hasRemaining())
                writer.write(buf);
        }
    }

    try (InputStream input = new GZIPInputStream(new FileInputStream(file))) {
        int off = 0;
        while (off < output.length) {
            int rlen = input.read(output, off, output.length - off);
            assertNotEquals("insufficient bytes were written", -1, rlen);
            off += rlen;
        }
    }

    assertArrayEquals(data, output);
}
 
Example 17
Source Project: helix   Source File: ZkGrep.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Gunzip a file
 * @param zipFile
 * @return
 */
static File gunzip(File zipFile) {
  File outputFile = new File(stripGzSuffix(zipFile.getAbsolutePath()));

  byte[] buffer = new byte[1024];

  try {

    GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(zipFile));
    FileOutputStream out = new FileOutputStream(outputFile);

    int len;
    while ((len = gzis.read(buffer)) > 0) {
      out.write(buffer, 0, len);
    }

    gzis.close();
    out.close();

    return outputFile;
  } catch (IOException e) {
    LOG.error("fail to gunzip file: " + zipFile, e);
  }

  return null;
}
 
Example 18
Source Project: accumulo-recipes   Source File: AccumuloBlobStoreTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSaveAndQueryComplex() throws Exception {
    AccumuloBlobStore blobStore = new AccumuloBlobStore(getConnector(), CHUNK_SIZE);

    Collection<String> testValues = new ArrayList<String>(10);
    for (int i = 0; i < CHUNK_SIZE; i++)
        testValues.add(randomUUID().toString());

    //Store json in a Gzipped format
    OutputStream storageStream = blobStore.store("test", "1", currentTimeMillis(), "");
    mapper.writeValue(new GZIPOutputStream(storageStream), testValues);

    //reassemble the json after unzipping the stream.
    InputStream retrievalStream = blobStore.get("test", "1", Auths.EMPTY);
    Collection<String> actualValues = mapper.readValue(new GZIPInputStream(retrievalStream), strColRef);

    //if there were no errors, then verify that the two collections are equal.
    assertThat(actualValues, is(equalTo(testValues)));
}
 
Example 19
Source Project: gemfirexd-oss   Source File: MergeLogFiles.java    License: Apache License 2.0 6 votes vote down vote up
/**
     * Creates a new <code>Reader</code> that reads from the given log
     * file with the given name.  Invoking this constructor will start
     * this reader thread.
     * @param patterns java regular expressions that an entry must match one or more of
     */
    public NonThreadedReader(InputStream logFile, String logFileName,
                  ThreadGroup group, boolean tabOut, boolean suppressBlanks, List<Pattern> patterns) {
//         super(group, "Reader for " + ((logFileName != null) ? logFileName : logFile.toString()));
      if (logFileName.endsWith(".gz")) {
        try {
          this.logFile = new BufferedReader(new InputStreamReader(new GZIPInputStream(logFile)));
        } catch (IOException e) {
          System.err.println(logFileName + " does not appear to be in gzip format");
          this.logFile = new BufferedReader(new InputStreamReader(logFile));
        }
      } else {
        this.logFile =
          new BufferedReader(new InputStreamReader(logFile));
      }
      this.logFileName = logFileName;
      this.patterns = patterns;
//      this.suppressBlanks = suppressBlanks;
//      this.tabOut = tabOut;
      this.parser = new LogFileParser(this.logFileName, this.logFile, tabOut, suppressBlanks);
    }
 
Example 20
private InputStream urlToInputStream(URL url) throws IOException {
	URLConnection conn = url.openConnection();
	// pretend to be IE6
	conn.setRequestProperty("User-Agent", IE);
	// allow both GZip and Deflate (ZLib) encodings
	conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
	conn.connect();
	String encoding = conn.getContentEncoding();
	if ((encoding != null) && encoding.equalsIgnoreCase("gzip"))
		return new GZIPInputStream(conn.getInputStream());
	else if ((encoding != null) && encoding.equalsIgnoreCase("deflate"))
		return new InflaterInputStream(conn.getInputStream(), new Inflater(
			true));
	else
		return conn.getInputStream();
}
 
Example 21
Source Project: cocos-ui-libgdx   Source File: LyU.java    License: Apache License 2.0 6 votes vote down vote up
public static byte[] unGzip(byte[] encode) {
    ByteArrayInputStream bais = new ByteArrayInputStream(encode);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] result = null;
    try {
        GZIPInputStream gis = new GZIPInputStream(bais);
        int count;
        byte data[] = new byte[1024];
        while ((count = gis.read(data, 0, 1024)) != -1) {
            baos.write(data, 0, count);
        }
        gis.close();
        result = baos.toByteArray();
        baos.flush();
        baos.close();
        bais.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
 
Example 22
Source Project: Ngram-Graphs   Source File: INSECTCompressedMemoryDB.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public TObjectType loadObject(String sObjectName, String sObjectCategory) {
    ByteArrayInputStream bIn = new ByteArrayInputStream((byte[])ObjectMap.get(
            getObjectName(sObjectName, sObjectCategory)));
    TObjectType tObj = null;
    try {
        GZIPInputStream gzIn = new GZIPInputStream(bIn);
        ObjectInputStream oIn = new ObjectInputStream(gzIn);
        tObj = (TObjectType) oIn.readObject();
        oIn.close();
        gzIn.close();
        bIn.close();
    } catch (IOException iOException) {
        System.err.println("Cannot load object from memory. Reason:");
        iOException.printStackTrace(System.err);
    } catch (ClassNotFoundException classNotFoundException) {
        System.err.println("Cannot load object from memory. Reason:");
        classNotFoundException.printStackTrace(System.err);
    }
    
    return tObj;        
}
 
Example 23
Source Project: joshua   Source File: MIRACore.java    License: Apache License 2.0 6 votes vote down vote up
private void gunzipFile(String gzippedFileName, String outputFileName) {
  // NOTE: this will delete the original file

  try {
    GZIPInputStream in = new GZIPInputStream(new FileInputStream(gzippedFileName));
    FileOutputStream out = new FileOutputStream(outputFileName);

    byte[] buffer = new byte[4096];
    int len;
    while ((len = in.read(buffer)) > 0) {
      out.write(buffer, 0, len);
    }

    in.close();
    out.close();

    deleteFile(gzippedFileName);

  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 24
@Override
public void processDict() throws IOException  {
	// first, we open the file
	FileInputStream fin = new FileInputStream(this.path);
	GZIPInputStream gzis = new GZIPInputStream(fin);
	InputStreamReader xover = new InputStreamReader(gzis);
	BufferedReader bf = new BufferedReader(xover);

	String line;
	while ((line = bf.readLine()) != null) {
		String pair[] = line.split("\t");
		this.dict.put(pair[0], pair[1]);

	}
	bf.close();
	xover.close();
	gzis.close();
	fin.close();

}
 
Example 25
@Test
public void testSaveAndQueryComplex() throws Exception {
    ExtendedAccumuloBlobStore blobStore = new ExtendedAccumuloBlobStore(getConnector(), CHUNK_SIZE);

    Collection<String> testValues = new ArrayList<String>(10);
    for (int i = 0; i < CHUNK_SIZE; i++)
        testValues.add(randomUUID().toString());

    //Store json in a Gzipped format
    OutputStream storageStream = blobStore.store("test", "1", currentTimeMillis(), "");
    mapper.writeValue(new GZIPOutputStream(storageStream), testValues);

    //reassemble the json after unzipping the stream.
    InputStream retrievalStream = blobStore.get("test", "1", Auths.EMPTY);
    Collection<String> actualValues = mapper.readValue(new GZIPInputStream(retrievalStream), strColRef);

    //if there were no errors, then verify that the two collections are equal.
    assertThat(actualValues, is(equalTo(testValues)));
}
 
Example 26
Source Project: Tomcat8-Source-Read   Source File: GzipInterceptor.java    License: MIT License 5 votes vote down vote up
/**
 * @param data  Data to decompress
 * @return      Decompressed data
 * @throws IOException Compression error
 */
public static byte[] decompress(byte[] data) throws IOException {
    ByteArrayOutputStream bout =
        new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE);
    ByteArrayInputStream bin = new ByteArrayInputStream(data);
    GZIPInputStream gin = new GZIPInputStream(bin);
    byte[] tmp = new byte[DEFAULT_BUFFER_SIZE];
    int length = gin.read(tmp);
    while (length > -1) {
        bout.write(tmp, 0, length);
        length = gin.read(tmp);
    }
    return bout.toByteArray();
}
 
Example 27
Source Project: hadoop   Source File: TestGridmixSubmission.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Expands a file compressed using {@code gzip}.
 *
 * @param fs  the {@code FileSystem} corresponding to the given file.
 * @param in  the path to the compressed file.
 * @param out the path to the uncompressed output.
 * @throws Exception if there was an error during the operation.
 */
private void expandGzippedTrace(FileSystem fs, Path in, Path out)
        throws Exception {
  byte[] buff = new byte[4096];
  GZIPInputStream gis = new GZIPInputStream(fs.open(in));
  FSDataOutputStream fsdOs = fs.create(out);
  int numRead;
  while ((numRead = gis.read(buff, 0, buff.length)) != -1) {
    fsdOs.write(buff, 0, numRead);
  }
  gis.close();
  fsdOs.close();
}
 
Example 28
Source Project: jdk8u60   Source File: ReadUByte.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static void realMain(String[] args) throws Throwable {
    try {
        new GZIPInputStream(new BrokenInputStream());
        fail("Failed to throw expected IOException");
    } catch (IOException ex) {
        String msg = ex.getMessage();
        if (msg.indexOf("ReadUByte$BrokenInputStream.read() returned value out of range") < 0) {
            fail("IOException contains incorrect message: '" + msg + "'");
        }
    }
}
 
Example 29
Source Project: syndesis   Source File: CamelKSupport.java    License: Apache License 2.0 5 votes vote down vote up
public static String uncompress(byte[] data) throws IOException {
    if (data == null) {
        return null;
    }

    try(
        ByteArrayInputStream bis = new ByteArrayInputStream(data, 0, data.length);
        InputStream is = new GZIPInputStream(Base64.getDecoder().wrap(bis))
    ) {
        return IOUtils.toString(is, UTF_8);
    }
}
 
Example 30
Source Project: warp10-platform   Source File: UNGZIP.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
  Object o = stack.pop();

  if (!(o instanceof byte[])) {
    throw new WarpScriptException(getName() + " operates on a byte array.");
  }
  
  byte[] data = (byte[]) o;
  
  ByteArrayInputStream bin = new ByteArrayInputStream(data);
  ByteArrayOutputStream decompressed = new ByteArrayOutputStream(data.length);
  
  byte[] buf = new byte[1024];
  
  try {
    GZIPInputStream in = new GZIPInputStream(bin);

    while(true) {
      int len = in.read(buf);
      
      if (len < 0) {
        break;
      }
      
      decompressed.write(buf, 0, len);
    }
    
    in.close();
  } catch (IOException ioe) {
    throw new WarpScriptException(getName() + " encountered an error while decompressing.", ioe);
  }
  
  stack.push(decompressed.toByteArray());
  
  return stack;
}
 
Example 31
Source Project: yosegi   Source File: GzipCompressor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public byte[] decompress(
    final byte[] data , final int start , final int length ) throws IOException {
  ByteBuffer wrapBuffer = ByteBuffer.wrap( data , start , length );
  int dataLength = wrapBuffer.getInt();

  ByteArrayInputStream byteArrayIn =
      new ByteArrayInputStream( data , start + Integer.BYTES , length );
  GZIPInputStream in = new GZIPInputStream( byteArrayIn , 1024 * 256 );

  byte[] retVal = new byte[dataLength];
  InputStreamUtils.read( in , retVal , 0 , dataLength );

  return retVal;
}
 
Example 32
public static MixedMaterial load(Component parent) {
    Configuration config = Configuration.getInstance();
    File terrainDirectory = config.getTerrainDirectory();
    if ((terrainDirectory == null) || (! terrainDirectory.isDirectory())) {
        terrainDirectory = DesktopUtils.getDocumentsFolder();
    }
    File selectedFile = FileUtils.selectFileForOpen(SwingUtilities.getWindowAncestor(parent), "Select WorldPainter custom terrain file", terrainDirectory, new FileFilter() {
        @Override
        public boolean accept(File f) {
            return f.isDirectory() || f.getName().toLowerCase().endsWith(".terrain");
        }

        @Override
        public String getDescription() {
            return "WorldPainter Custom Terrains (*.terrain)";
        }
    });
    if (selectedFile != null) {
        try {
            try (ObjectInputStream in = new ObjectInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(selectedFile))))) {
                return MixedMaterial.duplicateNewMaterialsWhile(() -> (MixedMaterial) in.readObject());
            }
        } catch (IOException e) {
            throw new RuntimeException("I/O error while reading " + selectedFile, e);
        }
    }
    return null;
}
 
Example 33
Source Project: tlaplus   Source File: FileUtil.java    License: MIT License 5 votes vote down vote up
/**
 * Constructs a input stream from the file
 * @param file
 * @param useGzip
 * @param useIBuffers
 * @param buffersize
 * @return
 * @throws IOException
 * SZ Feb 20, 2009: FileNotFoundException removed
 */
public static InputStream newBZFileInputStream(String file, boolean useGzip, boolean useIBuffers, int buffersize)
        throws IOException
{
    if (useGzip)
    {
        return new GZIPInputStream(new FileInputStream(file), buffersize);
    } else if (useIBuffers)
    {
        return new BufferedInputStream(new FileInputStream(file), buffersize);
    } else
    {
        return new FileInputStream(file);
    }
}
 
Example 34
Source Project: endpoints-java   Source File: IoUtilTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGetRequestInputStream_emptyStream() throws Exception {
  MockHttpServletRequest request = new MockHttpServletRequest();
  request.setContent(new byte[0]);
  request.addHeader("Content-Encoding", "gzip");
  InputStream stream = IoUtil.getRequestInputStream(request);
  assertThat(stream).isNotInstanceOf(GZIPInputStream.class);
  assertThat(IoUtil.readStream(stream)).isEqualTo("");
}
 
Example 35
Source Project: cubedb   Source File: TestUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static List<DataRow> readFromJsonFileLineByLine(String gzipFile) throws IOException {
  GZIPInputStream source = new GZIPInputStream(new FileInputStream(new File(gzipFile)));
  String content = IOUtils.toString(source);
  List<DataRow> out = new ArrayList<>();
  TypeLiteral<DataRow> t = new TypeLiteral<DataRow>() {};

  for (String line : content.split("\n")) out.add(JsonIterator.deserialize(line, t));

  return out;
}
 
Example 36
@NonNull
private static InputStream getInputStreamFrom(
        @NonNull HttpURLConnection httpURLConnection) throws IOException {
    int httpResponseCode = httpURLConnection.getResponseCode();
    InputStream inputStream = httpResponseCode < 400
            ? httpURLConnection.getInputStream()
            : httpURLConnection.getErrorStream();
    inputStream = isGzipUsed(httpURLConnection)
            ? new GZIPInputStream(inputStream)
            : inputStream;
    return inputStream;
}
 
Example 37
Source Project: dkpro-jwpl   Source File: Tools.java    License: Apache License 2.0 5 votes vote down vote up
public static InputStream openInputFile(String arg) throws IOException {
	if (arg.equals("-")) {
		return openStandardInput();
	}
	InputStream infile = new BufferedInputStream(new FileInputStream(arg), IN_BUF_SZ);
	if (arg.endsWith(".gz")) {
		return new GZIPInputStream(infile);
	}
	else if (arg.endsWith(".bz2")) {
		return openBZip2Stream(infile);
	}
	else {
		return infile;
	}
}
 
Example 38
Source Project: cc-dbp   Source File: FileUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static String bytesToString(byte[] bs) {
	try {
		ByteArrayInputStream i = new ByteArrayInputStream(bs);
		GZIPInputStream gz = new GZIPInputStream(i);
		return readStreamAsString(gz);
	} catch (Exception e) {
		throw new Error(e);
	}
}
 
Example 39
Source Project: aw-reporting   Source File: CallableReportDownloader.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Transforms the downloaded result into a {@link File} object.
 *
 * @param reportStream the downloaded report stream
 * @return the downloaded report file
 */
@VisibleForTesting File handleReportStreamResult(InputStream reportStream)
    throws ReportProcessingException {
  Preconditions.checkState(reportStream != null, "Cannot get report data: input stream is NULL.");

  // Get clientCustomerId from session and covert to Long type. The string field was set from
  // Long type in AwqlReportDownloader, so it's able to parse back to Long.
  Long clientCustomerId = Long.parseLong(session.getClientCustomerId());

  try (GZIPInputStream gzipReportStream = new GZIPInputStream(reportStream)) {
    String filenamePrefix = reportDefinition.getReportType() + "-" + clientCustomerId + "-";
    File reportFile = File.createTempFile(filenamePrefix, ".csv");

    // Copy stream to CSV report.
    logger.debug("Starting processing rules of " + reportFile.getPath() + " report...");
    Streams.copy(gzipReportStream, new FileOutputStream(reportFile));
    logger.debug("... " + reportFile.getPath()  + " copied successfully.");

    return reportFile;
  } catch (IOException e) {
    String errorMessage = "Failed to load report data from stream for "
        + reportDefinition.getReportType() + " with account "
        + session.getClientCustomerId() + ".";
    logger.error(errorMessage, e);
    throw new ReportProcessingException(errorMessage, e);
  }
}
 
Example 40
Source Project: act   Source File: PubchemParserTest.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testParserProcessesTheCorrectChemicals() throws Exception {
  File testFile = new File(this.getClass().getResource("CompoundTest.xml.gz").getFile());

  String expectedInchi1 = "InChI=1S/C18H27FN2/c1-2-14-11-17(20-16-5-3-4-6-16)13-21(12-14)18-9-7-15(19)8-10-18/h7-10,14,16-17,20H,2-6,11-13H2,1H3";
  String expectedSmiles1 = "CCC1CC(CN(C1)C2=CC=C(C=C2)F)NC3CCCC3";
  String expectedCanonicalName1 = "N-cyclopentyl-5-ethyl-1-(4-fluorophenyl)piperidin-3-amine";
  Long expectedPubchemId1 = 84000001L;

  Chemical testChemical1 = new Chemical(1L, expectedPubchemId1, expectedCanonicalName1, expectedSmiles1);
  testChemical1.setInchi(expectedInchi1);

  String expectedInchi2 = "InChI=1S/C16H23FN2/c17-13-5-3-9-16(11-13)19-10-4-8-15(12-19)18-14-6-1-2-7-14/h3,5,9,11,14-15,18H,1-2,4,6-8,10,12H2";
  String expectedSmiles2 = "C1CCC(C1)NC2CCCN(C2)C3=CC(=CC=C3)F";
  String expectedCanonicalName2 = "N-cyclopentyl-1-(3-fluorophenyl)piperidin-3-amine";
  Long expectedPubchemId2 = 84000002L;

  Chemical testChemical2 = new Chemical(2L, expectedPubchemId2, expectedCanonicalName2, expectedSmiles2);
  testChemical2.setInchi(expectedInchi2);

  List<Chemical> expectedChemicals = new ArrayList<>();
  expectedChemicals.add(testChemical1);
  expectedChemicals.add(testChemical2);

  XMLInputFactory factory = XMLInputFactory.newInstance();
  XMLEventReader eventReader = factory.createXMLEventReader(new GZIPInputStream(new FileInputStream(testFile)));

  int counter = 0;
  Chemical actualChemical;
  while ((actualChemical = pubchemParser.extractNextChemicalFromXMLStream(eventReader)) != null) {
    Chemical expectedChemical = expectedChemicals.get(counter);
    assertEquals("Inchis parsed from the xml file should be the same as expected", expectedChemical.getInChI(), actualChemical.getInChI());
    assertEquals("Canonical name parsed from the xml file should be the same as expected", expectedChemical.getCanon(), actualChemical.getCanon());
    assertEquals("Smiles parsed from the xml file should be the same as expected", expectedChemical.getSmiles(), actualChemical.getSmiles());
    assertEquals("Pubchem id parsed from the xml file should be the same as expected", expectedChemical.getPubchemID(), actualChemical.getPubchemID());
    counter++;
  }

  assertEquals("Two chemicals should be parsed from the xml file", 2, counter);
}