List of usage examples for java.util.zip InflaterInputStream InflaterInputStream
public InflaterInputStream(InputStream in)
From source file:com.ichi2.anki.AnkiDroidProxy.java
public String getDecks() { // Log.i(AnkiDroidApp.TAG, "getDecks - user = " + username + ", password = " + password); String decksServer = "{}"; try {/*from ww w .j a v a 2 s . c o m*/ // FIXME: Client is hardcoded. String data = "p=" + URLEncoder.encode(mPassword, "UTF-8") + "&client=ankidroid-0.4&u=" + URLEncoder.encode(mUsername, "UTF-8") + "&d=None&sources=" + URLEncoder.encode("[]", "UTF-8") + "&libanki=0.9.9.8.6&pversion=5"; // Log.i(AnkiDroidApp.TAG, "Data json = " + data); HttpPost httpPost = new HttpPost(SYNC_URL + "getDecks"); StringEntity entity = new StringEntity(data); httpPost.setEntity(entity); httpPost.setHeader("Accept-Encoding", "identity"); httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpResponse response = httpClient.execute(httpPost); Log.i(AnkiDroidApp.TAG, "Response = " + response.toString()); HttpEntity entityResponse = response.getEntity(); Log.i(AnkiDroidApp.TAG, "Entity's response = " + entityResponse.toString()); InputStream content = entityResponse.getContent(); Log.i(AnkiDroidApp.TAG, "Content = " + content.toString()); decksServer = Utils.convertStreamToString(new InflaterInputStream(content)); Log.i(AnkiDroidApp.TAG, "String content = " + decksServer); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { Log.i(AnkiDroidApp.TAG, "ClientProtocolException = " + e.getMessage()); } catch (IOException e) { Log.i(AnkiDroidApp.TAG, "IOException = " + e.getMessage()); } return decksServer; }
From source file:Fetcher.Fetcher.java
@Deprecated @Override//w ww . j av a2 s. c om /** * run() is deprecated. Use startFetching() instead. */ public void run() { WebDocument link = null; HttpURLConnection connection; Proxy p; //PreConnfiguration //Configure proxy //TODO Anonymizer is deprecated. Use in following for warning generation. switch (Variables.anonymizerProxyType) { case DIRECT: p = new Proxy(Proxy.Type.DIRECT, new InetSocketAddress(Variables.anonymizerIP, Variables.anonymizerPort)); break; case HTTP: p = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(Variables.anonymizerIP, Variables.anonymizerPort)); break; case SOCKS: p = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(Variables.anonymizerIP, Variables.anonymizerPort)); break; case NONE: default: p = null; break; } link = Methods.getNextProfileLink(); while (link != null && isWorking) { //Start fetching ... //Check if it should work or not Date currentTime = Methods.getCurrentTime(); if (!currentTime.after(Variables.startTime) || !currentTime.before(Variables.endTime)) { try { synchronized (t) { getThread().wait(60000); //sleep 60 seconds } } catch (InterruptedException ex) { if (Variables.debug) { Variables.logger.Log(Fetcher.class, Variables.LogType.Error, "Time is not between start and end time and thread is in exception!"); } } finally { continue; } } String URL = link.getNextUrl(); String UA = Methods.getRandomUserAgent(); //Use this UA for refererd or single links. //loop for referer for (int i = 0; i <= link.getRefererCount(); URL = link.getNextUrl(), i++) { if (Variables.debug && Variables.vv) { Variables.logger.Log(Fetcher.class, Variables.LogType.Trace, "Fetcher (" + Methods.Colorize(name, Methods.Color.Green) + ") start getting " + URL); } try { //Anonymizer if (Variables.anonymizerProxyType == Variables.AnonymizerProxy.NONE) { connection = (HttpURLConnection) new URL(URL).openConnection(); } else { connection = (HttpURLConnection) new URL(URL).openConnection(p); } connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("User-Agent", UA); connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); connection.setRequestProperty("Accept-Encoding", "gzip, deflated"); String referer = link.getNextReferrer(); if (referer != null) { connection.setRequestProperty("Referer", referer); referer = null; System.gc(); } //Send Cookie using user input if (!(Variables.Cookie == null || Variables.Cookie.equalsIgnoreCase(""))) { connection.setRequestProperty("Cookie", Variables.Cookie); } else if (cookies.getCookieStore().getCookies().size() > 0) { //From referer, there are some cookies connection.setRequestProperty("Cookie", Join(",", cookies.getCookieStore().getCookies())); } connection.setRequestMethod("GET"); connection.connect(); //Get Cookie from response getCookies(connection); if (connection.getResponseCode() == 200) { //Write to file String outputName = Variables.outputDirectory + link.getOutputName().substring(0, link.getOutputName().lastIndexOf(".")) + i + link.getOutputName().substring(link.getOutputName().lastIndexOf(".")); //Check extension if (!(outputName.endsWith("html") || outputName.endsWith("htm"))) { outputName += "html"; } //get content String html = ""; if (connection.getContentEncoding().equalsIgnoreCase("gzip")) { html = IOUtils.toString(new GZIPInputStream(connection.getInputStream())); } else if (connection.getContentEncoding().equalsIgnoreCase("deflate")) { html = IOUtils.toString(new InflaterInputStream(connection.getInputStream())); } FileWriter fw = new FileWriter(outputName); fw.write(html); fw.flush(); fw.close(); } else { //The returned code is not 200. if (Variables.debug) { Variables.logger.Log(Fetcher.class, Variables.LogType.Error, "Fetcher could not download (" + Methods.Colorize(URL, Methods.Color.Red) + ") in " + name); if (Variables.vv) { Variables.logger.Log(Fetcher.class, Variables.LogType.Error, "Server responded (" + Methods.Colorize(connection.getResponseCode() + " - " + connection.getResponseMessage(), Methods.Color.Red) + ") for " + URL); } } } //Close the connection connection.disconnect(); //Report progress Variables.logger.logResult(connection, link); Methods.oneFinished(); if (Variables.debug && Variables.vv) { Variables.logger.Log(Fetcher.class, Variables.LogType.Info, "[+] Done fetching (" + Methods.Colorize(URL, Methods.Color.Red) + "]"); } try { synchronized (t) { t.wait(Methods.getNextRandom() * 1000); } } catch (InterruptedException ex) { if (Variables.debug) { Variables.logger.Log(Fetcher.class, Variables.LogType.Error, "Cannot interrupt thread [" + Methods.Colorize(name, Methods.Color.Red) + "]. Interrupted before!"); } } catch (IllegalArgumentException ex) { if (Variables.debug) { Variables.logger.Log(Fetcher.class, Variables.LogType.Error, "-1 is returned as random number for thread [" + Methods.Colorize(name, Methods.Color.Red) + "]."); } } } catch (IOException ex) { if (Variables.debug) { if (Variables.vv) { Variables.logger.Log(Fetcher.class, Variables.LogType.Error, "Error in fetching [" + Methods.Colorize(URL, Methods.Color.Red) + "] in fetcher (" + Methods.Colorize(name, Methods.Color.Yellow) + ") for writing in (" + Methods.Colorize(link.getOutputName(), Methods.Color.White) + "). Detail:\r\n" + ex.getMessage()); } else { Variables.logger.Log(Fetcher.class, Variables.LogType.Error, "Error in fetching [" + Methods.Colorize(URL, Methods.Color.Red) + "]"); } } } catch (NullPointerException ex) { //Thrown sometimes and make the thread as Dead! if (Variables.debug) { if (Variables.vv) { Variables.logger.Log(Fetcher.class, Variables.LogType.Error, "Null pointer occured. Error in fetching [" + Methods.Colorize(URL, Methods.Color.Red) + "] in fetcher (" + Methods.Colorize(name, Methods.Color.Yellow) + ") for writing in (" + Methods.Colorize(link.getOutputName(), Methods.Color.White) + ")."); } else { Variables.logger.Log(Fetcher.class, Variables.LogType.Error, "Null pointer occured. Error in fetching [" + Methods.Colorize(URL, Methods.Color.Red) + "]"); } } } } //Check size limit and compress ... long size = Methods.getFolderSize(Variables.outputDirectory); if (size >= Variables.outputSizeLimit) { //Deactivate itself by waiting ... Variables.state = Variables.microbotState.Compressing; Variables.threadController.changeActiveThreads(false, t, Variables.microbotState.Compressing); } //Check if user terminated program or not if (isWorking) { link = Methods.getNextProfileLink(); } } //Thread finished. (Normally or by force) Variables.state = Variables.microbotState.Stopping; Variables.threadController.changeActiveThreads(false, t, Variables.microbotState.Stopping); //URLs done. This thread finishes its work. if (Variables.debug) { Variables.logger.Log(Fetcher.class, Variables.LogType.Info, "Fetcher (" + Methods.Colorize(name, Methods.Color.Green) + ") finished its work."); } }
From source file:com.apporiented.hermesftp.cmd.AbstractFtpCmdStor.java
/** * Creates an input stream that supports reading records. * //from w w w.j a v a2 s. c o m * @param is The nested input stream. * @param mode The transmission mode. * @param charset The encoding or null if binary. * @param restartMarkers Optional map that stores restart markers. * @return The stream object. * @throws UnsupportedEncodingException Thrown if encoding unknown. */ private RecordReadSupport createRecInputStream(InputStream is, int mode, String charset, Map<Long, Long> restartMarkers) throws UnsupportedEncodingException { RecordReadSupport result = null; byte[] eorBytes = charset == null ? new byte[0] : getEorBytes(charset); if (mode == MODE_BLOCK) { result = new BlockModeInputStream(is, eorBytes, restartMarkers); } else if (mode == MODE_STREAM) { result = new RecordInputStream(is, getEorBytes(charset)); } else if (mode == MODE_ZIP) { result = new RecordInputStream(new InflaterInputStream(is), getEorBytes(charset)); } else { log.error("Unsupported record mode: " + mode); } if (charset != null) { result = new TextInputStream((InputStream) result, charset); } return result; }
From source file:org.graylog.plugins.beats.BeatsFrameDecoder.java
/** * @see <a href="https://github.com/logstash-plugins/logstash-input-beats/blob/master/PROTOCOL.md#compressed-frame-type">'compressed' frame type</a> */// w w w. ja va 2 s.c o m private ChannelBuffer[] processCompressedFrame(Channel channel, ChannelBuffer channelBuffer) throws Exception { if (channelBuffer.readableBytes() >= 4) { final long payloadLength = channelBuffer.readUnsignedInt(); if (channelBuffer.readableBytes() < payloadLength) { channelBuffer.resetReaderIndex(); } else { final byte[] data = new byte[(int) payloadLength]; channelBuffer.readBytes(data); try (final ByteArrayInputStream dataStream = new ByteArrayInputStream(data); final InputStream in = new InflaterInputStream(dataStream)) { final ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(ByteStreams.toByteArray(in)); return processCompressedDataFrames(channel, buffer); } } } else { channelBuffer.resetReaderIndex(); } return null; }
From source file:org.apache.logging.log4j.core.layout.GelfLayoutTest.java
private void testCompressedLayout(final CompressionType compressionType, final boolean includeStacktrace, final boolean includeThreadContext, String host, final boolean includeNullDelimiter) throws IOException { for (final Appender appender : root.getAppenders().values()) { root.removeAppender(appender);//from w ww. j a v a2 s . c o m } // set up appenders final GelfLayout layout = GelfLayout.newBuilder().setConfiguration(ctx.getConfiguration()).setHost(host) .setAdditionalFields(new KeyValuePair[] { new KeyValuePair(KEY1, VALUE1), new KeyValuePair(KEY2, "${java:runtime}"), }) .setCompressionType(compressionType).setCompressionThreshold(1024) .setIncludeStacktrace(includeStacktrace).setIncludeThreadContext(includeThreadContext) .setIncludeNullDelimiter(includeNullDelimiter).build(); final ListAppender eventAppender = new ListAppender("Events", null, null, true, false); final ListAppender rawAppender = new ListAppender("Raw", null, layout, true, true); final ListAppender formattedAppender = new ListAppender("Formatted", null, layout, true, false); final EncodingListAppender encodedAppender = new EncodingListAppender("Encoded", null, layout, false, true); eventAppender.start(); rawAppender.start(); formattedAppender.start(); encodedAppender.start(); if (host == null) host = NetUtils.getLocalHostname(); final JavaLookup javaLookup = new JavaLookup(); // set appenders on root and set level to debug root.addAppender(eventAppender); root.addAppender(rawAppender); root.addAppender(formattedAppender); root.addAppender(encodedAppender); root.setLevel(Level.DEBUG); root.debug(LINE1); ThreadContext.put(MDCKEY1, MDCVALUE1); ThreadContext.put(MDCKEY2, MDCVALUE2); root.info(LINE2); final Exception exception = new RuntimeException("some error"); root.error(LINE3, exception); formattedAppender.stop(); final List<LogEvent> events = eventAppender.getEvents(); final List<byte[]> raw = rawAppender.getData(); final List<String> messages = formattedAppender.getMessages(); final List<byte[]> raw2 = encodedAppender.getData(); final String threadName = Thread.currentThread().getName(); //@formatter:off assertJsonEquals("{" + "\"version\": \"1.1\"," + "\"host\": \"" + host + "\"," + "\"timestamp\": " + GelfLayout.formatTimestamp(events.get(0).getTimeMillis()) + "," + "\"level\": 7," + "\"_thread\": \"" + threadName + "\"," + "\"_logger\": \"\"," + "\"short_message\": \"" + LINE1 + "\"," + "\"_" + KEY1 + "\": \"" + VALUE1 + "\"," + "\"_" + KEY2 + "\": \"" + javaLookup.getRuntime() + "\"" + "}", messages.get(0)); assertJsonEquals("{" + "\"version\": \"1.1\"," + "\"host\": \"" + host + "\"," + "\"timestamp\": " + GelfLayout.formatTimestamp(events.get(1).getTimeMillis()) + "," + "\"level\": 6," + "\"_thread\": \"" + threadName + "\"," + "\"_logger\": \"\"," + "\"short_message\": \"" + LINE2 + "\"," + (includeThreadContext ? "\"_" + MDCKEY1 + "\": \"" + MDCVALUE1 + "\"," + "\"_" + MDCKEY2 + "\": \"" + MDCVALUE2 + "\"," : "") + "\"_" + KEY1 + "\": \"" + VALUE1 + "\"," + "\"_" + KEY2 + "\": \"" + javaLookup.getRuntime() + "\"" + "}", messages.get(1)); //@formatter:on final byte[] compressed = raw.get(2); final byte[] compressed2 = raw2.get(2); final ByteArrayInputStream bais = new ByteArrayInputStream(compressed); final ByteArrayInputStream bais2 = new ByteArrayInputStream(compressed2); InputStream inflaterStream; InputStream inflaterStream2; switch (compressionType) { case GZIP: inflaterStream = new GZIPInputStream(bais); inflaterStream2 = new GZIPInputStream(bais2); break; case ZLIB: inflaterStream = new InflaterInputStream(bais); inflaterStream2 = new InflaterInputStream(bais2); break; case OFF: inflaterStream = bais; inflaterStream2 = bais2; break; default: throw new IllegalStateException("Missing test case clause"); } final byte[] uncompressed = IOUtils.toByteArray(inflaterStream); final byte[] uncompressed2 = IOUtils.toByteArray(inflaterStream2); inflaterStream.close(); inflaterStream2.close(); final String uncompressedString = new String(uncompressed, layout.getCharset()); final String uncompressedString2 = new String(uncompressed2, layout.getCharset()); //@formatter:off final String expected = "{" + "\"version\": \"1.1\"," + "\"host\": \"" + host + "\"," + "\"timestamp\": " + GelfLayout.formatTimestamp(events.get(2).getTimeMillis()) + "," + "\"level\": 3," + "\"_thread\": \"" + threadName + "\"," + "\"_logger\": \"\"," + "\"short_message\": \"" + LINE3 + "\"," + "\"full_message\": \"" + String.valueOf(JsonStringEncoder.getInstance() .quoteAsString(includeStacktrace ? GelfLayout.formatThrowable(exception).toString() : exception.toString())) + "\"," + (includeThreadContext ? "\"_" + MDCKEY1 + "\": \"" + MDCVALUE1 + "\"," + "\"_" + MDCKEY2 + "\": \"" + MDCVALUE2 + "\"," : "") + "\"_" + KEY1 + "\": \"" + VALUE1 + "\"," + "\"_" + KEY2 + "\": \"" + javaLookup.getRuntime() + "\"" + "}"; //@formatter:on assertJsonEquals(expected, uncompressedString); assertJsonEquals(expected, uncompressedString2); }
From source file:org.jasig.cas.authentication.principal.GoogleAccountsService.java
private static String zlibDeflate(final byte[] bytes) { final ByteArrayInputStream bais = new ByteArrayInputStream(bytes); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final InflaterInputStream iis = new InflaterInputStream(bais); final byte[] buf = new byte[1024]; try {/*from w w w. j a v a 2 s. c om*/ int count = iis.read(buf); while (count != -1) { baos.write(buf, 0, count); count = iis.read(buf); } return new String(baos.toByteArray()); } catch (final Exception e) { return null; } finally { try { iis.close(); } catch (final Exception e) { // nothing to do } } }
From source file:com.cgxlib.xq.rebind.JsniBundleGenerator.java
/** * Get the content of a javascript source. It supports remote sources hosted in CDN's. *//* ww w . ja v a 2 s . co m*/ private String getContent(TreeLogger logger, String path, String src) throws UnableToCompleteException { HttpURLConnection connection = null; InputStream in = null; try { if (!src.matches("(?i)https?://.*")) { String file = path + "/" + src; logger.log(TreeLogger.INFO, getClass().getSimpleName() + " - importing external javascript: " + file); in = this.getClass().getClassLoader().getResourceAsStream(file); if (in == null) { logger.log(TreeLogger.ERROR, "Unable to read javascript file: " + file); } } else { logger.log(TreeLogger.INFO, getClass().getSimpleName() + " - downloading external javascript: " + src); URL url = new URL(src); connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Accept-Encoding", "gzip, deflate"); connection.setRequestProperty("Host", url.getHost()); connection.setConnectTimeout(3000); connection.setReadTimeout(3000); int status = connection.getResponseCode(); if (status != HttpURLConnection.HTTP_OK) { logger.log(TreeLogger.ERROR, "Server Error: " + status + " " + connection.getResponseMessage()); throw new UnableToCompleteException(); } String encoding = connection.getContentEncoding(); in = connection.getInputStream(); if ("gzip".equalsIgnoreCase(encoding)) { in = new GZIPInputStream(in); } else if ("deflate".equalsIgnoreCase(encoding)) { in = new InflaterInputStream(in); } } return inputStreamToString(in); } catch (IOException e) { logger.log(TreeLogger.ERROR, "Error: " + e.getMessage()); throw new UnableToCompleteException(); } finally { if (connection != null) { connection.disconnect(); } } }
From source file:org.diorite.impl.world.io.anvil.AnvilRegion.java
private NbtInputStream getInputStream(final int length, final byte version) throws IOException { if (version == VERSION_GZIP) { final byte[] data = new byte[length - 1]; this.raf.read(data); return new NbtInputStream(new GZIPInputStream(new ByteArrayInputStream(data))); }//from w w w . j a v a 2 s .c o m if (version == VERSION_DEFLATE) { final byte[] data = new byte[length - 1]; this.raf.read(data); return new NbtInputStream(new InflaterInputStream(new ByteArrayInputStream(data))); } throw new RuntimeException("Unknown version: " + version); }
From source file:sx.blah.discord.api.internal.DiscordWS.java
@Override public void onWebSocketBinary(byte[] payload, int offset, int len) { BufferedReader reader = new BufferedReader( new InputStreamReader(new InflaterInputStream(new ByteArrayInputStream(payload, offset, len)))); onWebSocketText(reader.lines().collect(Collectors.joining())); try {// w w w.j a v a2 s .co m reader.close(); } catch (IOException e) { Discord4J.LOGGER.error(LogMarkers.WEBSOCKET, "Encountered websocket error: ", e); } }
From source file:org.fejoa.library.database.CSRepositoryBuilder.java
static private IChunkAccessor getEncryptionChunkAccessor(final FejoaContext context, final ChunkStore.Transaction transaction, final SymmetricKeyData keyData, final ChunkContainerRef ref) { return new IChunkAccessor() { final ICryptoInterface cryptoInterface = context.getCrypto(); private byte[] getIv(byte[] hashValue) { final int ivSizeBytes = keyData.settings.ivSize / 8; byte[] iv = Arrays.copyOfRange(hashValue, 0, ivSizeBytes); // xor with the base IV for (int i = 0; i < ivSizeBytes; i++) iv[i] = (byte) (keyData.iv[i] ^ iv[i]); return iv; }//from w w w.j a v a 2 s .c o m @Override public DataInputStream getChunk(ChunkPointer hash) throws IOException, CryptoException { byte[] iv = getIv(hash.getIV()); byte[] chunkData = transaction.getChunk(hash.getBoxHash()); if (chunkData == null) throw new IOException("Chunk not found: " + hash.getBoxHash()); InputStream inputStream = new ByteArrayInputStream(chunkData); inputStream = cryptoInterface.decryptSymmetric(inputStream, keyData.key, iv, keyData.settings); if (ref.getBoxHeader().getCompressionType() == BoxHeader.CompressionType.ZLIB_COMPRESSION) inputStream = new InflaterInputStream(inputStream); return new DataInputStream(inputStream); } @Override public PutResult<HashValue> putChunk(byte[] data, HashValue ivHash) throws IOException, CryptoException { ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); OutputStream outputStream = byteOutputStream; outputStream = cryptoInterface.encryptSymmetric(outputStream, keyData.key, getIv(ivHash.getBytes()), keyData.settings); if (ref.getBoxHeader().getCompressionType() == BoxHeader.CompressionType.ZLIB_COMPRESSION) outputStream = new DeflaterOutputStream(outputStream); outputStream.write(data); outputStream.close(); return transaction.put(byteOutputStream.toByteArray()); } @Override public void releaseChunk(HashValue data) { } }; }