Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
16 changes: 4 additions & 12 deletions dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -786,11 +785,9 @@ public static byte[] base642bytes(final String str, final int off, final int len
*/
public static byte[] zip(byte[] bytes) throws IOException {
UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
OutputStream os = new DeflaterOutputStream(bos);
try {
try (DeflaterOutputStream os = new DeflaterOutputStream(bos)) {
os.write(bytes);
} finally {
os.close();
bos.close();
}
return bos.toByteArray();
Expand All @@ -804,16 +801,11 @@ public static byte[] zip(byte[] bytes) throws IOException {
* @throws IOException
*/
public static byte[] unzip(byte[] bytes) throws IOException {
UnsafeByteArrayInputStream bis = new UnsafeByteArrayInputStream(bytes);
UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
InputStream is = new InflaterInputStream(bis);
try {
try (UnsafeByteArrayInputStream bis = new UnsafeByteArrayInputStream(bytes);
UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
InflaterInputStream is = new InflaterInputStream(bis)) {
IOUtils.write(is, bos);
return bos.toByteArray();
} finally {
is.close();
bis.close();
bos.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,13 @@ public static String[] readLines(InputStream is) throws IOException {

public static String read(InputStream is, String encoding) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
InputStreamReader inputStreamReader = new InputStreamReader(is, encoding);
char[] buf = new char[1024];
int len;
while ((len = inputStreamReader.read(buf)) != -1) {
stringBuilder.append(buf, 0, len);
try (InputStreamReader inputStreamReader = new InputStreamReader(is, encoding)) {
char[] buf = new char[1024];
int len;
while ((len = inputStreamReader.read(buf)) != -1) {
stringBuilder.append(buf, 0, len);
}
}
inputStreamReader.close();
return stringBuilder.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,9 @@ private String generatePrivatePemKey(KeyPair keyPair) throws IOException {
private String generatePemKey(String type, byte[] content) throws IOException {
PemObject pemObject = new PemObject(type, content);
StringWriter str = new StringWriter();
JcaPEMWriter jcaPEMWriter = new JcaPEMWriter(str);
jcaPEMWriter.writeObject(pemObject);
jcaPEMWriter.close();
str.close();
try (JcaPEMWriter jcaPEMWriter = new JcaPEMWriter(str)) {
jcaPEMWriter.writeObject(pemObject);
}
return str.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,14 @@ public byte[] compress(byte[] payloadByteArr) throws RpcException {
return new byte[0];
}

ByteArrayOutputStream out = new ByteArrayOutputStream();
BZip2CompressorOutputStream cos;
try {
cos = new BZip2CompressorOutputStream(out);
cos.write(payloadByteArr);
cos.close();
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
try (BZip2CompressorOutputStream cos = new BZip2CompressorOutputStream(out)) {
cos.write(payloadByteArr);
}
return out.toByteArray();
} catch (Exception e) {
throw new IllegalStateException(e);
}

return out.toByteArray();
}

@Override
Expand All @@ -85,11 +82,10 @@ public byte[] decompress(byte[] payloadByteArr) {
return new byte[0];
}

ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(payloadByteArr);
try {
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(payloadByteArr);
BZip2CompressorInputStream unZip = new BZip2CompressorInputStream(in)) {
int totalBytesRead = 0;
BZip2CompressorInputStream unZip = new BZip2CompressorInputStream(in);
byte[] buffer = new byte[2048];
int n;
while ((n = unZip.read(buffer)) >= 0) {
Expand All @@ -100,9 +96,9 @@ public byte[] decompress(byte[] payloadByteArr) {
}
out.write(buffer, 0, n);
}
return out.toByteArray();
} catch (Exception e) {
throw new IllegalStateException(e);
}
return out.toByteArray();
}
}
Loading