下面列出了java.util.zip.GZIPInputStream#close ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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();
}
}
/**
* 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();
}
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);
}
}
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);
}
/**
* 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;
}
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();
}
/**
* 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;
}
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];
}
}
/**
* 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;
}
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;
}
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);
}
}
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;
}
@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);
}
@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;
}
/**
* 数据解压缩
* @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();
}
}
/**
* 数据解压缩
*
* @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();
}
@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"));
}
@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();
}
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 "";
}
}
@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();
}