Skip to content

Commit

Permalink
Avoid increasing the sequence of sub header twice during flushing the…
Browse files Browse the repository at this point in the history
… compressed packet
  • Loading branch information
onlyacat committed Jan 5, 2024
1 parent 12369df commit d6778d4
Showing 1 changed file with 9 additions and 4 deletions.
Expand Up @@ -75,6 +75,7 @@ public class CompressPacketOutputStream extends AbstractPacketOutputStream {
private static final float MIN_COMPRESSION_RATIO = 0.9f;
private final byte[] header = new byte[7];
private final byte[] subHeader = new byte[4];
private boolean subHeaderIsGenerated = false;
private int maxPacketLength = MAX_PACKET_LENGTH;
private int compressSeqNo;
private byte[] remainingData = new byte[0];
Expand Down Expand Up @@ -161,6 +162,7 @@ protected void flushBuffer(boolean commandEnd) throws IOException {
subHeader[1] = (byte) (pos >>> 8);
subHeader[2] = (byte) (pos >>> 16);
subHeader[3] = (byte) this.seqNo++;
subHeaderIsGenerated = true;
deflater.write(subHeader, 0, 4);
deflater.write(buf, 0, uncompressSize - (remainingData.length + 4));
deflater.finish();
Expand Down Expand Up @@ -272,10 +274,13 @@ protected void flushBuffer(boolean commandEnd) throws IOException {
if (remainingData.length != 0) {
out.write(remainingData);
}
subHeader[0] = (byte) pos;
subHeader[1] = (byte) (pos >>> 8);
subHeader[2] = (byte) (pos >>> 16);
subHeader[3] = (byte) this.seqNo++;
// Avoid generating the sub header twice
if (!subHeaderIsGenerated){
subHeader[0] = (byte) pos;
subHeader[1] = (byte) (pos >>> 8);
subHeader[2] = (byte) (pos >>> 16);
subHeader[3] = (byte) this.seqNo++;
}
out.write(subHeader, 0, 4);
out.write(buf, 0, uncompressSize - (remainingData.length + 4));
cmdLength += remainingData.length;
Expand Down

0 comments on commit d6778d4

Please sign in to comment.