read ( )源码实例Demo

java.util.zip.GZIPInputStream#read ( )源码实例Demo

下面列出了java.util.zip.GZIPInputStream#read ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: anthelion   文件: GZIPUtils.java
/**
 * Returns an gunzipped copy of the input array.  
 * @throws IOException if the input cannot be properly decompressed
 */
public static final byte[] unzip(byte[] in) throws IOException {
  // decompress using GZIPInputStream 
  ByteArrayOutputStream outStream = 
    new ByteArrayOutputStream(EXPECTED_COMPRESSION_RATIO * in.length);

  GZIPInputStream inStream = 
    new GZIPInputStream ( new ByteArrayInputStream(in) );

  byte[] buf = new byte[BUF_SIZE];
  while (true) {
    int size = inStream.read(buf);
    if (size <= 0) 
      break;
    outStream.write(buf, 0, size);
  }
  outStream.close();

  return outStream.toByteArray();
}
 
源代码2 项目: cocos-ui-libgdx   文件: LyU.java
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;
}
 
源代码3 项目: libcurldroid   文件: Result.java
/**
 * 
 * @return decoded if body gzipped
 */
public byte[] getDecodedBody() throws IOException {
	if (!"gzip".equalsIgnoreCase(getHeader("Content-Encoding"))) {
		return body;
	}
	if (decodedBody == null) {
		Log.d(TAG, "uncompress gzipped content");
		GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(body));
		ByteArrayOutputStream byos = new ByteArrayOutputStream(body.length * 3);
		byte[] buf = new byte[4096];
		int len;
		while ((len = gzis.read(buf, 0, buf.length)) != -1) {
			byos.write(buf, 0, len);
		}
		decodedBody = byos.toByteArray();
		gzis.close();
		byos.close();
	}
	
	return decodedBody;
}
 
源代码4 项目: deeplearning4j   文件: WritableUtils.java
public static byte[] readCompressedByteArray(DataInput in) throws IOException {
    int length = in.readInt();
    if (length == -1)
        return null;
    byte[] buffer = new byte[length];
    in.readFully(buffer); // could/should use readFully(buffer,0,length)?
    GZIPInputStream gzi = new GZIPInputStream(new ByteArrayInputStream(buffer, 0, buffer.length));
    byte[] outbuf = new byte[length];
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int len;
    while ((len = gzi.read(outbuf, 0, outbuf.length)) != -1) {
        bos.write(outbuf, 0, len);
    }
    byte[] decompressed = bos.toByteArray();
    bos.close();
    gzi.close();
    return decompressed;
}
 
源代码5 项目: search-spring-boot-starter   文件: CommonUtils.java
public static String unGzip(InputStream ins) throws Exception {
    GZIPInputStream gis = new GZIPInputStream(ins);
    byte[] b = new byte[512];
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int read = 0;
    while ((read = gis.read(b)) != -1) {
        bos.write(b, 0, read);
    }
    byte[] outbyte = bos.toByteArray();
    bos.flush();
    bos.close();
    gis.close();
    ins.close();

    String result = new String(outbyte, "utf-8");
    return result;
}
 
源代码6 项目: Cynthia   文件: CommonUtil.java
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);
}
 
源代码7 项目: Decoder-Improved   文件: GZIPDecoder.java
@Override
public byte[] modifyBytes(byte[] input) throws ModificationException {
    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(input);
        GZIPInputStream gzis = new GZIPInputStream(bais);

        byte[] buffer = new byte[input.length * 2];
        int bytesRead;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        while ((bytesRead = gzis.read(buffer)) != -1) {
            output.write(buffer, 0, bytesRead);
        }
        return output.toByteArray();
    } catch (IOException e) {
        Logger.printErrorFromException(e);
        throw new ModificationException("Invalid GZIP Input");
    }
}
 
源代码8 项目: j2objc   文件: GZIPInputStreamTest.java
/**
 * Regression test for HARMONY-3703.
 *
 * @tests java.util.zip.GZIPInputStream#read()
 */
public void test_read() throws IOException {
    int result = 0;
    byte[] buffer = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    File f = new File(resources.getAbsolutePath() + "test.gz");
    FileOutputStream out = new FileOutputStream(f);
    GZIPOutputStream gout = new GZIPOutputStream(out);

    // write 100 bytes to the stream
    for (int i = 0; i < 10; i++) {
        gout.write(buffer);
    }
    gout.finish();
    out.write(1);
    out.close();
    gout.close();

    GZIPInputStream gis = new GZIPInputStream(new FileInputStream(f));
    buffer = new byte[100];
    gis.read(buffer);
    result = gis.read();
    gis.close();
    f.delete();

    assertEquals("Incorrect value returned at the end of the file", -1, result);
}
 
