本文整理汇总了Java中java.util.zip.Deflater.setInput方法的典型用法代码示例。如果您正苦于以下问题:Java Deflater.setInput方法的具体用法?Java Deflater.setInput怎么用?Java Deflater.setInput使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.zip.Deflater
的用法示例。
在下文中一共展示了Deflater.setInput方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: compressForZlib
import java.util.zip.Deflater; //导入方法依赖的package包/类
/**
* 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;
}
示例2: deflater
import java.util.zip.Deflater; //导入方法依赖的package包/类
/**
* 压缩.
*
* @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();
}
示例3: dumpFlate
import java.util.zip.Deflater; //导入方法依赖的package包/类
/** 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;
}
示例4: writeObject
import java.util.zip.Deflater; //导入方法依赖的package包/类
void writeObject(ObjectOutputStream out) throws IOException {
out.writeInt(bufSize);
out.writeBoolean(buffer != null);
if (buffer != null) {
Deflater compressor = new Deflater();
// for small buffers, the compressed size can be somewhat larger than the original
byte[] compressedBytes = new byte[bufSize + 32];
int compressedSize;
compressor.setInput(buffer,startPos,bufSize);
compressor.finish();
compressedSize = compressor.deflate(compressedBytes);
out.writeInt(compressedSize);
out.write(compressedBytes,0,compressedSize);
} else {
out.writeUTF(eventBufferFileName);
}
}
示例5: deflate
import java.util.zip.Deflater; //导入方法依赖的package包/类
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();
}
示例6: compress
import java.util.zip.Deflater; //导入方法依赖的package包/类
private static byte[] compress(byte[] data) throws IOException {
Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
deflater.setInput(data);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
deflater.finish();
byte[] buffer = new byte[1024];
while (!deflater.finished()) {
int count = deflater.deflate(buffer);
outputStream.write(buffer, 0, count);
}
outputStream.close();
byte[] output = outputStream.toByteArray();
Log.d("Compressor", "Original: " + data.length);
Log.d("Compressor", "Compressed: " + output.length);
return output;
}
示例7: compressBytes
import java.util.zip.Deflater; //导入方法依赖的package包/类
public static DeflaterInflaterData compressBytes(byte[] input) {
Deflater compresser = new Deflater();
compresser.setInput(input);
compresser.finish();
byte[] buffer = new byte[100];
byte[] result = new byte[0];
int compressedDataLength = 0;
int totalCompressedDataLength = 0;
do {
byte[] newResult = new byte[result.length + buffer.length];
System.arraycopy(result, 0, newResult, 0, result.length);
compressedDataLength = compresser.deflate(buffer);
totalCompressedDataLength += compressedDataLength;
// System.out.println(compressedDataLength);
// System.out.println("uc: b "+buffer.length);
// System.out.println("uc: r "+result.length);
// System.out.println("uc: nr "+newResult.length);
// System.out.println();
System.arraycopy(buffer, 0, newResult, result.length, buffer.length);
result = newResult;
} while (compressedDataLength != 0);
return new DeflaterInflaterData(totalCompressedDataLength, result);
}
示例8: compress
import java.util.zip.Deflater; //导入方法依赖的package包/类
/**
* 压缩
*
* @param data 待压缩的数据
* @return
*/
public static byte[] compress(byte[] data)
{
Deflater deflater = new Deflater();
deflater.setInput(data);
deflater.finish();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[BUFFER_LENGTH];
while (!deflater.finished())
{
int byteCount = deflater.deflate(buf);
baos.write(buf, 0, byteCount);
}
deflater.end();
byte[] compressedBytes = baos.toByteArray();
return compressedBytes;
}
示例9: storeData
import java.util.zip.Deflater; //导入方法依赖的package包/类
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);
}
}
示例10: deflateAndBase64
import java.util.zip.Deflater; //导入方法依赖的package包/类
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);
}
}
示例11: deflate
import java.util.zip.Deflater; //导入方法依赖的package包/类
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());
}
示例12: deflate
import java.util.zip.Deflater; //导入方法依赖的package包/类
/**
* 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);
}
}
示例13: dumpFlate
import java.util.zip.Deflater; //导入方法依赖的package包/类
/**
* 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;
}
示例14: binaryEncode
import java.util.zip.Deflater; //导入方法依赖的package包/类
private String binaryEncode(String tag) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Deflater deflater = new Deflater();
deflater.setInput(tag.getBytes());
deflater.finish();
byte[] buffer = new byte[1024];
while (!deflater.finished()) {
int count = deflater.deflate(buffer);
outputStream.write(buffer, 0, count);
}
outputStream.close();
byte compressed[] = outputStream.toByteArray();
return bytesToHex(compressed);
}
示例15: save
import java.util.zip.Deflater; //导入方法依赖的package包/类
public void save(DataOutputStream dos) throws IOException, OutOfMemoryError {
// todo [performance] profile memory use during the save operation
// there is ~80MB bytes used for byte[], for the length of uncompressed data ~20MB
Properties props = new Properties();
settings.store(props);
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest("save properties: --------------------------------------------------------------"); // NOI18N
LOGGER.finest(settings.debug());
LOGGER.finest("-------------------------------------------------------------------------------"); // NOI18N
}
ByteArrayOutputStream baos = new ByteArrayOutputStream(1000000); // ~1MB pre-allocated
DataOutputStream snapshotDataStream = new DataOutputStream(baos);
ByteArrayOutputStream baos2 = new ByteArrayOutputStream(10000); // ~10kB pre-allocated
DataOutputStream settingsDataStream = new DataOutputStream(baos2);
try {
snapshot.writeToStream(snapshotDataStream);
snapshotDataStream.flush();
props.store(settingsDataStream, ""); //NOI18N
settingsDataStream.flush();
byte[] snapshotBytes = baos.toByteArray();
byte[] compressedBytes = new byte[snapshotBytes.length];
Deflater d = new Deflater();
d.setInput(snapshotBytes);
d.finish();
int compressedLen = d.deflate(compressedBytes);
int uncompressedLen = snapshotBytes.length;
// binary file format:
// 1. magic number: "nbprofiler"
// 2. int type
// 3. int length of snapshot data size
// 4. snapshot data bytes
// 5. int length of settings data size
// 6. settings data bytes (.properties plain text file format)
// 7. String (UTF) custom comments
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest("save version:" + SNAPSHOT_FILE_VERSION_MAJOR //NOI18N
+ "." + SNAPSHOT_FILE_VERSION_MINOR); // NOI18N
LOGGER.finest("save type:" + getType()); // NOI18N
LOGGER.finest("length of uncompressed snapshot data:" + uncompressedLen); // NOI18N
LOGGER.finest("save length of snapshot data:" + compressedLen); // NOI18N
LOGGER.finest("length of settings data:" + baos2.size()); // NOI18N
}
dos.writeBytes(PROFILER_FILE_MAGIC_STRING); // 1. magic number: "nbprofiler"
dos.writeByte(SNAPSHOT_FILE_VERSION_MAJOR); // 2. file version
dos.writeByte(SNAPSHOT_FILE_VERSION_MINOR); // 3. file version
dos.writeInt(getType()); // 4. int type
dos.writeInt(compressedLen); // 5. int length of compressed snapshot data size
dos.writeInt(uncompressedLen); // 5. int length of compressed snapshot data size
dos.write(compressedBytes, 0, compressedLen); // 6. compressed snapshot data bytes
dos.writeInt(baos2.size()); // 7. int length of settings data size
dos.write(baos2.toByteArray()); // 8. settings data bytes (.properties plain text file format)
dos.writeUTF(userComments);
} catch (OutOfMemoryError e) {
baos = null;
snapshotDataStream = null;
baos2 = null;
settingsDataStream = null;
throw e;
} finally {
if (snapshotDataStream != null) {
snapshotDataStream.close();
}
if (settingsDataStream != null) {
settingsDataStream.close();
}
}
}