Skip to content

Commit 154a202

Browse files
authored
Remove jzlib dependency (#772)
* Remove jzlib dependency * Document the Java 7 prerequisite
1 parent 830a39d commit 154a202

File tree

4 files changed

+29
-52
lines changed

4 files changed

+29
-52
lines changed

README.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ If you need something that is not included, it shouldn't be too hard to add (do
9595
http://ssh-comparison.quendi.de/comparison.html[SSH Implementation Comparison]
9696

9797
== Dependencies
98-
Java 6+. http://www.slf4j.org/download.html[slf4j] is required. http://www.bouncycastle.org/java.html[bouncycastle] is highly recommended and required for using some of the crypto algorithms. http://www.jcraft.com/jzlib/[jzlib] is required for using zlib compression.
98+
Java 7+. http://www.slf4j.org/download.html[slf4j] is required. http://www.bouncycastle.org/java.html[bouncycastle] is highly recommended and required for using some of the crypto algorithms.
9999

100100
== Reporting bugs
101101
Issue tracker: https://github.com/hierynomus/sshj/issues

build-publishing.gradle

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ dependencies {
2323
compile "org.slf4j:slf4j-api:1.7.7"
2424
compile "org.bouncycastle:bcprov-jdk15on:$bouncycastleVersion"
2525
compile "org.bouncycastle:bcpkix-jdk15on:$bouncycastleVersion"
26-
compile "com.jcraft:jzlib:1.1.3"
2726

2827
testCompile "junit:junit:4.11"
2928
testCompile "org.mockito:mockito-core:1.9.5"

build.gradle

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ dependencies {
4242
implementation "org.slf4j:slf4j-api:1.7.36"
4343
implementation "org.bouncycastle:bcprov-jdk15on:$bouncycastleVersion"
4444
implementation "org.bouncycastle:bcpkix-jdk15on:$bouncycastleVersion"
45-
implementation "com.jcraft:jzlib:1.1.3"
4645
implementation "com.hierynomus:asn-one:0.6.0"
4746

4847
implementation "net.i2p.crypto:eddsa:0.3.0"

src/main/java/net/schmizz/sshj/transport/compression/ZlibCompression.java

+28-49
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@
1515
*/
1616
package net.schmizz.sshj.transport.compression;
1717

18-
import com.jcraft.jzlib.Deflater;
19-
import com.jcraft.jzlib.GZIPException;
20-
import com.jcraft.jzlib.Inflater;
21-
import com.jcraft.jzlib.JZlib;
18+
import java.util.zip.DataFormatException;
19+
import java.util.zip.Deflater;
20+
import java.util.zip.Inflater;
21+
2222
import net.schmizz.sshj.common.Buffer;
2323
import net.schmizz.sshj.common.DisconnectReason;
24-
import net.schmizz.sshj.common.SSHRuntimeException;
2524
import net.schmizz.sshj.transport.TransportException;
25+
import net.schmizz.sshj.transport.compression.Compression;
2626

27-
/** ZLib based Compression. */
28-
public class ZlibCompression
29-
implements Compression {
27+
public class ZlibCompression implements Compression {
3028

3129
/** Named factory for the ZLib Compression. */
3230
public static class Factory
@@ -52,19 +50,15 @@ public String getName() {
5250

5351
@Override
5452
public void init(Mode mode) {
55-
try {
56-
switch (mode) {
57-
case DEFLATE:
58-
deflater = new Deflater(JZlib.Z_DEFAULT_COMPRESSION);
59-
break;
60-
case INFLATE:
61-
inflater = new Inflater();
62-
break;
63-
default:
64-
assert false;
65-
}
66-
} catch (GZIPException gze) {
67-
53+
switch (mode) {
54+
case DEFLATE:
55+
deflater = new Deflater(Deflater.DEFAULT_COMPRESSION);
56+
break;
57+
case INFLATE:
58+
inflater = new Inflater();
59+
break;
60+
default:
61+
assert false;
6862
}
6963
}
7064

@@ -75,43 +69,28 @@ public boolean isDelayed() {
7569

7670
@Override
7771
public void compress(Buffer buffer) {
78-
deflater.setNextIn(buffer.array());
79-
deflater.setNextInIndex(buffer.rpos());
80-
deflater.setAvailIn(buffer.available());
72+
deflater.setInput(buffer.array(), buffer.rpos(), buffer.available());
8173
buffer.wpos(buffer.rpos());
8274
do {
83-
deflater.setNextOut(tempBuf);
84-
deflater.setNextOutIndex(0);
85-
deflater.setAvailOut(BUF_SIZE);
86-
final int status = deflater.deflate(JZlib.Z_PARTIAL_FLUSH);
87-
if (status == JZlib.Z_OK) {
88-
buffer.putRawBytes(tempBuf, 0, BUF_SIZE - deflater.getAvailOut());
89-
} else {
90-
throw new SSHRuntimeException("compress: deflate returned " + status);
91-
}
92-
} while (deflater.getAvailOut() == 0);
75+
final int len = deflater.deflate(tempBuf, 0, BUF_SIZE, Deflater.SYNC_FLUSH);
76+
buffer.putRawBytes(tempBuf, 0, len);
77+
} while (!deflater.needsInput());
9378
}
9479

95-
9680
@Override
9781
public void uncompress(Buffer from, Buffer to)
9882
throws TransportException {
99-
inflater.setNextIn(from.array());
100-
inflater.setNextInIndex(from.rpos());
101-
inflater.setAvailIn(from.available());
83+
inflater.setInput(from.array(), from.rpos(), from.available());
10284
while (true) {
103-
inflater.setNextOut(tempBuf);
104-
inflater.setNextOutIndex(0);
105-
inflater.setAvailOut(BUF_SIZE);
106-
final int status = inflater.inflate(JZlib.Z_PARTIAL_FLUSH);
107-
switch (status) {
108-
case JZlib.Z_OK:
109-
to.putRawBytes(tempBuf, 0, BUF_SIZE - inflater.getAvailOut());
110-
break;
111-
case JZlib.Z_BUF_ERROR:
85+
try {
86+
int len = inflater.inflate(tempBuf, 0, BUF_SIZE);
87+
if(len > 0) {
88+
to.putRawBytes(tempBuf, 0, len);
89+
} else {
11290
return;
113-
default:
114-
throw new TransportException(DisconnectReason.COMPRESSION_ERROR, "uncompress: inflate returned " + status);
91+
}
92+
} catch (DataFormatException e) {
93+
throw new TransportException(DisconnectReason.COMPRESSION_ERROR, "uncompress: inflate returned " + e.getMessage());
11594
}
11695
}
11796
}

0 commit comments

Comments
 (0)