源代码9 项目: jeesuite-libs   文件: GzipUtils.java
/**
 * 
 * @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();
	}
	return null;
}
 
源代码10 项目: mt-flume   文件: TestHDFSCompressedDataStream.java
@Test
public void testGzipDurability() throws Exception {
  Context context = new Context();
  HDFSCompressedDataStream writer = new HDFSCompressedDataStream();
  writer.configure(context);
  writer.open(fileURI, factory.getCodec(new Path(fileURI)),
      SequenceFile.CompressionType.BLOCK);

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

  byte[] buf = new byte[256];
  GZIPInputStream cmpIn = new GZIPInputStream(new FileInputStream(file));
  int len = cmpIn.read(buf);
  String result = new String(buf, 0, len, Charsets.UTF_8);
  result = result.trim(); // BodyTextEventSerializer adds a newline

  Assert.assertEquals("input and output must match", bodies[0], result);
}
 
源代码11 项目: Ffast-Java   文件: GzipUtils.java
/**
 * 解压
 * @param data
 * @return
 * @throws Exception
 */
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;
}
 
源代码12 项目: vespa   文件: TestUtils.java
public static String zipStreamToString(InputStream inputStream) throws IOException {
    GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
    final StringBuilder rawContent = new StringBuilder();
    while (true) {
        int x = gzipInputStream.read();
        if (x < 0) {
            break;
        }
        rawContent.append((char) x);
    }
    return rawContent.toString();
}
 
源代码13 项目: warp10-platform   文件: UNGZIP.java
@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;
}
 
源代码14 项目: huobi_Java   文件: InternalUtils.java
private static void decompress(InputStream is, OutputStream os) throws IOException {
  GZIPInputStream gis = new GZIPInputStream(is);
  int count;
  byte[] data = new byte[1024];
  while ((count = gis.read(data, 0, 1024)) != -1) {
    os.write(data, 0, count);
  }
  gis.close();
}
 
源代码15 项目: MikuMikuStudio   文件: GZIPSerializer.java
@SuppressWarnings("unchecked")
public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
    try
    {
        GZIPCompressedMessage result = new GZIPCompressedMessage();

        byte[] byteArray = new byte[data.remaining()];

        data.get(byteArray);

        GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(byteArray));
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        byte[] tmp = new byte[9012];
        int read;

        while (in.available() > 0 && ((read = in.read(tmp)) > 0)) {
            out.write(tmp, 0, read);
        }

        result.setMessage((Message)Serializer.readClassAndObject(ByteBuffer.wrap(out.toByteArray())));
        return (T)result;
    }
    catch (Exception e) {
        e.printStackTrace();
        throw new IOException(e.toString());
    }
}
 
源代码16 项目: radiance   文件: SvgBatikResizableIcon.java
/**
 * Constructs an input stream with uncompressed contents from the specified
 * input stream with compressed contents.
 * 
 * @param zippedStream
 *            Input stream with compressed contents.
 * @return Input stream with uncompressed contents.
 * @throws IOException
 *             in case any I/O operation failed.
 */
protected static InputStream constructFromZipStream(InputStream zippedStream)
		throws IOException {
	GZIPInputStream gis = new GZIPInputStream(zippedStream);
	ByteArrayOutputStream baos = new ByteArrayOutputStream();

	byte[] buf = new byte[2048];
	int len;
	while ((len = gis.read(buf)) != -1) {
		baos.write(buf, 0, len);
	}

	return new ByteArrayInputStream(baos.toByteArray());
}
 
源代码17 项目: panama   文件: GzipUtils.java
/**
 * gizp数据解压
 * @param bytes
 * @return
 * @throws IOException
 * String
 */
public static String uncompress(byte[] bytes) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();   
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);   
    GZIPInputStream gunzip = new GZIPInputStream(in);   
    byte[] buffer = new byte[256];   
    int n;   
    while ((n = gunzip.read(buffer))>= 0) {   
        out.write(buffer, 0, n);   
    }   
    // toString()使用平台默认编码,也可以显式的指定如toString(&quot;GBK&quot;)   
    return out.toString();   
}
 
