本文整理汇总了Java中java.util.zip.DeflaterOutputStream.finish方法的典型用法代码示例。如果您正苦于以下问题:Java DeflaterOutputStream.finish方法的具体用法?Java DeflaterOutputStream.finish怎么用?Java DeflaterOutputStream.finish使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.zip.DeflaterOutputStream
的用法示例。
在下文中一共展示了DeflaterOutputStream.finish方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: compressString
import java.util.zip.DeflaterOutputStream; //导入方法依赖的package包/类
public static String compressString(String s) throws Exception
{
if(s == null)
{
return "";
}
ByteArrayOutputStream ba = new ByteArrayOutputStream(s.length() * 2 + 20);
DeflaterOutputStream out = new DeflaterOutputStream(ba);
byte[] bytes = s.getBytes(CHARSET_UTF8);
out.write(bytes);
out.finish();
out.flush();
byte[] compressed = ba.toByteArray();
return Hex.toHexString(compressed);
}
示例2: compress
import java.util.zip.DeflaterOutputStream; //导入方法依赖的package包/类
static private byte[] compress(byte[] source) throws IOException
{
// ByteArrayInputStream in = new ByteArrayInputStream(source);
// ByteArrayOutputStream bous = new ByteArrayOutputStream();
// DeflaterOutputStream out = new DeflaterOutputStream(bous);
// byte[] buffer = new byte[1024];
// int len;
// while((len = in.read(buffer)) > 0) {
// out.write(buffer, 0, len);
// }
// byte[] ret = bous.toByteArray();
//
// bous.flush();
// in.close();
// out.close();
// return ret;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Deflater deflater = new Deflater(Deflater.DEFLATED);
DeflaterOutputStream deflaterStream = new DeflaterOutputStream(baos, deflater);
deflaterStream.write(source);
deflaterStream.finish();
return baos.toByteArray();
}
示例3: compressDeflate
import java.util.zip.DeflaterOutputStream; //导入方法依赖的package包/类
private static byte[] compressDeflate(byte[] data)
{
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream(500);
DeflaterOutputStream compresser = new DeflaterOutputStream(bout);
compresser.write(data, 0, data.length);
compresser.finish();
compresser.flush();
return bout.toByteArray();
}
catch (IOException ex) {
AssertionError ae = new AssertionError("IOException while writing to ByteArrayOutputStream!");
ae.initCause(ex);
throw ae;
}
}
示例4: deflateAndBase64Encode
import java.util.zip.DeflaterOutputStream; //导入方法依赖的package包/类
/**
* DEFLATE (RFC1951) compresses the given SAML message.
*
* @param message SAML message
*
* @return DEFLATE compressed message
*
* @throws MessageEncodingException thrown if there is a problem compressing the message
*/
protected String deflateAndBase64Encode(SAMLObject message) throws MessageEncodingException {
log.debug("Deflating and Base64 encoding SAML message");
try {
String messageStr = SerializeSupport.nodeToString(marshallMessage(message));
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
Deflater deflater = new Deflater(Deflater.DEFLATED, true);
DeflaterOutputStream deflaterStream = new DeflaterOutputStream(bytesOut, deflater);
deflaterStream.write(messageStr.getBytes("UTF-8"));
deflaterStream.finish();
return Base64Support.encode(bytesOut.toByteArray(), Base64Support.UNCHUNKED);
} catch (IOException e) {
throw new MessageEncodingException("Unable to DEFLATE and Base64 encode SAML message", e);
}
}
示例5: testNowrap
import java.util.zip.DeflaterOutputStream; //导入方法依赖的package包/类
@Test
public void testNowrap() throws IOException {
// Recompress with nowrap set to false.
Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, false /* nowrap */);
ByteArrayOutputStream compressedContentBuffer = new ByteArrayOutputStream();
DeflaterOutputStream deflateOut = new DeflaterOutputStream(compressedContentBuffer, deflater);
deflateOut.write(CONTENT);
deflateOut.finish();
deflateOut.close();
deflater.end();
compressedContent = compressedContentBuffer.toByteArray();
compressedContentIn = new ByteArrayInputStream(compressedContent);
// Now expect wrapped content in the uncompressor, and uncompressing should "just work".
uncompressor.setNowrap(false);
uncompressor.uncompress(compressedContentIn, uncompressedContentOut);
assertTrue(Arrays.equals(CONTENT, uncompressedContentOut.toByteArray()));
}
示例6: a
import java.util.zip.DeflaterOutputStream; //导入方法依赖的package包/类
public static byte[] a(byte[] bArr) {
if (bArr == null) {
return null;
}
OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream);
try {
deflaterOutputStream.write(bArr, 0, bArr.length);
deflaterOutputStream.finish();
deflaterOutputStream.flush();
deflaterOutputStream.close();
return byteArrayOutputStream.toByteArray();
} catch (Exception e) {
return null;
}
}
示例7: deflateAndBase64Encode
import java.util.zip.DeflaterOutputStream; //导入方法依赖的package包/类
/**
* DEFLATE (RFC1951) compresses the given SAML message.
*
* @param message SAML message
*
* @return DEFLATE compressed message
*
* @throws MessageEncodingException thrown if there is a problem compressing the message
*/
protected String deflateAndBase64Encode(SAMLObject message) throws MessageEncodingException {
log.debug("Deflating and Base64 encoding SAML message");
try {
String messageStr = XMLHelper.nodeToString(marshallMessage(message));
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
Deflater deflater = new Deflater(Deflater.DEFLATED, true);
DeflaterOutputStream deflaterStream = new DeflaterOutputStream(bytesOut, deflater);
deflaterStream.write(messageStr.getBytes("UTF-8"));
deflaterStream.finish();
return Base64.encodeBytes(bytesOut.toByteArray(), Base64.DONT_BREAK_LINES);
} catch (IOException e) {
throw new MessageEncodingException("Unable to DEFLATE and Base64 encode SAML message", e);
}
}
示例8: compress
import java.util.zip.DeflaterOutputStream; //导入方法依赖的package包/类
public static byte[] compress(final byte[] src, final int level) {
byte[] result = src;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(src.length);
java.util.zip.Deflater defeater = new java.util.zip.Deflater(level);
DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, defeater);
try {
deflaterOutputStream.write(src);
deflaterOutputStream.finish();
deflaterOutputStream.close();
result = byteArrayOutputStream.toByteArray();
} catch (IOException e) {
defeater.end();
e.printStackTrace();
} finally {
try {
byteArrayOutputStream.close();
} catch (IOException ignored) {
}
defeater.end();
}
return result;
}
示例9: compress
import java.util.zip.DeflaterOutputStream; //导入方法依赖的package包/类
public static byte[] compress(final byte[] src, final int level) throws IOException {
byte[] result = src;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(src.length);
java.util.zip.Deflater defeater = new java.util.zip.Deflater(level);
DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, defeater);
try {
deflaterOutputStream.write(src);
deflaterOutputStream.finish();
deflaterOutputStream.close();
result = byteArrayOutputStream.toByteArray();
} catch (IOException e) {
defeater.end();
throw e;
} finally {
try {
byteArrayOutputStream.close();
} catch (IOException ignored) {
}
defeater.end();
}
return result;
}
示例10: compress
import java.util.zip.DeflaterOutputStream; //导入方法依赖的package包/类
/**
* Description 压缩某个字节数组
*
* @param src
* @param level
* @return
* @throws IOException
*/
public static byte[] compress(final byte[] src, final int level) throws IOException {
byte[] result = src;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(src.length);
java.util.zip.Deflater defeater = new java.util.zip.Deflater(level);
DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, defeater);
try {
deflaterOutputStream.write(src);
deflaterOutputStream.finish();
deflaterOutputStream.close();
result = byteArrayOutputStream.toByteArray();
} catch (IOException e) {
defeater.end();
throw e;
} finally {
try {
byteArrayOutputStream.close();
} catch (IOException ignored) {
}
defeater.end();
}
return result;
}
示例11: deflateBytes
import java.util.zip.DeflaterOutputStream; //导入方法依赖的package包/类
private ByteArrayOutputStream deflateBytes(ByteArrayOutputStream baos) throws IOException {
ByteArrayOutputStream deflatedBytes = new ByteArrayOutputStream();
Deflater deflater = new Deflater(Deflater.DEFLATED, true);
DeflaterOutputStream deflaterStream = new DeflaterOutputStream(deflatedBytes, deflater);
deflaterStream.write(baos.toByteArray());
deflaterStream.finish();
return deflatedBytes;
}
示例12: compress
import java.util.zip.DeflaterOutputStream; //导入方法依赖的package包/类
@Override
public void compress(InputStream uncompressedIn, OutputStream compressedOut) throws IOException {
byte[] buffer = new byte[inputBufferSize];
DeflaterOutputStream deflaterOut =
new DeflaterOutputStream(compressedOut, createOrResetDeflater(), outputBufferSize);
int numRead = 0;
while ((numRead = uncompressedIn.read(buffer)) >= 0) {
deflaterOut.write(buffer, 0, numRead);
}
deflaterOut.finish();
deflaterOut.flush();
}
示例13: setUp
import java.util.zip.DeflaterOutputStream; //导入方法依赖的package包/类
@Before
public void setUp() throws IOException {
ByteArrayOutputStream compressedContentBuffer = new ByteArrayOutputStream();
Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
DeflaterOutputStream deflateOut = new DeflaterOutputStream(compressedContentBuffer, deflater);
deflateOut.write(CONTENT);
deflateOut.finish();
deflateOut.close();
deflater.end();
compressedContent = compressedContentBuffer.toByteArray();
uncompressor = new DeflateUncompressor();
compressedContentIn = new ByteArrayInputStream(compressedContent);
uncompressedContentOut = new ByteArrayOutputStream();
}
示例14: write
import java.util.zip.DeflaterOutputStream; //导入方法依赖的package包/类
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;
}
示例15: writeZTXT
import java.util.zip.DeflaterOutputStream; //导入方法依赖的package包/类
private void writeZTXT() throws IOException
{
if (param.isCompressedTextSet())
{
String[] text = param.getCompressedText();
for (int i = 0; i < text.length / 2; i++)
{
byte[] keyword = text[2 * i].getBytes();
byte[] value = text[2 * i + 1].getBytes();
ChunkStream cs = new ChunkStream("zTXt");
cs.write(keyword, 0, Math.min(keyword.length, 79));
cs.write(0);
cs.write(0);
DeflaterOutputStream dos = new DeflaterOutputStream(cs,
new Deflater(Deflater.BEST_COMPRESSION, true));
dos.write(value);
dos.finish();
cs.writeToStream(dataOutput);
cs.close();
}
}
}