List of usage examples for java.util.zip GZIPInputStream read
public int read(byte b[]) throws IOException
b.length
bytes of data from this input stream into an array of bytes. From source file:org.asynchttpclient.request.body.multipart.MultipartUploadTest.java
/** * Test that the files were sent, based on the response from the servlet * // w w w.java2 s . c o m * @param expectedContents * @param sourceFiles * @param r * @param deflate */ private void testSentFile(List<String> expectedContents, List<File> sourceFiles, Response r, List<Boolean> deflate) { String content = r.getResponseBody(); assertNotNull("===>" + content); logger.debug(content); String[] contentArray = content.split("\\|\\|"); // TODO: this fail on win32 assertEquals(contentArray.length, 2); String tmpFiles = contentArray[1]; assertNotNull(tmpFiles); assertTrue(tmpFiles.trim().length() > 2); tmpFiles = tmpFiles.substring(1, tmpFiles.length() - 1); String[] responseFiles = tmpFiles.split(","); assertNotNull(responseFiles); assertEquals(responseFiles.length, sourceFiles.size()); logger.debug(Arrays.toString(responseFiles)); int i = 0; for (File sourceFile : sourceFiles) { File tmp = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] sourceBytes = null; try (FileInputStream instream = new FileInputStream(sourceFile)) { byte[] buf = new byte[8092]; int len = 0; while ((len = instream.read(buf)) > 0) { baos.write(buf, 0, len); } logger.debug("================"); logger.debug("Length of file: " + baos.toByteArray().length); logger.debug("Contents: " + Arrays.toString(baos.toByteArray())); logger.debug("================"); System.out.flush(); sourceBytes = baos.toByteArray(); } tmp = new File(responseFiles[i].trim()); logger.debug("=============================="); logger.debug(tmp.getAbsolutePath()); logger.debug("=============================="); System.out.flush(); assertTrue(tmp.exists()); byte[] bytes; try (FileInputStream instream = new FileInputStream(tmp)) { ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); byte[] buf = new byte[8092]; int len = 0; while ((len = instream.read(buf)) > 0) { baos2.write(buf, 0, len); } bytes = baos2.toByteArray(); assertEquals(bytes, sourceBytes); } if (!deflate.get(i)) { String helloString = new String(bytes); assertEquals(helloString, expectedContents.get(i)); } else { try (FileInputStream instream = new FileInputStream(tmp)) { ByteArrayOutputStream baos3 = new ByteArrayOutputStream(); GZIPInputStream deflater = new GZIPInputStream(instream); try { byte[] buf3 = new byte[8092]; int len3 = 0; while ((len3 = deflater.read(buf3)) > 0) { baos3.write(buf3, 0, len3); } } finally { deflater.close(); } String helloString = new String(baos3.toByteArray()); assertEquals(expectedContents.get(i), helloString); } } } catch (Exception e) { e.printStackTrace(); fail("Download Exception"); } finally { if (tmp != null) FileUtils.deleteQuietly(tmp); i++; } } }
From source file:net.sf.ehcache.constructs.web.PageInfoTest.java
private byte[] ungzip3(final byte[] gzip) throws IOException { int size = 0; int counter = 0; GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(gzip)); int bytesRead = 0; byte[] buffer = new byte[500000]; byte[] tempBuffer = new byte[4096]; counter = 0;/*from ww w. j a va 2s .c om*/ while (bytesRead != -1) { bytesRead = gzipInputStream.read(tempBuffer); if (bytesRead != -1) { System.arraycopy(tempBuffer, 0, buffer, counter, bytesRead); counter += bytesRead; } } gzipInputStream.close(); size = counter; byte[] unzipped = new byte[size]; System.arraycopy(buffer, 0, unzipped, 0, counter); return unzipped; }
From source file:org.nuxeo.client.internals.util.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 .com*/ * @return the decoded data * @since 1.4 */ public static byte[] decode(String s) { byte[] bytes; try { bytes = s.getBytes(PREFERRED_ENCODING); } // end try catch (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 { IOUtils.closeQuietly(baos); IOUtils.closeQuietly(gzis); IOUtils.closeQuietly(bais); } // end finally } // end if: gzipped } // end if: bytes.length >= 2 return bytes; }
From source file:org.ecoinformatics.seek.datasource.EcogridGZippedDataCacheItem.java
/** * This method overwrite the super class - EcogridCompressedDataCacheItem. * It specifys the gunzip method to uncompress the file. The new ungzip file * will be the the cacheitem file name without the file extension. An file * location will be the cachedata/unzip/cacheitemname/ If this is tar file * too, we will untar it after ungzip it. * /*from w ww . j a v a 2 s. co m*/ * @throws Exception */ public void unCompressCacheItem() throws Exception { if (unCompressedFilePath != null) { log.debug("At unCompressCacheItem method in Zip ojbect"); // read the gzip file and ungzip it GZIPInputStream gZipFileReader = new GZIPInputStream(new FileInputStream(getFile())); String unGZipFileName = removeFileExtension(getAbsoluteFileName()); String unGZipFilePath = unCompressedFilePath + File.separator + unGZipFileName; log.debug("The unGzip aboslute file path is " + unGZipFilePath); File unGzipFile = new File(unGZipFilePath); FileOutputStream fileWriter = new FileOutputStream(unGzipFile); byte[] array = new byte[3000 * 1024]; int len; while ((len = gZipFileReader.read(array)) >= 0) { fileWriter.write(array, 0, len); } gZipFileReader.close(); fileWriter.close(); // if this is a tar file too, will untar it. if (getIsTarFile()) { if (unCompressedFilePath != null) { log.debug("untar the file after ungzip"); EcogridTarArchivedDataCacheItem.extractTarFile(unGZipFilePath, unCompressedCacheItemDir); } } unCompressedFileList = unCompressedCacheItemDir.list(); } }
From source file:net.sf.ehcache.constructs.web.PageInfoTest.java
private byte[] ungzip5(final byte[] gzip) throws IOException { int size = 0; int counter = 0; GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(gzip)); int bytesRead = 0; byte[] buffer = new byte[32768]; byte[] tempBuffer = new byte[4096]; counter = 0;/*from ww w . j av a 2 s . c o m*/ while (bytesRead != -1) { bytesRead = gzipInputStream.read(tempBuffer); if (bytesRead != -1) { if (buffer.length < counter + bytesRead) { byte[] newBuffer = new byte[buffer.length + 32768]; System.arraycopy(buffer, 0, newBuffer, 0, counter); buffer = newBuffer; } System.arraycopy(tempBuffer, 0, buffer, counter, bytesRead); counter += bytesRead; } } gzipInputStream.close(); size = counter; byte[] unzipped = new byte[size]; System.arraycopy(buffer, 0, unzipped, 0, counter); return unzipped; }
From source file:org.nuxeo.ecm.platform.forms.layout.io.Base64.java
/** * Decodes data from Base64 notation, automatically detecting gzip-compressed data and decompressing it. * * @param s the string to decode/*from w ww. jav a 2 s . c om*/ * @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 { IOUtils.closeQuietly(baos); IOUtils.closeQuietly(gzis); IOUtils.closeQuietly(bais); } // end finally } // end if: gzipped } // end if: bytes.length >= 2 return bytes; }
From source file:org.mule.util.Base64.java
/** * Decodes data from Base64 notation, automatically detecting gzip-compressed * data and decompressing it./*from w ww . ja v a 2s .c o m*/ * * @param s the string to decode * @return the decoded data * @since 1.4 */ public static byte[] decode(String s) { byte[] bytes; try { bytes = s.getBytes(PREFERRED_ENCODING); } // end try catch (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 (GZIPInputStream.GZIP_MAGIC == head) { ByteArrayInputStream bais = null; GZIPInputStream gzis = null; ByteArrayOutputStream baos = null; byte[] buffer = new byte[4096]; int length = 0; try { baos = new ByteArrayOutputStream(4096); bais = new ByteArrayInputStream(bytes); gzis = new 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 (IOException e) { // Just return originally-decoded bytes } // end catch finally { IOUtils.closeQuietly(baos); IOUtils.closeQuietly(gzis); IOUtils.closeQuietly(bais); } // end finally } // end if: gzipped } // end if: bytes.length >= 2 return bytes; }
From source file:org.nuxeo.ecm.automation.client.jaxrs.util.Base64.java
/** * Decodes data from Base64 notation, automatically detecting gzip-compressed data and decompressing it. * * @param s the string to decode// w ww . j ava 2 s . c om * @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 { IOUtils.closeQuietly(baos); IOUtils.closeQuietly(gzis); IOUtils.closeQuietly(bais); } // end finally } // end if: gzipped } // end if: bytes.length >= 2 return bytes; }
From source file:org.asynchttpclient.async.MultipartUploadTest.java
/** * Test that the files were sent, based on the response from the servlet * /*from w w w . j a v a 2s. c o m*/ * @param expectedContents * @param sourceFiles * @param r * @param deflate */ private void testSentFile(List<String> expectedContents, List<File> sourceFiles, Response r, List<Boolean> deflate) { String content = null; try { content = r.getResponseBody(); assertNotNull("===>" + content); logger.debug(content); } catch (IOException e) { fail("Unable to obtain content"); } String[] contentArray = content.split("\\|\\|"); // TODO: this fail on win32 assertEquals(contentArray.length, 2); String tmpFiles = contentArray[1]; assertNotNull(tmpFiles); assertTrue(tmpFiles.trim().length() > 2); tmpFiles = tmpFiles.substring(1, tmpFiles.length() - 1); String[] responseFiles = tmpFiles.split(","); assertNotNull(responseFiles); assertEquals(responseFiles.length, sourceFiles.size()); logger.debug(Arrays.toString(responseFiles)); int i = 0; for (File sourceFile : sourceFiles) { FileInputStream instream = null; File tmp = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] sourceBytes = null; try { instream = new FileInputStream(sourceFile); byte[] buf = new byte[8092]; int len = 0; while ((len = instream.read(buf)) > 0) { baos.write(buf, 0, len); } logger.debug("================"); logger.debug("Length of file: " + baos.toByteArray().length); logger.debug("Contents: " + Arrays.toString(baos.toByteArray())); logger.debug("================"); System.out.flush(); sourceBytes = baos.toByteArray(); } finally { IOUtils.closeQuietly(instream); } tmp = new File(responseFiles[i].trim()); logger.debug("=============================="); logger.debug(tmp.getAbsolutePath()); logger.debug("=============================="); System.out.flush(); assertTrue(tmp.exists()); instream = new FileInputStream(tmp); ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); byte[] buf = new byte[8092]; int len = 0; while ((len = instream.read(buf)) > 0) { baos2.write(buf, 0, len); } IOUtils.closeQuietly(instream); assertEquals(baos2.toByteArray(), sourceBytes); if (!deflate.get(i)) { String helloString = new String(baos2.toByteArray()); assertEquals(helloString, expectedContents.get(i)); } else { instream = new FileInputStream(tmp); ByteArrayOutputStream baos3 = new ByteArrayOutputStream(); GZIPInputStream deflater = new GZIPInputStream(instream); try { byte[] buf3 = new byte[8092]; int len3 = 0; while ((len3 = deflater.read(buf3)) > 0) { baos3.write(buf3, 0, len3); } } finally { deflater.close(); } String helloString = new String(baos3.toByteArray()); assertEquals(expectedContents.get(i), helloString); } } catch (Exception e) { e.printStackTrace(); fail("Download Exception"); } finally { if (tmp != null) FileUtils.deleteQuietly(tmp); IOUtils.closeQuietly(instream); i++; } } }
From source file:net.fabricmc.loom.task.DownloadTask.java
@TaskAction public void download() { try {//from w w w.j a va 2 s. co m LoomGradleExtension extension = this.getProject().getExtensions().getByType(LoomGradleExtension.class); downloadMcJson(extension, getLogger()); Gson gson = new Gson(); Version version = gson.fromJson(new FileReader(Constants.MINECRAFT_JSON.get(extension)), Version.class); if (!Constants.MINECRAFT_CLIENT_JAR.get(extension).exists() || !Checksum .equals(Constants.MINECRAFT_CLIENT_JAR.get(extension), version.downloads.get("client").sha1)) { this.getLogger().lifecycle(":downloading client"); FileUtils.copyURLToFile(new URL(version.downloads.get("client").url), Constants.MINECRAFT_CLIENT_JAR.get(extension)); } if (!Constants.MINECRAFT_SERVER_JAR.get(extension).exists() || !Checksum .equals(Constants.MINECRAFT_SERVER_JAR.get(extension), version.downloads.get("server").sha1)) { this.getLogger().lifecycle(":downloading server"); FileUtils.copyURLToFile(new URL(version.downloads.get("server").url), Constants.MINECRAFT_SERVER_JAR.get(extension)); } if (!Constants.POMF_DIR.get(extension).exists()) { Constants.POMF_DIR.get(extension).mkdir(); } if (!Constants.MAPPINGS_ZIP.get(extension).exists() && extension.hasPomf()) { this.getLogger().lifecycle(":downloading mappings"); try { FileUtils.copyURLToFile( new URL("http://modmuss50.me:8080/job/FabricMC/job/pomf/job/" + extension.version + "/" + extension.pomfVersion + "/artifact/build/libs/pomf-enigma-" + extension.version + "." + extension.pomfVersion + ".zip"), Constants.MAPPINGS_ZIP.get(extension)); } catch (Exception e) { throw new RuntimeException("Failed to download mappings", e); } } if (!extension.hasPomf()) { if (Constants.MAPPINGS_DIR_LOCAL.get(extension).exists()) { if (Constants.MAPPINGS_TINY_GZ_LOCAL.get(extension).exists() && Constants.MAPPINGS_ZIP_LOCAL.get(extension).exists()) { this.getLogger().lifecycle(":using local mappings!"); extension.localMappings = true; //We delete this to make sure they are always re extracted. deleteIfExists(Constants.MAPPINGS_ZIP.get(extension)); deleteIfExists(Constants.MAPPINGS_TINY_GZ.get(extension)); deleteIfExists(Constants.MAPPINGS_TINY.get(extension)); deleteIfExists(Constants.MAPPINGS_DIR.get(extension)); Constants.MAPPINGS_TINY_GZ = Constants.MAPPINGS_TINY_GZ_LOCAL; Constants.MAPPINGS_ZIP = Constants.MAPPINGS_ZIP_LOCAL; } } } if (!Constants.MAPPINGS_TINY.get(extension).exists() && extension.hasPomf()) { if (!Constants.MAPPINGS_TINY_GZ.get(extension).exists() && !extension.localMappings) { getLogger().lifecycle(":downloading tiny mappings"); try { FileUtils.copyURLToFile( new URL("http://modmuss50.me:8080/job/FabricMC/job/pomf/job/" + extension.version + "/" + extension.pomfVersion + "/artifact/build/libs/pomf-tiny-" + extension.version + "." + extension.pomfVersion + ".gz"), Constants.MAPPINGS_TINY_GZ.get(extension)); } catch (Exception e) { throw new RuntimeException("Failed to download mappings", e); } } GZIPInputStream gzipInputStream = new GZIPInputStream( new FileInputStream(Constants.MAPPINGS_TINY_GZ.get(extension))); FileOutputStream fileOutputStream = new FileOutputStream(Constants.MAPPINGS_TINY.get(extension)); int length; byte[] buffer = new byte[1024]; while ((length = gzipInputStream.read(buffer)) > 0) { fileOutputStream.write(buffer, 0, length); } gzipInputStream.close(); fileOutputStream.close(); } DependencyHandler dependencyHandler = getProject().getDependencies(); if (getProject().getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES) .getState() == Configuration.State.UNRESOLVED) { for (Version.Library library : version.libraries) { if (library.allowed() && library.getFile(extension) != null) { // By default, they are all available on all sides String configName = Constants.CONFIG_MC_DEPENDENCIES; if (library.name.contains("java3d") || library.name.contains("paulscode") || library.name.contains("lwjgl") || library.name.contains("twitch") || library.name.contains("jinput") || library.name.contains("text2speech") || library.name.contains("objc")) { configName = Constants.CONFIG_MC_DEPENDENCIES_CLIENT; } dependencyHandler.add(configName, library.getArtifactName()); } } } if (getProject().getConfigurations().getByName(Constants.CONFIG_NATIVES) .getState() == Configuration.State.UNRESOLVED) { version.libraries.stream().filter(lib -> lib.natives != null) .forEach(lib -> dependencyHandler.add(Constants.CONFIG_NATIVES, lib.getArtifactName())); } // Force add LaunchWrapper dependencyHandler.add(Constants.CONFIG_MC_DEPENDENCIES, "net.minecraft:launchwrapper:1.12"); Version.AssetIndex assetIndex = version.assetIndex; File assets = new File(extension.getFabricUserCache(), "assets-" + extension.version); if (!assets.exists()) { assets.mkdirs(); } File assetsInfo = new File(assets, "indexes" + File.separator + assetIndex.id + ".json"); if (!assetsInfo.exists() || !Checksum.equals(assetsInfo, assetIndex.sha1)) { this.getLogger().lifecycle(":downloading asset index"); FileUtils.copyURLToFile(new URL(assetIndex.url), assetsInfo); } ProgressLogger progressLogger = ProgressLogger.getProgressFactory(getProject(), getClass().getName()); progressLogger.start("Downloading assets...", "assets"); AssetIndex index = new Gson().fromJson(new FileReader(assetsInfo), AssetIndex.class); Map<String, AssetObject> parent = index.getFileMap(); final int totalSize = parent.size(); int position = 0; this.getLogger().lifecycle(":downloading assets..."); for (Map.Entry<String, AssetObject> entry : parent.entrySet()) { AssetObject object = entry.getValue(); String sha1 = object.getHash(); File file = new File(assets, "objects" + File.separator + sha1.substring(0, 2) + File.separator + sha1); if (!file.exists() || !Checksum.equals(file, sha1)) { this.getLogger().debug(":downloading asset " + entry.getKey()); FileUtils.copyURLToFile(new URL(Constants.RESOURCES_BASE + sha1.substring(0, 2) + "/" + sha1), file); } String assetName = entry.getKey(); int end = assetName.lastIndexOf("/") + 1; if (end > 0) { assetName = assetName.substring(end, assetName.length()); } progressLogger.progress(assetName + " - " + position + "/" + totalSize + " (" + (int) ((position / (double) totalSize) * 100) + "%) assets downloaded"); position++; } progressLogger.completed(); } catch (IOException e) { e.printStackTrace(); } }