private void readChunkUnzip(Inflater inflater, byte[] buffer, int offset, int length) throws IOException { try { do { int read = inflater.inflate(buffer, offset, length); if(read <= 0) { if(inflater.finished()) { throw new EOFException(); } if(inflater.needsInput()) { refillInflater(inflater); } else { throw new IOException("Can't inflate " + length + " bytes"); } } else { offset += read; length -= read; } } while(length > 0); } catch (DataFormatException ex) { throw (IOException)(new IOException("inflate error").initCause(ex)); } }
public int uncompress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int maxOutputLength) throws IOException { Inflater inf = inflater.get(); inf.reset(); inf.setInput(input, inputOffset, inputLength); if (inf.needsInput()) return 0; // We assume output is big enough try { return inf.inflate(output, outputOffset, maxOutputLength); } catch (DataFormatException e) { throw new IOException(e); } }
public static byte[] uncompress(final byte[] input) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Inflater decompressor = new Inflater(); try { decompressor.setInput(input); final byte[] buf = new byte[2048]; while (!decompressor.finished()) { int count = 0; try { count = decompressor.inflate(buf); } catch (DataFormatException e) { e.printStackTrace(); } bos.write(buf, 0, count); } } finally { decompressor.end(); } return bos.toByteArray(); }
/** * 解压缩. * * @param inputByte * byte[]数组类型的数据 * @return 解压缩后的数据 * @throws IOException */ public static byte[] inflater(final byte[] inputByte) throws IOException { int compressedDataLength = 0; Inflater compresser = new Inflater(false); compresser.setInput(inputByte, 0, inputByte.length); ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length); byte[] result = new byte[1024]; try { while (!compresser.finished()) { compressedDataLength = compresser.inflate(result); if (compressedDataLength == 0) { break; } o.write(result, 0, compressedDataLength); } } catch (Exception ex) { System.err.println("Data format error!\n"); ex.printStackTrace(); } finally { o.close(); } compresser.end(); return o.toByteArray(); }
/** * 解压缩. * * @param inputByte byte[]数组类型的数据 * @return 解压缩后的数据 * @throws IOException */ public static byte[] inflater(final byte[] inputByte) throws IOException { int compressedDataLength = 0; Inflater compresser = new Inflater(false); compresser.setInput(inputByte, 0, inputByte.length); ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length); byte[] result = new byte[1024]; try { while (!compresser.finished()) { compressedDataLength = compresser.inflate(result); if (compressedDataLength == 0) { break; } o.write(result, 0, compressedDataLength); } } catch (Exception ex) { System.err.println("Data format error!\n"); ex.printStackTrace(); } finally { o.close(); } compresser.end(); return o.toByteArray(); }
@Test public void verifyLogoutOneLogoutRequestNotAttempted() throws Exception { final SingleLogoutService service = new WebApplicationServiceFactory().createService(TEST_URL, SingleLogoutService.class); final LogoutRequest logoutRequest = new DefaultLogoutRequest(TICKET_ID, service, new URL(TEST_URL)); final Event event = getLogoutEvent(Arrays.asList(logoutRequest)); assertEquals(FrontChannelLogoutAction.REDIRECT_APP_EVENT, event.getId()); final List<LogoutRequest> list = WebUtils.getLogoutRequests(this.requestContext); assertEquals(1, list.size()); final String url = (String) event.getAttributes().get(FrontChannelLogoutAction.DEFAULT_FLOW_ATTRIBUTE_LOGOUT_URL); assertTrue(url.startsWith(TEST_URL + '?' + FrontChannelLogoutAction.DEFAULT_LOGOUT_PARAMETER + '=')); final byte[] samlMessage = CompressionUtils.decodeBase64ToByteArray( URLDecoder.decode(StringUtils.substringAfter(url, '?' + FrontChannelLogoutAction.DEFAULT_LOGOUT_PARAMETER + '='), "UTF-8")); final Inflater decompresser = new Inflater(); decompresser.setInput(samlMessage); final byte[] result = new byte[1000]; decompresser.inflate(result); decompresser.end(); final String message = new String(result); assertTrue(message.startsWith("<samlp:LogoutRequest xmlns:samlp=\"urn:oasis:names:tc:SAML:2.0:protocol\" ID=\"")); assertTrue(message.contains("<samlp:SessionIndex>" + TICKET_ID + "</samlp:SessionIndex>")); }
@Test public void verifyLogoutOneLogoutRequestNotAttempted() throws Exception { final LogoutRequest logoutRequest = new DefaultLogoutRequest(TICKET_ID, new SimpleWebApplicationServiceImpl(TEST_URL), new URL(TEST_URL)); final Event event = getLogoutEvent(Arrays.asList(logoutRequest)); assertEquals(FrontChannelLogoutAction.REDIRECT_APP_EVENT, event.getId()); final List<LogoutRequest> list = WebUtils.getLogoutRequests(this.requestContext); assertEquals(1, list.size()); final String url = (String) event.getAttributes().get(FrontChannelLogoutAction.DEFAULT_FLOW_ATTRIBUTE_LOGOUT_URL); assertTrue(url.startsWith(TEST_URL + "?" + FrontChannelLogoutAction.DEFAULT_LOGOUT_PARAMETER + "=")); final byte[] samlMessage = CompressionUtils.decodeBase64ToByteArray( URLDecoder.decode(StringUtils.substringAfter(url, "?" + FrontChannelLogoutAction.DEFAULT_LOGOUT_PARAMETER + "="), "UTF-8")); final Inflater decompresser = new Inflater(); decompresser.setInput(samlMessage); final byte[] result = new byte[1000]; decompresser.inflate(result); decompresser.end(); final String message = new String(result); assertTrue(message.startsWith("<samlp:LogoutRequest xmlns:samlp=\"urn:oasis:names:tc:SAML:2.0:protocol\" ID=\"")); assertTrue(message.contains("<samlp:SessionIndex>" + TICKET_ID + "</samlp:SessionIndex>")); }
public static byte[] b(byte[] bArr) throws UnsupportedEncodingException, DataFormatException { int i = 0; if (bArr == null || bArr.length == 0) { return null; } Inflater inflater = new Inflater(); inflater.setInput(bArr, 0, bArr.length); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] bArr2 = new byte[1024]; while (!inflater.needsInput()) { int inflate = inflater.inflate(bArr2); byteArrayOutputStream.write(bArr2, i, inflate); i += inflate; } inflater.end(); return byteArrayOutputStream.toByteArray(); }
/** * Inflate the given byte array by {@link #INFLATED_ARRAY_LENGTH}. * * @param bytes the bytes * @return the array as a string with {@code UTF-8} encoding */ public static String inflate(final byte[] bytes) { final Inflater inflater = new Inflater(true); final byte[] xmlMessageBytes = new byte[INFLATED_ARRAY_LENGTH]; final byte[] extendedBytes = new byte[bytes.length + 1]; System.arraycopy(bytes, 0, extendedBytes, 0, bytes.length); extendedBytes[bytes.length] = 0; inflater.setInput(extendedBytes); try { final int resultLength = inflater.inflate(xmlMessageBytes); inflater.end(); if (!inflater.finished()) { throw new RuntimeException("buffer not large enough."); } inflater.end(); return new String(xmlMessageBytes, 0, resultLength, StandardCharsets.UTF_8); } catch (final DataFormatException e) { return null; } }
@Override public boolean verifyUserSig(String identifier, String sig)throws QCloudException { try { Security.addProvider(new BouncyCastleProvider()); //DeBaseUrl64 urlSig to json Base64 decoder = new Base64(); byte [] compressBytes = Base64Url.base64DecodeUrl(sig.getBytes(Charset.forName("UTF-8"))); //Decompression Inflater decompression = new Inflater(); decompression.setInput(compressBytes, 0, compressBytes.length); byte [] decompressBytes = new byte [1024]; int decompressLength = decompression.inflate(decompressBytes); decompression.end(); String jsonString = new String(Arrays.copyOfRange(decompressBytes, 0, decompressLength)); //Get TLS.Sig from json JSONObject jsonObject= JSON.parseObject(jsonString); String sigTLS = jsonObject.getString("TLS.sig"); //debase64 TLS.Sig to get serailString byte[] signatureBytes = decoder.decode(sigTLS.getBytes(Charset.forName("UTF-8"))); String strSdkAppid = jsonObject.getString("TLS.sdk_appid"); String sigTime = jsonObject.getString("TLS.time"); String sigExpire = jsonObject.getString("TLS.expire_after"); if (!imConfig.getSdkAppId().equals(strSdkAppid)) { return false; } if ( System.currentTimeMillis()/1000 - Long.parseLong(sigTime) > Long.parseLong(sigExpire)) { return false; } //Get Serial String from json String SerialString = "TLS.appid_at_3rd:" + 0 + "\n" + "TLS.account_type:" + 0 + "\n" + "TLS.identifier:" + identifier + "\n" + "TLS.sdk_appid:" + imConfig.getSdkAppId() + "\n" + "TLS.time:" + sigTime + "\n" + "TLS.expire_after:" + sigExpire + "\n"; Reader reader = new CharArrayReader(imConfig.getPublicKey().toCharArray()); PEMParser parser = new PEMParser(reader); JcaPEMKeyConverter converter = new JcaPEMKeyConverter(); Object obj = parser.readObject(); parser.close(); PublicKey pubKeyStruct = converter.getPublicKey((SubjectPublicKeyInfo) obj); Signature signature = Signature.getInstance("SHA256withECDSA","BC"); signature.initVerify(pubKeyStruct); signature.update(SerialString.getBytes(Charset.forName("UTF-8"))); return signature.verify(signatureBytes); }catch (Exception e) { throw new QCloudException(e); } }
public static byte[] uncompress(byte[] input) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Inflater decompressor = new Inflater(); try { decompressor.setInput(input); final byte[] buf = new byte[2048]; while (!decompressor.finished()) { int count = 0; try { count = decompressor.inflate(buf); } catch (DataFormatException e) { e.printStackTrace(); } bos.write(buf, 0, count); } } finally { decompressor.end(); } return bos.toByteArray(); }
/** Descomprime un certificado contenido en el DNIe. * @param compressedCertificate Certificado comprimido en ZIP a partir del 9 byte. * @return Certificado codificado. * @throws IOException Cuando se produce un error en la descompresion del certificado. */ private static byte[] deflate(final byte[] compressedCertificate) throws IOException { final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); final Inflater decompressor = new Inflater(); decompressor.setInput(compressedCertificate, 8, compressedCertificate.length - 8); final byte[] buf = new byte[1024]; try { // Descomprimimos los datos while (!decompressor.finished()) { final int count = decompressor.inflate(buf); if (count == 0) { throw new DataFormatException(); } buffer.write(buf, 0, count); } // Obtenemos los datos descomprimidos return buffer.toByteArray(); } catch (final DataFormatException ex) { throw new IOException("Error al descomprimir el certificado: " + ex, ex); //$NON-NLS-1$ } }
@Override public OneWayCodec createDecoder() throws Exception { return new OneWayCodec() { private final Inflater inflater = new Inflater(); @Override public byte[] code(final byte[] data) throws Exception { inflater.reset(); final InflaterInputStream in = new InflaterInputStream(new ByteArrayInputStream(data), inflater); final ByteArrayOutputStream out = new ByteArrayOutputStream(data.length * 2); final byte[] b = new byte[512]; for (;;) { final int i = in.read(b); if (i == -1) { break; } out.write(b, 0, i); } return out.toByteArray(); } }; }
public static byte[] decompress(byte[] value) throws DataFormatException { ByteArrayOutputStream bos = new ByteArrayOutputStream(value.length); Inflater decompressor = new Inflater(); try { decompressor.setInput(value); final byte[] buf = new byte[1024]; while (!decompressor.finished()) { int count = decompressor.inflate(buf); bos.write(buf, 0, count); } } finally { decompressor.end(); } return bos.toByteArray(); }
static byte[] decompress(byte[] bytesIn, int offset) throws Exception { Inflater inflater = new Inflater(); inflater.setInput(bytesIn, offset, bytesIn.length - offset); ByteArrayOutputStream stream = new ByteArrayOutputStream(bytesIn.length - offset); byte[] buffer = new byte[1024]; while (!inflater.finished()) { int count = inflater.inflate(buffer); stream.write(buffer, 0, count); } stream.close(); byte[] bytesOut = stream.toByteArray(); inflater.end(); return bytesOut; }
@Override public void onMessage(ByteBuffer message) { try { //Thanks to ShadowLordAlpha for code and debugging. //Get the compressed message and inflate it StringBuilder builder = new StringBuilder(); Inflater decompresser = new Inflater(); byte[] bytes = message.array(); decompresser.setInput(bytes, 0, bytes.length); byte[] result = new byte[128]; while (!decompresser.finished()) { int resultLength = decompresser.inflate(result); builder.append(new String(result, 0, resultLength, "UTF-8")); } decompresser.end(); // send the inflated message to the TextMessage method onMessage(builder.toString()); } catch (DataFormatException | UnsupportedEncodingException e) { e.printStackTrace(); } }
@Test public void testLogoutOneLogoutRequestNotAttempted() throws Exception { final String FAKE_URL = "http://url"; LogoutRequest logoutRequest = new LogoutRequest(TICKET_ID, new SimpleWebApplicationServiceImpl(FAKE_URL)); WebUtils.putLogoutRequests(this.requestContext, Arrays.asList(logoutRequest)); this.requestContext.getFlowScope().put(FrontChannelLogoutAction.LOGOUT_INDEX, 0); final Event event = this.frontChannelLogoutAction.doExecute(this.requestContext); assertEquals(FrontChannelLogoutAction.REDIRECT_APP_EVENT, event.getId()); List<LogoutRequest> list = WebUtils.getLogoutRequests(this.requestContext); assertEquals(1, list.size()); final String url = (String) event.getAttributes().get("logoutUrl"); assertTrue(url.startsWith(FAKE_URL + "?SAMLRequest=")); final byte[] samlMessage = Base64.decodeBase64(URLDecoder.decode(StringUtils.substringAfter(url, "?SAMLRequest="), "UTF-8")); final Inflater decompresser = new Inflater(); decompresser.setInput(samlMessage); final byte[] result = new byte[1000]; decompresser.inflate(result); decompresser.end(); final String message = new String(result); assertTrue(message.startsWith("<samlp:LogoutRequest xmlns:samlp=\"urn:oasis:names:tc:SAML:2.0:protocol\" ID=\"")); assertTrue(message.indexOf("<samlp:SessionIndex>" + TICKET_ID + "</samlp:SessionIndex>") >= 0); }
/** * Testing method for decompression of ewf file hex bytes * @param ewfHexStr any zlib compressed hex * @return decompressed string */ protected static String decompress(String ewfHexStr) { Inflater inflater = new Inflater(); byte[] input = DatatypeConverter.parseHexBinary(ewfHexStr); inflater.setInput(input, 0, input.length); String outputString = "empty"; byte[] result = new byte[input.length]; int resultLength; try { resultLength = inflater.inflate(result); outputString = new String(result, 0, resultLength, "UTF-8"); } catch (DataFormatException | UnsupportedEncodingException e) { e.printStackTrace(); } inflater.end(); return outputString; }
private String decodeURLBase64DeflateString(final String input) throws UnsupportedEncodingException, DataFormatException { String urlDecoded = URLDecoder.decode(input, "UTF-8"); byte[] base64Decoded = Base64.decodeBase64(urlDecoded); Inflater decompresser = new Inflater(true); decompresser.setInput(base64Decoded); StringBuilder result = new StringBuilder(); while (!decompresser.finished()) { byte[] outputFraction = new byte[base64Decoded.length]; int resultLength = decompresser.inflate(outputFraction); result.append(new String(outputFraction, 0, resultLength, "UTF-8")); } return result.toString(); }
@Override public byte[] uncompress(byte[] data) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Inflater decompressor = new Inflater(); try { decompressor.setInput(data); final byte[] buf = new byte[2048]; while (!decompressor.finished()) { int count = decompressor.inflate(buf); bos.write(buf, 0, count); } } catch (DataFormatException e) { e.printStackTrace(); } finally { decompressor.end(); } return bos.toByteArray(); }
public static byte[] inflate(PbfRawBlob rawBlob) throws InvalidProtocolBufferException { Blob blob = Blob.parseFrom(rawBlob.getData()); byte[] blobData; if (blob.hasRaw()) { blobData = blob.getRaw().toByteArray(); } else if (blob.hasZlibData()) { Inflater inflater = new Inflater(); inflater.setInput(blob.getZlibData().toByteArray()); blobData = new byte[blob.getRawSize()]; try { inflater.inflate(blobData); } catch (DataFormatException e) { throw new OsmosisRuntimeException("Unable to decompress PBF blob.", e); } if (!inflater.finished()) { throw new OsmosisRuntimeException("PBF blob contains incomplete compressed data."); } } else { throw new OsmosisRuntimeException("PBF blob uses unsupported compression, only raw or zlib may be used."); } return blobData; }
public static DeflaterInflaterData uncompressBytes(byte[] output, int compressedDataLength) throws DataFormatException { Inflater decompresser = new Inflater(); decompresser.setInput(output, 0, compressedDataLength); byte[] buffer = new byte[512]; byte[] result = new byte[0]; int bytesRead; while (!decompresser.needsInput()) { bytesRead = decompresser.inflate(buffer); byte[] newResult = new byte[result.length + bytesRead]; System.arraycopy(result, 0, newResult, 0, result.length); System.arraycopy(buffer, 0, newResult, result.length, bytesRead); result = newResult; } // System.out.println(new String(result)); decompresser.end(); return new DeflaterInflaterData(result.length, result); }
private void refillInflater(Inflater inflater) throws IOException { while(chunkRemaining == 0) { closeChunk(); openChunk(IDAT); } int read = readChunk(buffer, 0, buffer.length); inflater.setInput(buffer, 0, read); }
protected void openFile() throws IOException { InputStream d = db.isFilesInJar() ? getClass().getResourceAsStream(fileName) : db.getFileAccess().openInputStreamElement(fileName); dataStreamIn = new DataInputStream( new BufferedInputStream( new InflaterInputStream(d, new Inflater()), 1 << 13)); }
private void enableCompression(Socket socket) throws IOException { InputStream inputStream = new InflaterInputStream(socket.getInputStream(), new Inflater(true)); input = Okio.buffer(Okio.source(inputStream)); ZOutputStream outputStream = new ZOutputStream(socket.getOutputStream(), JZlib.Z_BEST_SPEED, true); outputStream.setFlushMode(JZlib.Z_PARTIAL_FLUSH); output = Okio.buffer(Okio.sink(outputStream)); }
static byte[] decompress(byte[] data) throws IOException, DataFormatException { Inflater inflater = new Inflater(true); inflater.setInput(data); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); byte[] buffer = new byte[1024]; while (!inflater.finished()) { int count = inflater.inflate(buffer); outputStream.write(buffer, 0, count); } outputStream.close(); byte[] output = outputStream.toByteArray(); Log.d("Compressor", "Original: " + data.length); Log.d("Compressor", "Decompressed: " + output.length); return output; }
/** * Creates a new input stream with the specified decompressor and * buffer size. * * @param in the input stream * @param inf the decompressor ("inflater") * @param size the input buffer size * @throws IllegalArgumentException if size is <= 0 */ public InflaterInputStream(InputStream in, Inflater inf, int size) { super(in); if (in == null || inf == null) { throw new NullPointerException(); } else if (size <= 0) { throw new IllegalArgumentException("buffer size <= 0"); } this.inf = inf; buf = new byte[size]; }
public SocketInput(SocketMonitor socketMonitor) { super(socketMonitor); this.socketInputStream = socketMonitor.getSocket().getInputStream(); this.dataInputStream = new DataInputStream(socketInputStream) { @Override public void close() throws IOException { //prevents being closed by packets } }; this.inflater = new Inflater(SocketOutput.COMPRESSION_NOWRAP); this.pairedStreams = new PairedStreams(); }
public InputStream getInputStream(OutputStream monitorStream) throws IOException { if (entryOut != null) { entryOut.close(); size = entryOut.getSize(); data = ((ByteArrayOutputStream)entryOut.getWrappedStream()).toByteArray(); compressedSize = data.length; crc32 = entryOut.getCRC(); entryOut = null; InputStream rawis = new ByteArrayInputStream( data); if (compression == 0) return rawis; else { // Hacky, inflate using a sequence of input streams that returns 1 byte more than the actual length of the data. // This extra dummy byte is required by InflaterInputStream when the data doesn't have the header and crc fields (as it is in zip files). return new InflaterInputStream( new SequenceInputStream(rawis, new ByteArrayInputStream(new byte[1])), new Inflater( true)); } } ZioEntryInputStream dataStream; dataStream = new ZioEntryInputStream(this); if (monitorStream != null) dataStream.setMonitorStream( monitorStream); if (compression != 0) { // Note: When using nowrap=true with Inflater it is also necessary to provide // an extra "dummy" byte as input. This is required by the ZLIB native library // in order to support certain optimizations. dataStream.setReturnDummyByte(true); return new InflaterInputStream( dataStream, new Inflater( true)); } else return dataStream; }
public GzipSource(Source source) { if (source == null) { throw new IllegalArgumentException("source == null"); } this.inflater = new Inflater(true); this.source = Okio.buffer(source); this.inflaterSource = new InflaterSource(this.source, this.inflater); }
/** * Creates a new CompressedInputStream that reads the given stream from the * server. * * @param conn * @param streamFromServer */ public CompressedInputStream(Connection conn, InputStream streamFromServer) { this.traceProtocol = ((ConnectionPropertiesImpl) conn).traceProtocol; try { this.log = conn.getLog(); } catch (SQLException e) { this.log = new NullLogger(null); } this.in = streamFromServer; this.inflater = new Inflater(); }
public static byte[] decompressZlib(byte[] input) { try { Inflater decompress = new Inflater(); decompress.setInput(input); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(byteArrayOutputStream, decompress); inflaterOutputStream.flush(); return byteArrayOutputStream.toByteArray(); } catch (Exception e) { System.out.println("An error occured while decompressing data"); System.exit(-2); } return null; }