源代码18 项目: bidder   文件: HttpPostGet.java
/**
 * Send an HTTP get, once the http url is defined.
 * 
 * @param url
 *            . The url string to send.
 * @return String. The HTTP response to the GET
 * @throws Exception
 *             on network errors.
 */
public String sendGet(String url, int connTimeout, int readTimeout) throws Exception {

	URL obj = new URL(url);
	http = (HttpURLConnection) obj.openConnection();
	http.setConnectTimeout(connTimeout);
	http.setReadTimeout(readTimeout);
	http.setRequestProperty("Connection", "keep-alive");

	// optional default is GET
	http.setRequestMethod("GET");

	// add request header
	http.setRequestProperty("User-Agent", USER_AGENT);

	int responseCode = http.getResponseCode();
	// System.out.println("\nSending 'GET' request to URL : " + url);
	// System.out.println("Response Code : " + responseCode);
	
	
	String value = http.getHeaderField("Content-Encoding");
	
	if (value != null && value.equals("gzip")) {
		byte bytes [] = new byte[4096];
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		GZIPInputStream gis = new GZIPInputStream(http.getInputStream());
		while(true) {
			int n = gis.read(bytes);
			if (n < 0) break;
			baos.write(bytes,0,n);
		}
		return new String(baos.toByteArray());
	}

	BufferedReader in = new BufferedReader(new InputStreamReader(
			http.getInputStream()));
	
	String inputLine;
	StringBuilder response = new StringBuilder();

	while ((inputLine = in.readLine()) != null) {
		response.append(inputLine);
	}
	in.close();
	return response.toString();

}
 
源代码19 项目: warp10-platform   文件: GTSWrapperHelper.java
/**
 * Extract the encoded data, removing compression if needed
 * 
 * @param wrapper from which to extract the encoded data
 * @return the raw encoded data
 */
private static byte[] unwrapEncoded(GTSWrapper wrapper) {
  
  if (!wrapper.isCompressed()) {
    return wrapper.getEncoded();
  }
      
  byte[] bytes = wrapper.getEncoded();
 
  ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length);

  int pass = wrapper.getCompressionPasses();
  
  while(pass > 0) {
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
    baos.reset();
    
    try {
      GZIPInputStream gzis = new GZIPInputStream(in, 2048);
      
      byte[] buf = new byte[1024];
      
      while(true) {
        int len = gzis.read(buf);
        
        if (len < 0) {
          break;
        }
        
        baos.write(buf, 0, len);
      }
      
      gzis.close();          
    } catch (IOException ioe) {
      throw new RuntimeException("Invalid compressed content.");
    }
    bytes = baos.toByteArray();
    pass--;
  }

  return bytes;
}
 
源代码20 项目: bidder   文件: HttpPostGet.java
/**
 * Send an HTTP Post
 * @param targetURL
 *    String. The URL to transmit to.
 * @param data
 *            String. The payload String.
 * @param connTimeout
 * 			  int. The connection timeout in ms
 * @param readTimeout
 * 			  int. The read timeout in ms.
 * @return String. The contents of the POST return.
 * @throws Exception
 *             on network errors.
 */

public String sendPost(String targetURL, String data, int connTimeout, int readTimeout) throws Exception {
	URLConnection connection = new URL(targetURL).openConnection();
	connection.setRequestProperty("Content-Type", "application/json");
	connection.setDoInput(true);
	connection.setDoOutput(true);
	connection.setConnectTimeout(connTimeout);
	connection.setReadTimeout(readTimeout);
	OutputStream output = connection.getOutputStream();
	
	try {
		output.write(data.getBytes());
	} finally {
		try {
			output.close();
		} catch (IOException logOrIgnore) {
			logOrIgnore.printStackTrace();
		}
	}
	InputStream response = connection.getInputStream();
	
	http = (HttpURLConnection) connection;
	code = http.getResponseCode();
	
	String value = http.getHeaderField("Content-Encoding");
	
	if (value != null && value.equals("gzip")) {
		byte bytes [] = new byte[4096];
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		GZIPInputStream gis = new GZIPInputStream(http.getInputStream());
		while(true) {
			int n = gis.read(bytes);
			if (n < 0) break;
			baos.write(bytes,0,n);
		}
		return new String(baos.toByteArray());
	}
	
		

	byte [] b = getContents(response);
	response.close();
	if (b.length == 0)
		return null;
	return new String(b, 0, b.length);
}
 
源代码评论
动弹
沙发等你来抢