java.util.zip.GZIPInputStream Java Examples

The following examples show how to use java.util.zip.GZIPInputStream. 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 Project: Man-Man   Author: Adonai   File: Man2HtmlTest.java    License: GNU General Public License v3.0 7 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 #2
Source Project: cocos-ui-libgdx   Author: varFamily   File: LyU.java    License: Apache License 2.0 7 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 #3
Source Project: bistoury   Author: qunarcorp   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 #4
Source Project: SVG-Android   Author: MegatronKing   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 #5
Source Project: signalfx-java   Author: signalfx   File: HttpDataPointProtobufReceiverConnectionTest.java    License: Apache License 2.0 6 votes vote down vote up
@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 #6
Source Project: beihu-boot   Author: beihu-stack   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 #7
Source Project: tutorials   Author: eugenp   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 #8
Source Project: timbuctoo   Author: HuygensING   File: ResourceSyncFileLoader.java    License: GNU General Public License v3.0 6 votes vote down vote up
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 #9
Source Project: database   Author: blazegraph   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 #10
Source Project: cramtools   Author: enasequence   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 #11
Source Project: beatoraja   Author: exch-bms2   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 #12
Source Project: jeesuite-libs   Author: vakinge   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 #13
Source Project: io   Author: fujitsu-pio   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 #14
Source Project: kourami   Author: Kingsford-Group   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 #15
@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 #16
Source Project: RP-DBSCAN   Author: kaist-dmlab   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 #17
Source Project: monsoon   Author: groupon   File: GzipWriterTest.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@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 #18
Source Project: helix   Author: apache   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 #19
Source Project: accumulo-recipes   Author: calrissian   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 #20
Source Project: gemfirexd-oss   Author: gemxd   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 #21
Source Project: stategen   Author: stategen   File: JavadocParanamer.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
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 #22
Source Project: Ngram-Graphs   Author: ayushoriginal   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   Author: apache   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
Source Project: AffectiveTweets   Author: felipebravom   File: IntensityLexiconEvaluator.java    License: GNU General Public License v3.0 6 votes vote down vote up
@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
Source Project: accumulo-recipes   Author: calrissian   File: ExtendedAccumuloBlobStoreTest.java    License: Apache License 2.0 6 votes vote down vote up
@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   Author: chenmudu   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   Author: naver   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   Author: chenghanpeng   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   Author: syndesisio   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   Author: senx   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;
}