Search in sources :

Example 51 with Inflater

use of java.util.zip.Inflater in project cxf by apache.

the class CompressionUtils method inflate.

public static InputStream inflate(byte[] deflatedToken, boolean nowrap) throws DataFormatException {
    Inflater inflater = new Inflater(true);
    inflater.setInput(deflatedToken);
    byte[] buffer = new byte[deflatedToken.length];
    int inflateLen;
    ByteArrayOutputStream inflatedToken = new ByteArrayOutputStream();
    while (!inflater.finished()) {
        inflateLen = inflater.inflate(buffer, 0, deflatedToken.length);
        if (inflateLen == 0 && !inflater.finished()) {
            if (inflater.needsInput()) {
                throw new DataFormatException("Inflater can not inflate all the token bytes");
            }
            break;
        }
        inflatedToken.write(buffer, 0, inflateLen);
    }
    return new ByteArrayInputStream(inflatedToken.toByteArray());
}
Also used : DataFormatException(java.util.zip.DataFormatException) ByteArrayInputStream(java.io.ByteArrayInputStream) Inflater(java.util.zip.Inflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 52 with Inflater

use of java.util.zip.Inflater in project cxf by apache.

the class DeflateEncoderDecoderTest method testInflateDeflateWithTokenDuplication.

@Test
public void testInflateDeflateWithTokenDuplication() throws Exception {
    String token = "valid_grant valid_grant valid_grant valid_grant valid_grant valid_grant";
    DeflateEncoderDecoder deflateEncoderDecoder = new DeflateEncoderDecoder();
    byte[] deflatedToken = deflateEncoderDecoder.deflateToken(token.getBytes());
    String cxfInflatedToken = IOUtils.toString(deflateEncoderDecoder.inflateToken(deflatedToken));
    String streamInflatedToken = IOUtils.toString(new InflaterInputStream(new ByteArrayInputStream(deflatedToken), new Inflater(true)));
    assertEquals(streamInflatedToken, token);
    assertEquals(cxfInflatedToken, token);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater) Test(org.junit.Test)

Example 53 with Inflater

use of java.util.zip.Inflater in project leopard by tanhaichao.

the class tls_sigature method CheckTLSSignature.

/**
 * @brief 校验 tls 票据
 * @param urlSig 返回 tls 票据
 * @param strAppid3rd 填写与 sdkAppid 一致的字符串形式的值
 * @param skdAppid 应的 appid
 * @param identifier 用户 id
 * @param accountType 创建应用后在配置页面上展示的 acctype
 * @param publicKey 用于校验 tls 票据的公钥内容,但是需要先将公钥文件转换为 java 原生 api 使用的格式,下面是推荐的命令
 *         openssl pkcs8 -topk8 -in ec_key.pem -outform PEM -out p8_priv.pem -nocrypt
 * @return 如果出错 CheckTLSSignatureResult 中的 verifyResult 为 false,错误信息在 errMsg,校验成功为 true
 * @throws DataFormatException
 */
@Deprecated
public static CheckTLSSignatureResult CheckTLSSignature(String urlSig, String strAppid3rd, long skdAppid, String identifier, long accountType, String publicKey) throws DataFormatException {
    CheckTLSSignatureResult result = new CheckTLSSignatureResult();
    Security.addProvider(new BouncyCastleProvider());
    // DeBaseUrl64 urlSig to json
    Base64 decoder = new Base64();
    // byte [] compressBytes = decoder.decode(urlSig.getBytes());
    byte[] compressBytes = base64_url.base64DecodeUrl(urlSig.getBytes(Charset.forName("UTF-8")));
    // System.out.println("#compressBytes Passing in[" + compressBytes.length + "] " + Hex.encodeHexString(compressBytes));
    // 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));
    // System.out.println("#Json String passing in : \n" + jsonString);
    // Get TLS.Sig from json
    JSONObject jsonObject = new JSONObject(jsonString);
    String sigTLS = jsonObject.getString("TLS.sig");
    // debase64 TLS.Sig to get serailString
    byte[] signatureBytes = decoder.decode(sigTLS.getBytes(Charset.forName("UTF-8")));
    try {
        String sigTime = jsonObject.getString("TLS.time");
        String sigExpire = jsonObject.getString("TLS.expire_after");
        // + Long.parseLong(sigTime) + "-" + Long.parseLong(sigExpire));
        if (System.currentTimeMillis() / 1000 - Long.parseLong(sigTime) > Long.parseLong(sigExpire)) {
            result.errMessage = new String("TLS sig is out of date ");
            System.out.println("Timeout");
            return result;
        }
        // Get Serial String from json
        String SerialString = "TLS.appid_at_3rd:" + strAppid3rd + "\n" + "TLS.account_type:" + accountType + "\n" + "TLS.identifier:" + identifier + "\n" + "TLS.sdk_appid:" + skdAppid + "\n" + "TLS.time:" + sigTime + "\n" + "TLS.expire_after:" + sigExpire + "\n";
        // System.out.println("#SerialString : \n" + SerialString);
        Reader reader = new CharArrayReader(publicKey.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")));
        boolean bool = signature.verify(signatureBytes);
        // System.out.println("#jdk ecdsa verify : " + bool);
        result.verifyResult = bool;
    } catch (Exception e) {
        e.printStackTrace();
        result.errMessage = "Failed in checking sig";
    }
    return result;
}
Also used : Base64(org.apache.commons.codec.binary.Base64) PublicKey(java.security.PublicKey) CharArrayReader(java.io.CharArrayReader) Reader(java.io.Reader) JcaPEMKeyConverter(org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter) IOException(java.io.IOException) DataFormatException(java.util.zip.DataFormatException) CharArrayReader(java.io.CharArrayReader) JSONObject(org.json.JSONObject) PEMParser(org.bouncycastle.openssl.PEMParser) Signature(java.security.Signature) Inflater(java.util.zip.Inflater) JSONObject(org.json.JSONObject) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 54 with Inflater

use of java.util.zip.Inflater in project scheduling by ow2-proactive.

the class ObjectByteConverter method byteArrayToObject.

/**
 * Convert the given byte array into the corresponding object.
 * <p>
 * The given byteArray can be uncompressed if it has been compressed before.
 *
 * @param input the byteArray to be convert as an object.
 * @param uncompress true if the given byteArray must be also uncompressed, false if no compression was made on it.
 * @return the object corresponding to the given byteArray.
 * @throws IOException if an I/O exception occurs when writing the returned object
 * @throws ClassNotFoundException if class represented by given byteArray is not found.
 */
public static Object byteArrayToObject(byte[] input, boolean uncompress) throws IOException, ClassNotFoundException {
    if (input == null) {
        return null;
    }
    if (uncompress) {
        // Uncompress the bytes
        Inflater decompressor = new Inflater();
        decompressor.setInput(input);
        ByteArrayOutputStream bos = null;
        try {
            // Create an expandable byte array to hold the compressed data.
            bos = new ByteArrayOutputStream();
            // Compress the data
            byte[] buf = new byte[512];
            while (!decompressor.finished()) {
                int count = decompressor.inflate(buf);
                bos.write(buf, 0, count);
            }
            decompressor.end();
            // set the UNCOMPRESSED data
            input = bos.toByteArray();
        } catch (DataFormatException dfe) {
            // convert into io exception to fit previous behavior
            throw new IOException("Compressed data format is invalid : " + dfe.getMessage(), dfe);
        } finally {
            if (bos != null) {
                bos.close();
            }
        }
    }
    // here, input byteArray is uncompressed if needed
    ByteArrayInputStream bais = null;
    ObjectInputStream ois = null;
    try {
        bais = new ByteArrayInputStream(input);
        ois = new ObjectInputStream(bais);
        return ois.readObject();
    } finally {
        if (ois != null) {
            ois.close();
        }
        if (bais != null) {
            bais.close();
        }
    }
}
Also used : DataFormatException(java.util.zip.DataFormatException) ByteArrayInputStream(java.io.ByteArrayInputStream) Inflater(java.util.zip.Inflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 55 with Inflater

use of java.util.zip.Inflater in project graphhopper by graphhopper.

the class PbfBlobDecoder method readBlobContent.

private byte[] readBlobContent() throws IOException {
    Fileformat.Blob blob = Fileformat.Blob.parseFrom(rawBlob);
    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 RuntimeException("Unable to decompress PBF blob.", e);
        }
        if (!inflater.finished()) {
            throw new RuntimeException("PBF blob contains incomplete compressed data.");
        }
        inflater.end();
    } else {
        throw new RuntimeException("PBF blob uses unsupported compression, only raw or zlib may be used.");
    }
    return blobData;
}
Also used : DataFormatException(java.util.zip.DataFormatException) Fileformat(org.openstreetmap.osmosis.osmbinary.Fileformat) Inflater(java.util.zip.Inflater)

Aggregations

Inflater (java.util.zip.Inflater)108 InflaterInputStream (java.util.zip.InflaterInputStream)36 IOException (java.io.IOException)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)31 DataFormatException (java.util.zip.DataFormatException)30 InputStream (java.io.InputStream)24 ByteArrayInputStream (java.io.ByteArrayInputStream)14 GZIPInputStream (java.util.zip.GZIPInputStream)14 Deflater (java.util.zip.Deflater)9 Test (org.junit.Test)7 DataInputStream (java.io.DataInputStream)6 OutputStream (java.io.OutputStream)6 BufferedInputStream (java.io.BufferedInputStream)5 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 HttpURLConnection (java.net.HttpURLConnection)5 ByteBuffer (java.nio.ByteBuffer)5 JSONObject (org.json.JSONObject)5 DataOutputStream (java.io.DataOutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4