java.util.zip.GZIPOutputStream.finish()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(7.9k)|赞(0)|评价(0)|浏览(786)

本文整理了Java中java.util.zip.GZIPOutputStream.finish()方法的一些代码示例,展示了GZIPOutputStream.finish()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。GZIPOutputStream.finish()方法的具体详情如下:
包路径:java.util.zip.GZIPOutputStream
类名称:GZIPOutputStream
方法名:finish

GZIPOutputStream.finish介绍

[英]Indicates to the stream that all data has been written out, and any GZIP terminal data can now be written.
[中]向流指示所有数据都已写入,现在可以写入任何GZIP终端数据。

代码示例

代码示例来源:origin: weibocom/motan

public static byte[] gzip(byte[] data) throws IOException {
  ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
  GZIPOutputStream gzip = null;
  try {
    gzip = new GZIPOutputStream(bos);
    gzip.write(data);
    gzip.finish();
    return bos.toByteArray();
  } finally {
    if (gzip != null) {
      gzip.close();
    }
  }
}

代码示例来源:origin: weibocom/motan

public byte[] compress(byte[] org, boolean useGzip, int minGzSize) throws IOException {
  if (useGzip && org.length > minGzSize) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    GZIPOutputStream gos = new GZIPOutputStream(outputStream);
    gos.write(org);
    gos.finish();
    gos.flush();
    gos.close();
    byte[] ret = outputStream.toByteArray();
    return ret;
  } else {
    return org;
  }
}

代码示例来源:origin: jwtk/jjwt

protected byte[] doCompress(byte[] payload) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    GZIPOutputStream compressorOutputStream = new GZIPOutputStream(outputStream, true);
    try {
      compressorOutputStream.write(payload, 0, payload.length);
      compressorOutputStream.finish();
      return outputStream.toByteArray();
    } finally {
      Objects.nullSafeClose(compressorOutputStream, outputStream);
    }
  }
}

代码示例来源:origin: gocd/gocd

public static byte[] encodeMessage(Message msg) {
  String encode = gson.toJson(msg);
  try {
    try (ByteArrayOutputStream bytes = new ByteArrayOutputStream()) {
      try (GZIPOutputStream out = new GZIPOutputStream(bytes)) {
        out.write(encode.getBytes(StandardCharsets.UTF_8));
        out.finish();
      }
      return bytes.toByteArray();
    }
  } catch (IOException e) {
    throw bomb(e);
  }
}

代码示例来源:origin: Netflix/eureka

public Value(String payload) {
  this.payload = payload;
  if (!EMPTY_PAYLOAD.equals(payload)) {
    Stopwatch tracer = compressPayloadTimer.start();
    try {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      GZIPOutputStream out = new GZIPOutputStream(bos);
      byte[] rawBytes = payload.getBytes();
      out.write(rawBytes);
      // Finish creation of gzip file
      out.finish();
      out.close();
      bos.close();
      gzipped = bos.toByteArray();
    } catch (IOException e) {
      gzipped = null;
    } finally {
      if (tracer != null) {
        tracer.stop();
      }
    }
  } else {
    gzipped = null;
  }
}

代码示例来源:origin: jMonkeyEngine/jmonkeyengine

public void writeObject(ByteBuffer buffer, Object object) throws IOException {
    if (!(object instanceof GZIPCompressedMessage)) return;
    Message message = ((GZIPCompressedMessage)object).getMessage();

    ByteBuffer tempBuffer = ByteBuffer.allocate(512000);
    Serializer.writeClassAndObject(tempBuffer, message);

    ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutput = new GZIPOutputStream(byteArrayOutput);

    tempBuffer.flip();
    gzipOutput.write(tempBuffer.array(), 0, tempBuffer.limit());
    gzipOutput.flush();
    gzipOutput.finish();
    gzipOutput.close();

    buffer.put(byteArrayOutput.toByteArray());
  }
}

代码示例来源:origin: igniterealtime/Openfire

private static byte[] getJavaScriptContent(boolean compress) throws IOException
{
  StringWriter writer = new StringWriter();
  for(String file : getJavascriptFiles()) {
    writer.write(getJavaScriptFile(file));
  }
  
  if (compress) {
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
      try (GZIPOutputStream gzos = new GZIPOutputStream(baos)) {
        gzos.write(writer.toString().getBytes());
        gzos.finish();
        gzos.flush();
        return baos.toByteArray();
      }
    }
  }
  else {
    return writer.toString().getBytes();
  }
}

代码示例来源:origin: NanoHttpd/nanohttpd

private void sendBodyWithCorrectEncoding(OutputStream outputStream, long pending) throws IOException {
  if (useGzipWhenAccepted()) {
    GZIPOutputStream gzipOutputStream = null;
    try {
      gzipOutputStream = new GZIPOutputStream(outputStream);
    } catch (Exception e) {
      if (this.data != null) {
        this.data.close();
      }
    }
    if (gzipOutputStream != null) {
      sendBody(gzipOutputStream, -1);
      gzipOutputStream.finish();
    }
  } else {
    sendBody(outputStream, pending);
  }
}

代码示例来源:origin: Tencent/tinker

