下面列出了java.nio.channels.OverlappingFileLockException#java.util.zip.GZIPInputStream 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 主方法
*/
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)) {
}
}
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();
}
}
@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);
}
/**
* @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);
}
}
@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);
}
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;
}
}
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);
}
}
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);
}
@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");
}
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;
}
}
/**
* 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();
}
/**
* 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;
}
}
}
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;
}
@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));
}
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();
}
}
@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);
}
/**
* 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;
}
@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)));
}
/**
* 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);
}
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();
}
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;
}
@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;
}
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);
}
}
@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();
}
@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)));
}
/**
* @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();
}
/**
* 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();
}
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 + "'");
}
}
}
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);
}
}
@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;
}