private DeflateCompressor() { deflater = new FastThreadLocal<Deflater>() { @Override protected Deflater initialValue() { return new Deflater(); } }; inflater = new FastThreadLocal<Inflater>() { @Override protected Inflater initialValue() { return new Inflater(); } }; }
private String zipCompression(String data) throws UnsupportedEncodingException, IOException { Deflater zipDeflater = new Deflater(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); try { zipDeflater.setInput(getBytes(data)); zipDeflater.finish(); byte[] buffer = new byte[1024]; int count = 0; while (!zipDeflater.finished()) { count = zipDeflater.deflate(buffer); stream.write(buffer, 0, count); } return new String(Base64.getEncoder().encode(stream.toByteArray()), LOCAL_ENCODING); } finally { stream.close(); zipDeflater.end(); } }
/** * zlib compress 2 byte * * @param bytesToCompress * @return */ public static byte[] compressForZlib(byte[] bytesToCompress) { Deflater deflater = new Deflater(); deflater.setInput(bytesToCompress); deflater.finish(); byte[] bytesCompressed = new byte[Short.MAX_VALUE]; int numberOfBytesAfterCompression = deflater.deflate(bytesCompressed); byte[] returnValues = new byte[numberOfBytesAfterCompression]; System.arraycopy ( bytesCompressed, 0, returnValues, 0, numberOfBytesAfterCompression ); return returnValues; }
/** * 压缩. * * @param inputByte * 需要解压缩的byte[]数组 * @return 压缩后的数据 * @throws IOException */ public static byte[] deflater(final byte[] inputByte) throws IOException { int compressedDataLength = 0; Deflater compresser = new Deflater(); compresser.setInput(inputByte); compresser.finish(); ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length); byte[] result = new byte[1024]; try { while (!compresser.finished()) { compressedDataLength = compresser.deflate(result); o.write(result, 0, compressedDataLength); } } finally { o.close(); } compresser.end(); return o.toByteArray(); }
public static byte[] compress(final byte[] data) { final Deflater deflater = new Deflater(); deflater.setInput(data); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); deflater.finish(); final byte[] buffer = new byte[1024]; try { while (!deflater.finished()) { final int count = deflater.deflate(buffer); // returns the generated // code... // index outputStream.write(buffer, 0, count); } outputStream.close(); } catch (final IOException e) { log.log(Level.SEVERE, e.getMessage(), e); } return outputStream.toByteArray(); }
protected void openFile() throws HsqlException { try { FileAccess fa = database.getFileAccess(); java.io.OutputStream fos = fa.openOutputStreamElement(outFile); outDescriptor = fa.getFileSync(fos); fileStreamOut = new DeflaterOutputStream(fos, new Deflater(Deflater.DEFAULT_COMPRESSION), bufferSize); } catch (IOException e) { throw Trace.error(Trace.FILE_IO_ERROR, Trace.Message_Pair, new Object[] { e.toString(), outFile }); } }
/** * 压缩. * * @param inputByte 需要解压缩的byte[]数组 * @return 压缩后的数据 * @throws IOException */ public static byte[] deflater(final byte[] inputByte) throws IOException { int compressedDataLength = 0; Deflater compresser = new Deflater(); compresser.setInput(inputByte); compresser.finish(); ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length); byte[] result = new byte[1024]; try { while (!compresser.finished()) { compressedDataLength = compresser.deflate(result); o.write(result, 0, compressedDataLength); } } finally { o.close(); } compresser.end(); return o.toByteArray(); }
public static byte[] deflate(byte[] data, int level) throws Exception { Deflater deflater = new Deflater(level); deflater.reset(); deflater.setInput(data); deflater.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); byte[] buf = new byte[1024]; try { while (!deflater.finished()) { int i = deflater.deflate(buf); bos.write(buf, 0, i); } } finally { deflater.end(); } return bos.toByteArray(); }
protected void openFile() { try { FileAccess fa = database.getFileAccess(); java.io.OutputStream fos = fa.openOutputStreamElement(outFile); outDescriptor = fa.getFileSync(fos); fileStreamOut = new DeflaterOutputStream(fos, new Deflater(Deflater.DEFAULT_COMPRESSION), bufferSize); } catch (IOException e) { throw Error.error(ErrorCode.FILE_IO_ERROR, ErrorCode.M_Message_Pair, new Object[] { e.toString(), outFile }); } }
public Packer(final File destFile, final Signer signer, final Boolean inPlace) throws FileNotFoundException { this.inPlace = inPlace; if (inPlace) { //In codesign.py this is what we use this.zipStream = new ZipOutputStream( new DataOutputStream( new ByteArrayOutputStream(128*1024*1024-1))); //Avoid java bug https://bugs.openjdk.java.net/browse/JDK-8055949 by being able to get to max buffer size of MAX_INT-16 zipStream.setLevel(Deflater.NO_COMPRESSION); } else { this.zipStream = new ZipOutputStream( new BufferedOutputStream( new FileOutputStream(destFile))); } this.signer = signer; }
/** Write the entire content into the given file using Flate compression (see RFC1951) then return the number of bytes written. */ public long dumpFlate(RandomAccessFile os) throws IOException { Deflater zip = new Deflater(Deflater.BEST_COMPRESSION); byte[] output = new byte[8192]; Iterator<byte[]> it = list.iterator(); // when null, that means we have told the Deflater that no more input would be coming long ans = 0; // the number of bytes written out so far while(true) { if (it!=null && zip.needsInput() && it.hasNext()) { byte[] in = it.next(); if (in == list.getLast()) { zip.setInput(in, 0, n); it=null; zip.finish(); } else { zip.setInput(in, 0, SIZE); } } if (it==null && zip.finished()) break; int count = zip.deflate(output); if (count > 0) { ans = ans + count; if (ans < 0) throw new IOException("Data too large to be written to the output file."); os.write(output, 0, count); } } return ans; }
@Override public OneWayCodec createEncoder() throws Exception { return new OneWayCodec() { private final Deflater deflater = new Deflater(compressionLevel); @Override public byte[] code(final byte[] data) throws Exception { deflater.reset(); final ByteArrayOutputStream bout = new ByteArrayOutputStream(data.length / 2); try (final DeflaterOutputStream out = new DeflaterOutputStream(bout, deflater)) { out.write(data); } return bout.toByteArray(); } }; }
public static byte[] compress(byte[] value, int offset, int length, int compressionLevel) { ByteArrayOutputStream bos = new ByteArrayOutputStream(length); Deflater compressor = new Deflater(); try { compressor.setLevel(compressionLevel); // 将当前压缩级别设置为指定值。 compressor.setInput(value, offset, length); compressor.finish(); // 调用时,指示压缩应当以输入缓冲区的当前内容结尾。 // Compress the data final byte[] buf = new byte[1024]; while (!compressor.finished()) { // 如果已到达压缩数据输出流的结尾,则返回 true。 int count = compressor.deflate(buf); // 使用压缩数据填充指定缓冲区。 bos.write(buf, 0, count); } } finally { compressor.end(); // 关闭解压缩器并放弃所有未处理的输入。 } return bos.toByteArray(); }
public static byte[] deflate(byte[] data, int level) throws Exception { Deflater deflater = new Deflater(level); deflater.reset(); deflater.setInput(data); deflater.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); byte[] buf = new byte[1024]; try { while (!deflater.finished()) { int i = deflater.deflate(buf); bos.write(buf, 0, i); } } finally { deflater.end(); bos.close(); } return bos.toByteArray(); }
@Override public synchronized void flush() throws IOException { if (hasLastByte) { // - do not allow the gzip header to be flushed on its own // - do not do anything if there is no data to send // trick the deflater to flush /** * Now this is tricky: We force the Deflater to flush its data by * switching compression level. As yet, a perplexingly simple workaround * for * http://developer.java.sun.com/developer/bugParade/bugs/4255743.html */ if (!def.finished()) { def.setLevel(Deflater.NO_COMPRESSION); flushLastByte(); flagReenableCompression = true; } } out.flush(); }
public static byte[] compress(byte input[]) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Deflater compressor = new Deflater(1); try { compressor.setInput(input); compressor.finish(); final byte[] buf = new byte[2048]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } } finally { compressor.end(); } return bos.toByteArray(); }
@Override public byte[] compress(byte[] data) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Deflater compressor = new Deflater(1); try { compressor.setInput(data); compressor.finish(); final byte[] buf = new byte[2048]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } } finally { compressor.end(); } return bos.toByteArray(); }
private void storeData(ByteBuffer data) { try { final byte[] input = data.array(); FileOutputStream fos = new FileOutputStream(file); final Deflater deflater = new Deflater(Deflater.BEST_SPEED, true); deflater.setInput(input, data.arrayOffset(), data.remaining()); deflater.finish(); byte[] buf = new byte[1024]; while (!deflater.finished()) { int byteCount = deflater.deflate(buf); fos.write(buf, 0, byteCount); } deflater.end(); fos.close(); } catch (Exception e) { FileLog.e("tmessages", e); } }
public DataStream zlibCompress() { Deflater compressor = new Deflater(); compressor.setInput(this.get()); this.reset(); this.put(Hex.toByteArray("0178040000")); // size?! byte[] buf = new byte[1024]; int length = 0; compressor.finish(); while (!compressor.finished()) { int count = compressor.deflate(buf); this.put(buf, 0, count); length += count; } compressor.end(); return this; }
public static boolean write(Save save, Area area) { if (save.readOnly) return false; if (!area.isReady()) return false; AreaMap map = area.areaMap(); DataGroup[] dataGroups; if (map == null || map.world == null || map.world.entities == null) { dataGroups = new DataGroup[0]; } else { dataGroups = map.world.entities.getEntitiesForSave(area.areaX, area.areaZ); } if (!area.modifiedSinceSave(dataGroups)) return false; area.saveModCount(); Deflater deflater = deflaterThreadLocal.get(); FileHandle file = file(save, area.areaX, area.areaZ); try { deflater.reset(); OutputStream stream = file.write(false, 8192); DeflaterOutputStream deflaterStream = new DeflaterOutputStream(stream, deflater); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(deflaterStream); DataOutputStream dataOutputStream = new DataOutputStream(bufferedOutputStream); area.writeSave(dataOutputStream, dataGroups); bufferedOutputStream.flush(); deflaterStream.finish(); stream.close(); } catch (Exception e) { Log.error("Failed to write area " + area.areaX + "," + area.areaZ, e); return false; } return true; }
private String deflateAndBase64(final String data) { try { final Deflater deflater = new Deflater(); deflater.setInput(data.getBytes(HttpConstants.UTF8_ENCODING)); 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 DatatypeConverter.printBase64Binary(output); } catch (final UnsupportedEncodingException e) { throw new RuntimeException("Cannot find encoding:" + HttpConstants.UTF8_ENCODING, e); } }
public static byte[] deflate(byte[] data, int level) throws Exception { Deflater deflater = getDef(level); if (deflater == null) throw new IllegalArgumentException("No deflate for level " + level + " !"); deflater.reset(); deflater.setInput(data); deflater.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); while (!deflater.finished()) { int i = deflater.deflate(buf.get()); bos.write(buf.get(), 0, i); } //Deflater::end is called the time when the process exits. return bos.toByteArray(); }
public static DeflateResult deflate(ByteBuffer input) { byte[] inputBuf; int inputOffset; int inputLength = input.remaining(); if (input.hasArray()) { inputBuf = input.array(); inputOffset = input.arrayOffset() + input.position(); input.position(input.limit()); } else { inputBuf = new byte[inputLength]; inputOffset = 0; input.get(inputBuf); } CRC32 crc32 = new CRC32(); crc32.update(inputBuf, inputOffset, inputLength); long crc32Value = crc32.getValue(); ByteArrayOutputStream out = new ByteArrayOutputStream(); Deflater deflater = new Deflater(9, true); deflater.setInput(inputBuf, inputOffset, inputLength); deflater.finish(); byte[] buf = new byte[65536]; while (!deflater.finished()) { int chunkSize = deflater.deflate(buf); out.write(buf, 0, chunkSize); } return new DeflateResult(inputLength, crc32Value, out.toByteArray()); }
/** * Compress a byte array with a given {@link Deflater}. * @param source The array to compress. * @param off The offset in {@code source}. * @param len The number of bytes to process in {@code source}. * @param defl The deflater to use. * @return The compressed array. */ public static byte[] deflate(byte[] source, int off, int len, Deflater defl) { defl.setInput(source, off, len); defl.finish(); byte[] buf = new byte[1024]; try (DirectByteArrayOutputStream bos = new DirectByteArrayOutputStream(1024)) { int n; while ((n = defl.deflate(buf)) > 0) bos.write(buf, 0, n); defl = null; return Arrays.copyOf(bos.toByteArray(), bos.size()); } catch (IOException e) { // Never happens with ByteArrayOutputStream return null; } }
/** * Deflate the given string via a {@link java.util.zip.Deflater}. * The result will be base64 encoded with {@link #UTF8_ENCODING}. * * @param data the data * @return base64 encoded string */ public static String deflate(final String data) { try { final Deflater deflater = new Deflater(); deflater.setInput(data.getBytes(UTF8_ENCODING)); 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 encodeBase64(output); } catch (final UnsupportedEncodingException e) { throw new RuntimeException("Cannot find encoding:" + UTF8_ENCODING, e); } }
private void releaseDeflater(Deflater def) { synchronized (deflaters) { if (inflaters.size() < MAX_FLATER) { def.reset(); deflaters.add(def); } else { def.end(); } } }
public SocketOutput(SocketMonitor socketMonitor) { super(socketMonitor); this.socketOutputStream = socketMonitor.getSocket().getOutputStream(); this.dataOutputStream = new NoCloseDataOutputStream(socketOutputStream); this.deflater = new Deflater(COMPRESSION_LEVEL, COMPRESSION_NOWRAP); this.uncompressedOutput = new DirectByteArrayOutputStream(); this.uncompressedDataOutput = new NoCloseDataOutputStream(uncompressedOutput); this.compressionOutput = new DirectByteArrayOutputStream(); }
public void open() { lobBlockSize = database.logger.getLobBlockSize(); cryptLobs = database.logger.cryptLobs; compressLobs = database.logger.propCompressLobs; if (compressLobs || cryptLobs) { int largeBufferBlockSize = largeLobBlockSize + 4 * 1024; inflater = new Inflater(); deflater = new Deflater(Deflater.BEST_SPEED); dataBuffer = new byte[largeBufferBlockSize]; } if (database.getType() == DatabaseType.DB_RES) { lobStore = new LobStoreInJar(database, lobBlockSize); } else if (database.getType() == DatabaseType.DB_FILE) { lobStore = new LobStoreRAFile(database, lobBlockSize); if (!database.isFilesReadOnly()) { byteBuffer = new byte[lobBlockSize]; initialiseLobSpace(); } } else { lobStore = new LobStoreMem(lobBlockSize); byteBuffer = new byte[lobBlockSize]; initialiseLobSpace(); } }
@Override public String getUserSig(String identifier, long expire)throws QCloudException { try { Security.addProvider(new BouncyCastleProvider()); Reader reader = new CharArrayReader(imConfig.getPrivateKey().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\":\"" + imConfig.getSdkAppId() +"\"," +"\"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:" + imConfig.getSdkAppId() + "\n" + "TLS.time:" + time + "\n" + "TLS.expire_after:" + expire +"\n"; //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= JSON.parseObject(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(); return new String(Base64Url.base64EncodeUrl(Arrays.copyOfRange(compressBytes,0,compressBytesLength))); }catch (Exception e) { throw new QCloudException(e); } }
private void btnBackupClicked() { String backuppath=txtpath.getText(); String Database =txtdatabase.getText(); String Password =txtpass.getText(); String user=txtusername.getText(); Backup b = new Backup(); try { if(txtusername.getText().equals("") || txtpath.getText().equals("") || txtdatabase.getText().equals("")) { ErrorMessage.display("Incomplete Details", "Please fill all the fields first"); } else { byte[] data = b.getData("localhost", "3306", user, Password, Database).getBytes(); File filedst = new File(backuppath+"\\"+Database+".zip"); FileOutputStream dest = new FileOutputStream(filedst); ZipOutputStream zip = new ZipOutputStream( new BufferedOutputStream(dest)); zip.setMethod(ZipOutputStream.DEFLATED); zip.setLevel(Deflater.BEST_COMPRESSION); zip.putNextEntry(new ZipEntry(Database+".sql")); zip.write(data); zip.close(); dest.close(); SuccessMessage.display("Database BackUp Wizard", "Back Up Successfully for Database: " + ""+Database+"\n"+"On Dated: "+date); } }catch (Exception ex){ ErrorMessage.display("Database BackUp Wizard", ex.getMessage()+" \nBack Up Failed for Database: "+Database+"\n "+"On Dated: "); } }
/** Compresses the specified byte range using the * specified compressionLevel (constants are defined in * java.util.zip.Deflater). */ public static byte[] compress(byte[] value, int offset, int length, int compressionLevel) { /* Create an expandable byte array to hold the compressed data. * You cannot use an array that's the same size as the orginal because * there is no guarantee that the compressed data will be smaller than * the uncompressed data. */ ByteArrayOutputStream bos = new ByteArrayOutputStream(length); Deflater compressor = new Deflater(); try { compressor.setLevel(compressionLevel); compressor.setInput(value, offset, length); compressor.finish(); // Compress the data final byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } } finally { compressor.end(); } return bos.toByteArray(); }
/** * Create a logout message for front channel logout. * * @param logoutRequest the logout request. * @return a front SAML logout message. */ public String createFrontChannelLogoutMessage(final LogoutRequest logoutRequest) { final String logoutMessage = this.logoutMessageBuilder.create(logoutRequest); final Deflater deflater = new Deflater(); deflater.setInput(logoutMessage.getBytes(ASCII)); deflater.finish(); final byte[] buffer = new byte[logoutMessage.length()]; final int resultSize = deflater.deflate(buffer); final byte[] output = new byte[resultSize]; System.arraycopy(buffer, 0, output, 0, resultSize); return Base64.encodeBase64String(output); }