diffOut.flush();
GZIPOutputStream bzip2Out = new GZIPOutputStream(diffOut);
DataOutputStream dataOut = new DataOutputStream(bzip2Out);
bzip2Out.finish();
bzip2Out = new GZIPOutputStream(diffOut);
bzip2Out.write(diffBlock, 0, diffBLockLen);
bzip2Out.finish();
bzip2Out.flush();
int diffBlockLen = diffOut.size() - ctrlBlockLen - BSUtil.HEADER_SIZE;
bzip2Out = new GZIPOutputStream(diffOut);
bzip2Out.write(extraBlock, 0, extraBlockLen);
bzip2Out.finish();
bzip2Out.flush();

代码示例来源:origin: osmandapp/Osmand

ous.flush();
if(gzip){
  GZIPOutputStream gous = new GZIPOutputStream(ous, 1024);
  Algorithms.streamCopy(bis, gous);
  gous.flush();
  gous.finish();
} else {
  Algorithms.streamCopy(bis, ous);

代码示例来源:origin: geoserver/geoserver

gzipOut = new GZIPOutputStream(output);
output = gzipOut;
  gzipOut.finish();
  gzipOut.flush();

代码示例来源:origin: com.alibaba/fastjson

ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
  if (bytes.length < 512) {
    gzipOut = new GZIPOutputStream(byteOut, bytes.length);
  } else {
    gzipOut = new GZIPOutputStream(byteOut);
  gzipOut.finish();
  out.writeByteArray(byteOut.toByteArray());
} catch (IOException ex) {

代码示例来源:origin: pentaho/pentaho-kettle

gzipOutputStream = new GZIPOutputStream( socketOut, 50000 );
 bufferedOutputStream = new BufferedOutputStream( gzipOutputStream, bufferSize );
} else {
 gzipOutputStream.finish();
} else {
 outputStream.flush();

代码示例来源:origin: rackerlabs/blueflood

public synchronized byte[] gzip(InputStream input) throws IOException {
    Timer.Context ctx = timer.time();
    try {
      gzippedPayload = new ByteArrayOutputStream();
      gZCompressor = new GZIPOutputStream(gzippedPayload);
      while ((nbRead = input.read(buf)) != -1) {
        gZCompressor.write(buf, 0, nbRead);
      }

      input.close();

      gZCompressor.finish();
      gZCompressor.close();

      return gzippedPayload.toByteArray();
    } finally {
      ctx.stop();
    }
  }
}

代码示例来源:origin: apache/cloudstack

public static ByteBuffer doCompress(ByteBuffer buffer, int length) {
  ByteArrayOutputStream byteOut = new ByteArrayOutputStream(length);
  byte[] array;
  if (buffer.hasArray()) {
    array = buffer.array();
  } else {
    array = new byte[buffer.capacity()];
    buffer.get(array);
  }
  try {
    GZIPOutputStream out = new GZIPOutputStream(byteOut, length);
    out.write(array);
    out.finish();
    out.close();
  } catch (IOException e) {
    s_logger.error("Fail to compress the request!", e);
  }
  return ByteBuffer.wrap(byteOut.toByteArray());
}

代码示例来源:origin: openmrs/openmrs-core

GZIPOutputStream gzipstream = new GZIPOutputStream(compressedContent);
byte[] bytes = baos.toByteArray();
gzipstream.write(bytes);
gzipstream.finish();
gzipstream.finish();

代码示例来源:origin: com.google.gwt/gwt-servlet

try {
 output = new ByteArrayOutputStream(responseBytes.length);
 gzipOutputStream = new GZIPOutputStream(output);
 gzipOutputStream.write(responseBytes);
 gzipOutputStream.finish();
 gzipOutputStream.flush();
 setGzipEncodingHeader(response);

代码示例来源:origin: aws-amplify/aws-sdk-android

private InputStream getGzippedInputStream(String str) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gos = new GZIPOutputStream(baos);
    gos.write(str.getBytes(StringUtils.UTF8));
    gos.finish();
    byte[] content = baos.toByteArray();
    gos.close();

    return new ByteArrayInputStream(content);
  }
}

代码示例来源:origin: resteasy/Resteasy

GZIPOutputStream gzipstream = new GZIPOutputStream(compressedContent);
gzipstream.write(stringWriter.toString().getBytes());
gzipstream.finish();

代码示例来源:origin: jamesagnew/hapi-fhir

@Override
public void interceptRequest(IHttpRequest theRequestInterface) {
  HttpRequestBase theRequest = ((ApacheHttpRequest) theRequestInterface).getApacheRequest();
  if (theRequest instanceof HttpEntityEnclosingRequest) {
    Header[] encodingHeaders = theRequest.getHeaders(Constants.HEADER_CONTENT_ENCODING);
    if (encodingHeaders == null || encodingHeaders.length == 0) {
      HttpEntityEnclosingRequest req = (HttpEntityEnclosingRequest)theRequest;
      
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      GZIPOutputStream gos;
      try {
        gos = new GZIPOutputStream(bos);
        req.getEntity().writeTo(gos);
        gos.finish();
      } catch (IOException e) {
        ourLog.warn("Failed to GZip outgoing content", e);
        return;
      }
      
      byte[] byteArray = bos.toByteArray();
      ByteArrayEntity newEntity = new ByteArrayEntity(byteArray);
      req.setEntity(newEntity);
      req.addHeader(Constants.HEADER_CONTENT_ENCODING, "gzip");
    }
  }
  
}

相关文章