-
Notifications
You must be signed in to change notification settings - Fork 8.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
optimize: GzipUtil code style #2140
Conversation
Codecov Report
@@ Coverage Diff @@
## develop #2140 +/- ##
=============================================
- Coverage 54.37% 54.36% -0.01%
+ Complexity 2470 2469 -1
=============================================
Files 447 447
Lines 14742 14742
Branches 1734 1734
=============================================
- Hits 8016 8015 -1
+ Misses 5967 5966 -1
- Partials 759 761 +2
|
compressor/seata-compressor-gzip/src/main/java/io/seata/compressor/gzip/GzipUtil.java
Outdated
Show resolved
Hide resolved
compressor/seata-compressor-gzip/src/main/java/io/seata/compressor/gzip/GzipUtil.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left some comments.
@xingfudeshi public static byte[] compress(byte[] bytes) {
if (bytes == null) {
throw new NullPointerException("bytes is null");
} else {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Throwable var2 = null;
Object var5;
try {
GZIPOutputStream gzip = new GZIPOutputStream(out);
Throwable var4 = null;
try {
gzip.write(bytes);
gzip.flush();
gzip.finish();
var5 = out.toByteArray();
} catch (Throwable var30) {
var5 = var30;
var4 = var30;
throw var30;
} finally {
if (gzip != null) {
if (var4 != null) {
try {
gzip.close();
} catch (Throwable var29) {
var4.addSuppressed(var29);
}
} else {
gzip.close();
}
}
}
} catch (Throwable var32) {
var2 = var32;
throw var32;
} finally {
if (out != null) {
if (var2 != null) {
try {
out.close();
} catch (Throwable var28) {
var2.addSuppressed(var28);
}
} else {
out.close();
}
}
}
return (byte[])var5;
} catch (IOException var34) {
throw new RuntimeException("gzip compress error", var34);
}
}
} |
@xingfudeshi public static byte[] decompress(byte[] bytes) {
if (bytes == null) {
throw new NullPointerException("bytes is null");
} else {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Throwable var2 = null;
try {
GZIPInputStream gunzip = new GZIPInputStream(new ByteArrayInputStream(bytes));
Throwable var4 = null;
try {
byte[] buffer = new byte[8192];
int n;
while((n = gunzip.read(buffer)) > -1) {
out.write(buffer, 0, n);
}
byte[] var7 = out.toByteArray();
return var7;
} catch (Throwable var32) {
var4 = var32;
throw var32;
} finally {
if (gunzip != null) {
if (var4 != null) {
try {
gunzip.close();
} catch (Throwable var31) {
var4.addSuppressed(var31);
}
} else {
gunzip.close();
}
}
}
} catch (Throwable var34) {
var2 = var34;
throw var34;
} finally {
if (out != null) {
if (var2 != null) {
try {
out.close();
} catch (Throwable var30) {
var2.addSuppressed(var30);
}
} else {
out.close();
}
}
}
} catch (IOException var36) {
throw new RuntimeException("gzip decompress error", var36);
}
}
} |
What i mean is that the use of try with resource statement is redundant for ByteArrayOutputStream or ByteArrayInputStream.Because the method close does not do anything. |
Oh, Im sorry, I will change my code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
compressor/seata-compressor-gzip/src/main/java/io/seata/compressor/gzip/GzipUtil.java
Show resolved
Hide resolved
Codecov Report
@@ Coverage Diff @@
## develop #2140 +/- ##
=============================================
+ Coverage 54.2% 54.33% +0.13%
- Complexity 2469 2492 +23
=============================================
Files 447 454 +7
Lines 14723 14865 +142
Branches 1695 1749 +54
=============================================
+ Hits 7980 8077 +97
- Misses 5960 6018 +58
+ Partials 783 770 -13
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Ⅰ. Describe what this PR did
Ⅱ. Does this pull request fix one issue?
Ⅲ. Why don't you add test cases (unit test/integration test)?
Ⅳ. Describe how to verify it
Ⅴ. Special notes for reviews