List of usage examples for java.util.zip GZIPInputStream close
public void close() throws IOException
From source file:org.pentaho.di.trans.steps.cubeinput.CubeInputMeta.java
public void getFields(RowMetaInterface r, String name, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space, Repository repository, IMetaStore metaStore) throws KettleStepException { GZIPInputStream fis = null; DataInputStream dis = null;// w w w . j a va 2s . co m try { InputStream is = KettleVFS.getInputStream(space.environmentSubstitute(filename), space); fis = new GZIPInputStream(is); dis = new DataInputStream(fis); RowMetaInterface add = new RowMeta(dis); for (int i = 0; i < add.size(); i++) { add.getValueMeta(i).setOrigin(name); } r.mergeRowMeta(add); } catch (KettleFileException kfe) { throw new KettleStepException( BaseMessages.getString(PKG, "CubeInputMeta.Exception.UnableToReadMetaData"), kfe); } catch (IOException e) { throw new KettleStepException( BaseMessages.getString(PKG, "CubeInputMeta.Exception.ErrorOpeningOrReadingCubeFile"), e); } finally { try { if (fis != null) { fis.close(); } if (dis != null) { dis.close(); } } catch (IOException ioe) { throw new KettleStepException( BaseMessages.getString(PKG, "CubeInputMeta.Exception.UnableToCloseCubeFile"), ioe); } } }
From source file:it.drwolf.ridire.session.async.Mapper.java
private StringWithEncoding getGuessedEncodingAndSetRawContentFromGZFile(File resourceFile) throws IOException { byte[] buf = new byte[Mapper.BUFLENGTH]; int count = 0; GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(resourceFile)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((count = gzis.read(buf)) != -1) { baos.write(buf, 0, count);//from w ww.j a v a2 s. c om } gzis.close(); baos.close(); CharsetDetector charsetDetector = new CharsetDetector(); byte[] byteArray = baos.toByteArray(); charsetDetector.setText(byteArray); CharsetMatch[] matches = charsetDetector.detectAll(); String encoding = this.allowedCharsets.get(1); for (CharsetMatch cm : matches) { if (this.allowedCharsets.contains(cm.getName())) { encoding = cm.getName(); // System.out.println(encoding); break; } } String rawContent = new String(byteArray, encoding); return new StringWithEncoding(rawContent, encoding); }
From source file:com.qlkh.client.server.proxy.ProxyServlet.java
/** * A highly performant ungzip implementation. Do not refactor this without taking new timings. * See ElementTest in ehcache for timings * * @param gzipped the gzipped content//from w w w. j a va 2 s.c o m * @return an ungzipped byte[] * @throws java.io.IOException when something bad happens */ private byte[] ungzip(final byte[] gzipped) throws IOException { final GZIPInputStream inputStream = new GZIPInputStream(new ByteArrayInputStream(gzipped)); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(gzipped.length); final byte[] buffer = new byte[FOUR_KB]; int bytesRead = 0; while (bytesRead != -1) { bytesRead = inputStream.read(buffer, 0, FOUR_KB); if (bytesRead != -1) { byteArrayOutputStream.write(buffer, 0, bytesRead); } } byte[] ungzipped = byteArrayOutputStream.toByteArray(); inputStream.close(); byteArrayOutputStream.close(); return ungzipped; }
From source file:org.warlock.itk.distributionenvelope.Payload.java
/** * Return decompressed content./*from w w w.j ava2 s.c o m*/ * @param t Base64 encoded string containing the compressed content. * @return * @throws Exception */ private byte[] decompressBody(String t) throws Exception { byte decoded[] = null; Base64 b64 = new Base64(); decoded = b64.decode(t.getBytes("UTF-8")); ByteArrayOutputStream uncomp = new ByteArrayOutputStream(); GZIPInputStream gzIn = new GZIPInputStream(new ByteArrayInputStream(decoded), UNCOMPRESSBUFFERSIZE); byte[] buffer = new byte[UNCOMPRESSBUFFERSIZE]; int l = -1; while ((l = gzIn.read(buffer, 0, UNCOMPRESSBUFFERSIZE)) != -1) { uncomp.write(buffer, 0, l); } gzIn.close(); return uncomp.toByteArray(); }
From source file:it.drwolf.ridire.session.async.Mapper.java
@SuppressWarnings("unchecked") private StringWithEncoding transformPDF2HTML(File resourceFile, EntityManager entityManager) throws IOException, InterruptedException { String workingDirName = System.getProperty("java.io.tmpdir"); String userDir = System.getProperty("user.dir"); byte[] buf = new byte[Mapper.BUFLENGTH]; int count = 0; GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(resourceFile)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((count = gzis.read(buf)) != -1) { baos.write(buf, 0, count);//from w w w .j a v a 2 s. co m } gzis.close(); baos.close(); byte[] byteArray = baos.toByteArray(); String uuid = UUID.randomUUID().toString(); String pdfFileName = uuid + ".pdf"; String htmlFileName = uuid + ".html"; File tmpDir = new File(workingDirName); String htmlFileNameCompletePath = workingDirName + JobMapperMonitor.FILE_SEPARATOR + htmlFileName; File fileToConvert = new File(tmpDir, pdfFileName); FileUtils.writeByteArrayToFile(fileToConvert, byteArray); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); CommandParameter cp = entityManager.find(CommandParameter.class, CommandParameter.PDFTOHTML_EXECUTABLE_KEY); CommandLine commandLine = CommandLine.parse(cp.getCommandValue()); commandLine.addArgument("-c"); commandLine.addArgument("-i"); commandLine.addArgument(fileToConvert.getAbsolutePath()); commandLine.addArgument(htmlFileNameCompletePath); executor.setExitValue(0); executor.execute(commandLine); try { FileUtils.moveFileToDirectory( new File(userDir + JobMapperMonitor.FILE_SEPARATOR + uuid + "-outline.html"), tmpDir, false); } catch (IOException e) { } cp = entityManager.find(CommandParameter.class, CommandParameter.PDFCLEANER_EXECUTABLE_KEY); commandLine = CommandLine .parse("java -Xmx128m -jar -Djava.io.tmpdir=" + this.tempDir + " " + cp.getCommandValue()); commandLine.addArgument(htmlFileNameCompletePath); commandLine.addArgument("39"); commandLine.addArgument("6"); commandLine.addArgument("5"); executor = new DefaultExecutor(); executor.setExitValue(0); ExecuteWatchdog watchdog = new ExecuteWatchdog(Mapper.PDFCLEANER_TIMEOUT); executor.setWatchdog(watchdog); ByteArrayOutputStream baosStdOut = new ByteArrayOutputStream(1024); ExecuteStreamHandler executeStreamHandler = new PumpStreamHandler(baosStdOut, null, null); executor.setStreamHandler(executeStreamHandler); int exitValue = executor.execute(commandLine); String htmlString = null; if (exitValue == 0) { htmlString = baosStdOut.toString(); } FileUtils.deleteQuietly(new File(htmlFileNameCompletePath)); PrefixFileFilter pff = new PrefixFileFilter(uuid); for (File f : FileUtils.listFiles(tmpDir, pff, null)) { FileUtils.deleteQuietly(f); } if (htmlString != null) { htmlString = htmlString.replaceAll(" ", " "); htmlString = htmlString.replaceAll("<br.*?>", " "); CharsetDetector charsetDetector = new CharsetDetector(); charsetDetector.setText(htmlString.getBytes()); String encoding = charsetDetector.detect().getName(); return new StringWithEncoding(htmlString, encoding); } return null; }
From source file:es.ehu.si.ixa.qwn.ppv.CLI.java
public final void compileGraph() throws IOException { String kb = parsedArguments.getString("kb"); String ukb = parsedArguments.getString("ukbPath"); // normal case: single graph is created if (!kb.equals("all")) { ukb_compile(ukb, kb);// w ww .ja v a 2 s . c o m System.out.println("new graph generated: " + kb + ".bin"); } // compile all graphs in the distribution else { System.out.println("Default behavior: all the graphs in the qwn-ppv distribution will be compiled"); Properties AvailableGraphs = new Properties(); try { AvailableGraphs .load(Thread.currentThread().getContextClassLoader().getResourceAsStream("graphs.txt")); } catch (IOException ioe) { ioe.printStackTrace(); } for (String key : AvailableGraphs.stringPropertyNames()) { String jarlocation = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); String location = jarlocation.substring(0, jarlocation.lastIndexOf("/")); String destKBPath = location + File.separator + "graphs" + File.separator + (String) AvailableGraphs.get(key); try { GZIPInputStream kbExtract = new GZIPInputStream(this.getClass().getClassLoader() .getResourceAsStream("graphs/" + (String) AvailableGraphs.get(key) + ".txt.gz")); File KBPath = new File(location + File.separator + "graphs"); KBPath.mkdirs(); OutputStream kbDestination = new FileOutputStream(destKBPath + ".txt"); IOUtils.copy(kbExtract, kbDestination); kbExtract.close(); kbDestination.close(); } catch (IOException ioe) { ioe.printStackTrace(); } ukb_compile(ukb, destKBPath + ".txt"); } } }
From source file:com.gelakinetic.mtgfam.helpers.CardDbAdapter.java
public static void copyDB(Context ctx) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ctx); SharedPreferences.Editor editor = preferences.edit(); try {//from w w w. ja v a2 s.c o m File folder = new File(DB_PATH); if (!folder.exists()) { folder.mkdir(); } File db = new File(folder, DB_NAME); if (db.exists()) { db.delete(); editor.putString("lastUpdate", ""); editor.putInt("databaseVersion", -1); editor.commit(); } if (!db.exists()) { GZIPInputStream gis = new GZIPInputStream(ctx.getResources().openRawResource(R.raw.db)); FileOutputStream fos = new FileOutputStream(db); byte[] buffer = new byte[1024]; int length; while ((length = gis.read(buffer)) > 0) { fos.write(buffer, 0, length); } editor.putInt("databaseVersion", CardDbAdapter.DATABASE_VERSION); editor.commit(); // Close the streams fos.flush(); fos.close(); gis.close(); } } catch (NotFoundException e) { } catch (IOException e) { } catch (Exception e) { } }
From source file:com.seajas.search.contender.service.modifier.FeedModifierService.java
/** * Retrieve the content of a result feed URL. * //from ww w. j a va 2 s.c o m * @param uri * @param encodingOverride * @param userAgent * @param resultHeaders * @return Reader */ private Reader getContent(final URI uri, final String encodingOverride, final String userAgent, final Map<String, String> resultHeaders) { Reader result = null; String contentType = null; // Retrieve the feed try { InputStream inputStream = null; if (uri.getScheme().equalsIgnoreCase("ftp") || uri.getScheme().equalsIgnoreCase("ftps")) { FTPClient ftpClient = uri.getScheme().equalsIgnoreCase("ftps") ? new FTPSClient() : new FTPClient(); try { ftpClient.connect(uri.getHost(), uri.getPort() != -1 ? uri.getPort() : 21); if (StringUtils.hasText(uri.getUserInfo())) { if (uri.getUserInfo().contains(":")) ftpClient.login(uri.getUserInfo().substring(0, uri.getUserInfo().indexOf(":")), uri.getUserInfo().substring(uri.getUserInfo().indexOf(":") + 1)); else ftpClient.login(uri.getUserInfo(), ""); inputStream = ftpClient.retrieveFileStream(uri.getPath()); } } finally { ftpClient.disconnect(); } } else if (uri.getScheme().equalsIgnoreCase("file")) { File file = new File(uri); if (!file.isDirectory()) inputStream = new FileInputStream(uri.getPath()); else inputStream = RSSDirectoryBuilder.build(file); } else if (uri.getScheme().equalsIgnoreCase("http") || uri.getScheme().equalsIgnoreCase("https")) { try { HttpGet method = new HttpGet(uri.toString()); if (resultHeaders != null) for (Entry<String, String> resultHeader : resultHeaders.entrySet()) method.setHeader(new BasicHeader(resultHeader.getKey(), resultHeader.getValue())); if (userAgent != null) method.setHeader(CoreProtocolPNames.USER_AGENT, userAgent); SizeRestrictedHttpResponse response = httpClient.execute(method, new SizeRestrictedResponseHandler(maximumContentLength, uri)); try { if (response != null) { inputStream = new ByteArrayInputStream(response.getResponse()); contentType = response.getContentType() != null ? response.getContentType().getValue() : null; } else return null; } catch (RuntimeException e) { method.abort(); throw e; } } catch (IllegalArgumentException e) { logger.error("Invalid URL " + uri.toString() + " - not returning content", e); return null; } } else { logger.error("Unknown protocol " + uri.getScheme() + ". Skipping feed."); return null; } // Guess the character encoding using ROME's reader, then buffer it so we can discard the input stream (and close the connection) InputStream readerInputStream = new BufferedInputStream(inputStream); MediaType mediaType = autoDetectParser.getDetector().detect(readerInputStream, new Metadata()); try { Reader reader = null; if (mediaType.getType().equals("application")) { if (mediaType.getSubtype().equals("x-gzip")) { GZIPInputStream gzipInputStream = new GZIPInputStream(readerInputStream); if (encodingOverride != null) reader = readerToBuffer(new StringBuffer(), new InputStreamReader(gzipInputStream, encodingOverride), false); else reader = readerToBuffer(new StringBuffer(), contentType != null ? new XmlHtmlReader(gzipInputStream, contentType, true) : new XmlReader(gzipInputStream, true), false); gzipInputStream.close(); } else if (mediaType.getSubtype().equals("zip")) { ZipFile zipFile = null; // ZipInputStream can't do read-aheads, so we have to use a temporary on-disk file instead File temporaryFile = File.createTempFile("profiler-", ".zip"); try { FileOutputStream zipOutputStream = new FileOutputStream(temporaryFile); IOUtils.copy(readerInputStream, zipOutputStream); readerInputStream.close(); zipOutputStream.flush(); zipOutputStream.close(); // Create a new entry and process it zipFile = new ZipFile(temporaryFile); Enumeration<? extends ZipEntry> zipEnumeration = zipFile.entries(); ZipEntry zipEntry = zipEnumeration.nextElement(); if (zipEntry == null || zipEntry.isDirectory() || zipEnumeration.hasMoreElements()) { logger.error( "ZIP files are currently expected to contain one and only one entry, which is to be a file"); return null; } // We currently only perform prolog stripping for ZIP files InputStream zipInputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry)); if (encodingOverride != null) reader = readerToBuffer(new StringBuffer(), new InputStreamReader( new BufferedInputStream(zipInputStream), encodingOverride), true); else result = readerToBuffer(new StringBuffer(), contentType != null ? new XmlHtmlReader(new BufferedInputStream(zipInputStream), contentType, true) : new XmlReader(new BufferedInputStream(zipInputStream), true), true); } catch (Exception e) { logger.error("An error occurred during ZIP file processing", e); return null; } finally { if (zipFile != null) zipFile.close(); if (!temporaryFile.delete()) logger.error("Unable to delete temporary file"); } } } if (result == null) { if (encodingOverride != null) result = readerToBuffer(new StringBuffer(), reader != null ? reader : new InputStreamReader(readerInputStream, encodingOverride), false); else result = readerToBuffer(new StringBuffer(), reader != null ? reader : contentType != null ? new XmlHtmlReader(readerInputStream, contentType, true) : new XmlReader(readerInputStream, true), false); } } catch (Exception e) { logger.error("An error occurred during stream processing", e); return null; } finally { inputStream.close(); } } catch (IOException e) { logger.error("Could not retrieve the given feed: " + e.getMessage(), e); return null; } return result; }
From source file:Base64.java
/** * Decodes data from Base64 notation, automatically * detecting gzip-compressed data and decompressing it. * * @param s the string to decode//w w w . j a v a 2 s . c o m * @return the decoded data * @since 1.4 */ public static byte[] decode(String s) { byte[] bytes; try { bytes = s.getBytes(PREFERRED_ENCODING); } // end try catch (java.io.UnsupportedEncodingException uee) { bytes = s.getBytes(); } // end catch //</change> // Decode bytes = decode(bytes, 0, bytes.length); // Check to see if it's gzip-compressed // GZIP Magic Two-Byte Number: 0x8b1f (35615) if (bytes != null && bytes.length >= 4) { int head = ((int) bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00); if (java.util.zip.GZIPInputStream.GZIP_MAGIC == head) { java.io.ByteArrayInputStream bais = null; java.util.zip.GZIPInputStream gzis = null; java.io.ByteArrayOutputStream baos = null; byte[] buffer = new byte[2048]; int length = 0; try { baos = new java.io.ByteArrayOutputStream(); bais = new java.io.ByteArrayInputStream(bytes); gzis = new java.util.zip.GZIPInputStream(bais); while ((length = gzis.read(buffer)) >= 0) { baos.write(buffer, 0, length); } // end while: reading input // No error? Get new bytes. bytes = baos.toByteArray(); } // end try catch (java.io.IOException e) { // Just return originally-decoded bytes } // end catch finally { try { baos.close(); } catch (Exception e) { } try { gzis.close(); } catch (Exception e) { } try { bais.close(); } catch (Exception e) { } } // end finally } // end if: gzipped } // end if: bytes.length >= 2 return bytes; }
From source file:Base64.java
/** * Decodes data from Base64 notation, automatically * detecting gzip-compressed data and decompressing it. * * @param s the string to decode/*from w ww . j a v a 2 s . co m*/ * @return the decoded data * @since 1.4 */ public static byte[] decode(String s) { byte[] bytes; try { bytes = s.getBytes(PREFERRED_ENCODING); } // end try catch (java.io.UnsupportedEncodingException uee) { bytes = s.getBytes(); } // end catch //</change> // Decode bytes = decode(bytes, 0, bytes.length); // Check to see if it's gzip-compressed // GZIP Magic Two-Byte Number: 0x8b1f (35615) if (bytes != null && bytes.length >= 4) { int head = (bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00); if (java.util.zip.GZIPInputStream.GZIP_MAGIC == head) { java.io.ByteArrayInputStream bais = null; java.util.zip.GZIPInputStream gzis = null; java.io.ByteArrayOutputStream baos = null; byte[] buffer = new byte[2048]; int length = 0; try { baos = new java.io.ByteArrayOutputStream(); bais = new java.io.ByteArrayInputStream(bytes); gzis = new java.util.zip.GZIPInputStream(bais); while ((length = gzis.read(buffer)) >= 0) { baos.write(buffer, 0, length); } // end while: reading input // No error? Get new bytes. bytes = baos.toByteArray(); } // end try catch (java.io.IOException e) { // Just return originally-decoded bytes } // end catch finally { try { baos.close(); } catch (Exception e) { } try { gzis.close(); } catch (Exception e) { } try { bais.close(); } catch (Exception e) { } } // end finally } // end if: gzipped } // end if: bytes.length >= 2 return bytes; }