Search in sources :

Example 76 with Deflater

use of java.util.zip.Deflater in project bitsquare by bitsquare.

the class Utils method compress.

private static byte[] compress(byte[] input) {
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_SPEED);
    compressor.setInput(input);
    compressor.finish();
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[8192];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    try {
        bos.close();
    } catch (IOException ignored) {
    }
    return bos.toByteArray();
}
Also used : Deflater(java.util.zip.Deflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 77 with Deflater

use of java.util.zip.Deflater in project cas by apereo.

the class CompressionUtils method deflate.

/**
 * Deflate the given string via a {@link java.util.zip.Deflater}.
 *
 * @param data the data
 * @return base64 encoded string
 */
public static String deflate(final String data) {
    final Deflater deflater = new Deflater();
    deflater.setInput(data.getBytes(StandardCharsets.UTF_8));
    deflater.finish();
    final byte[] buffer = new byte[data.length()];
    final int resultSize = deflater.deflate(buffer);
    final byte[] output = new byte[resultSize];
    System.arraycopy(buffer, 0, output, 0, resultSize);
    return EncodingUtils.encodeBase64(output);
}
Also used : Deflater(java.util.zip.Deflater)

Example 78 with Deflater

use of java.util.zip.Deflater in project hive by apache.

the class Utilities method setBaseWork.

private static Path setBaseWork(Configuration conf, BaseWork w, Path hiveScratchDir, String name, boolean useCache) {
    Kryo kryo = SerializationUtilities.borrowKryo();
    try {
        setPlanPath(conf, hiveScratchDir);
        Path planPath = getPlanPath(conf, name);
        setHasWork(conf, name);
        OutputStream out = null;
        final long serializedSize;
        final String planMode;
        if (HiveConf.getBoolVar(conf, ConfVars.HIVE_RPC_QUERY_PLAN)) {
            // add it to the conf
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            try {
                out = new DeflaterOutputStream(byteOut, new Deflater(Deflater.BEST_SPEED));
                SerializationUtilities.serializePlan(kryo, w, out);
                out.close();
                out = null;
            } finally {
                IOUtils.closeStream(out);
            }
            final String serializedPlan = Base64.encodeBase64String(byteOut.toByteArray());
            serializedSize = serializedPlan.length();
            planMode = "RPC";
            conf.set(planPath.toUri().getPath(), serializedPlan);
        } else {
            // use the default file system of the conf
            FileSystem fs = planPath.getFileSystem(conf);
            try {
                out = fs.create(planPath);
                SerializationUtilities.serializePlan(kryo, w, out);
                out.close();
                out = null;
                long fileLen = fs.getFileStatus(planPath).getLen();
                serializedSize = fileLen;
                planMode = "FILE";
            } finally {
                IOUtils.closeStream(out);
            }
            // able to get the plan directly from the cache
            if (useCache && !ShimLoader.getHadoopShims().isLocalMode(conf)) {
                // Set up distributed cache
                if (!DistributedCache.getSymlink(conf)) {
                    DistributedCache.createSymlink(conf);
                }
                String uriWithLink = planPath.toUri().toString() + "#" + name;
                DistributedCache.addCacheFile(new URI(uriWithLink), conf);
                // set replication of the plan file to a high number. we use the same
                // replication factor as used by the hadoop jobclient for job.xml etc.
                short replication = (short) conf.getInt("mapred.submit.replication", 10);
                fs.setReplication(planPath, replication);
            }
        }
        LOG.info("Serialized plan (via {}) - name: {} size: {}", planMode, w.getName(), humanReadableByteCount(serializedSize));
        // Cache the plan in this process
        gWorkMap.get(conf).put(planPath, w);
        return planPath;
    } catch (Exception e) {
        String msg = "Error caching " + name;
        LOG.error(msg, e);
        throw new RuntimeException(msg, e);
    } finally {
        SerializationUtilities.releaseKryo(kryo);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URI(java.net.URI) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SQLTransientException(java.sql.SQLTransientException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SerDeException(org.apache.hadoop.hive.serde2.SerDeException) SemanticException(org.apache.hadoop.hive.ql.parse.SemanticException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) Deflater(java.util.zip.Deflater) FileSystem(org.apache.hadoop.fs.FileSystem) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) Kryo(com.esotericsoftware.kryo.Kryo)

Example 79 with Deflater

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

the class tls_sigature method GenTLSSignature.

/**
 * @brief 生成 tls 票据
 * @param expire 有效期,单位是秒,推荐一个月
 * @param strAppid3rd 填写与 sdkAppid 一致字符串形式的值
 * @param skdAppid 应用的 appid
 * @param identifier 用户 id
 * @param accountType 创建应用后在配置页面上展示的 acctype
 * @param privStr 生成 tls 票据使用的私钥内容
 * @return 如果出错,GenTLSSignatureResult 中的 urlSig为空,errMsg 为出错信息,成功返回有效的票据
 * @throws IOException
 */
@Deprecated
public static GenTLSSignatureResult GenTLSSignature(long expire, String strAppid3rd, long skdAppid, String identifier, long accountType, String privStr) throws IOException {
    GenTLSSignatureResult result = new GenTLSSignatureResult();
    Security.addProvider(new BouncyCastleProvider());
    Reader reader = new CharArrayReader(privStr.toCharArray());
    JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
    PEMParser parser = new PEMParser(reader);
    Object obj = parser.readObject();
    parser.close();
    PrivateKey privKeyStruct = converter.getPrivateKey((PrivateKeyInfo) obj);
    // Create Json string and serialization String
    String jsonString = "{" + "\"TLS.account_type\":\"" + accountType + "\"," + "\"TLS.identifier\":\"" + identifier + "\"," + "\"TLS.appid_at_3rd\":\"" + strAppid3rd + "\"," + "\"TLS.sdk_appid\":\"" + skdAppid + "\"," + "\"TLS.expire_after\":\"" + expire + "\"" + "}";
    // System.out.println("#jsonString : \n" + jsonString);
    String time = String.valueOf(System.currentTimeMillis() / 1000);
    String SerialString = "TLS.appid_at_3rd:" + strAppid3rd + "\n" + "TLS.account_type:" + accountType + "\n" + "TLS.identifier:" + identifier + "\n" + "TLS.sdk_appid:" + skdAppid + "\n" + "TLS.time:" + time + "\n" + "TLS.expire_after:" + expire + "\n";
    try {
        // Create Signature by SerialString
        Signature signature = Signature.getInstance("SHA256withECDSA", "BC");
        signature.initSign(privKeyStruct);
        signature.update(SerialString.getBytes(Charset.forName("UTF-8")));
        byte[] signatureBytes = signature.sign();
        String sigTLS = Base64.encodeBase64String(signatureBytes);
        // System.out.println("#sigTLS : " + sigTLS);
        // Add TlsSig to jsonString
        JSONObject jsonObject = new JSONObject(jsonString);
        jsonObject.put("TLS.sig", (Object) sigTLS);
        jsonObject.put("TLS.time", (Object) time);
        jsonString = jsonObject.toString();
        // System.out.println("#jsonString : \n" + jsonString);
        // compression
        Deflater compresser = new Deflater();
        compresser.setInput(jsonString.getBytes(Charset.forName("UTF-8")));
        compresser.finish();
        byte[] compressBytes = new byte[512];
        int compressBytesLength = compresser.deflate(compressBytes);
        compresser.end();
        // System.out.println("#compressBytes "+ compressBytesLength+": " + Hex.encodeHexString(Arrays.copyOfRange(compressBytes,0,compressBytesLength)));
        // String userSig = Base64.encodeBase64URLSafeString(Arrays.copyOfRange(compressBytes,0,compressBytesLength));
        String userSig = new String(base64_url.base64EncodeUrl(Arrays.copyOfRange(compressBytes, 0, compressBytesLength)));
        result.urlSig = userSig;
    // System.out.println("urlSig: "+ userSig);
    } catch (Exception e) {
        e.printStackTrace();
        result.errMessage = "generate usersig failed";
    }
    return result;
}
Also used : PrivateKey(java.security.PrivateKey) 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) PEMParser(org.bouncycastle.openssl.PEMParser) JSONObject(org.json.JSONObject) Deflater(java.util.zip.Deflater) Signature(java.security.Signature) JSONObject(org.json.JSONObject) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 80 with Deflater

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

the class tls_sigature method GenTLSSignatureEx.

/**
 * @brief 生成 tls 票据,精简参数列表
 * @param skdAppid 应用的 sdkappid
 * @param identifier 用户 id
 * @param privStr 私钥文件内容
 * @param expire 有效期,以秒为单位,推荐时长一个月
 * @return
 * @throws IOException
 */
public static GenTLSSignatureResult GenTLSSignatureEx(long skdAppid, String identifier, String privStr, long expire) throws IOException {
    GenTLSSignatureResult result = new GenTLSSignatureResult();
    Security.addProvider(new BouncyCastleProvider());
    Reader reader = new CharArrayReader(privStr.toCharArray());
    JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
    PEMParser parser = new PEMParser(reader);
    Object obj = parser.readObject();
    parser.close();
    PrivateKey privKeyStruct = converter.getPrivateKey((PrivateKeyInfo) obj);
    String jsonString = "{" + "\"TLS.account_type\":\"" + 0 + "\"," + "\"TLS.identifier\":\"" + identifier + "\"," + "\"TLS.appid_at_3rd\":\"" + 0 + "\"," + "\"TLS.sdk_appid\":\"" + skdAppid + "\"," + "\"TLS.expire_after\":\"" + expire + "\"," + "\"TLS.version\": \"201512300000\"" + "}";
    String time = String.valueOf(System.currentTimeMillis() / 1000);
    String SerialString = "TLS.appid_at_3rd:" + 0 + "\n" + "TLS.account_type:" + 0 + "\n" + "TLS.identifier:" + identifier + "\n" + "TLS.sdk_appid:" + skdAppid + "\n" + "TLS.time:" + time + "\n" + "TLS.expire_after:" + expire + "\n";
    try {
        // Create Signature by SerialString
        Signature signature = Signature.getInstance("SHA256withECDSA", "BC");
        signature.initSign(privKeyStruct);
        signature.update(SerialString.getBytes(Charset.forName("UTF-8")));
        byte[] signatureBytes = signature.sign();
        String sigTLS = Base64.encodeBase64String(signatureBytes);
        // Add TlsSig to jsonString
        JSONObject jsonObject = new JSONObject(jsonString);
        jsonObject.put("TLS.sig", (Object) sigTLS);
        jsonObject.put("TLS.time", (Object) time);
        jsonString = jsonObject.toString();
        // compression
        Deflater compresser = new Deflater();
        compresser.setInput(jsonString.getBytes(Charset.forName("UTF-8")));
        compresser.finish();
        byte[] compressBytes = new byte[512];
        int compressBytesLength = compresser.deflate(compressBytes);
        compresser.end();
        String userSig = new String(base64_url.base64EncodeUrl(Arrays.copyOfRange(compressBytes, 0, compressBytesLength)));
        result.urlSig = userSig;
    } catch (Exception e) {
        e.printStackTrace();
        result.errMessage = "generate usersig failed";
    }
    return result;
}
Also used : PrivateKey(java.security.PrivateKey) 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) PEMParser(org.bouncycastle.openssl.PEMParser) JSONObject(org.json.JSONObject) Deflater(java.util.zip.Deflater) Signature(java.security.Signature) JSONObject(org.json.JSONObject) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Aggregations

Deflater (java.util.zip.Deflater)85 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)25 IOException (java.io.IOException)18 Test (org.junit.Test)13 Inflater (java.util.zip.Inflater)9 OutputStream (java.io.OutputStream)7 InflaterInputStream (java.util.zip.InflaterInputStream)7 InputStream (java.io.InputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 EOFException (java.io.EOFException)3 FileOutputStream (java.io.FileOutputStream)3 Field (java.lang.reflect.Field)3 CRC32 (java.util.zip.CRC32)3 BufferedOutputStream (java.io.BufferedOutputStream)2 CharArrayReader (java.io.CharArrayReader)2 DataOutputStream (java.io.DataOutputStream)2 FileInputStream (java.io.FileInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Reader (java.io.Reader)2