Remove deprecated ZLib usage

This commit is contained in:
Jeroen van Erp
2017-06-26 09:56:37 +02:00
parent d43fc4551e
commit a51270791d
2 changed files with 38 additions and 31 deletions

View File

@@ -104,6 +104,8 @@ Google Group: http://groups.google.com/group/sshj-users
Fork away! Fork away!
== Release history == Release history
SSHJ 0.22.0 (2017-??-??)::
* Fixed https://github.com/hierynomus/sshj/issues/331[#331]: Added support for wildcards in known_hosts file
SSHJ 0.21.1 (2017-04-25):: SSHJ 0.21.1 (2017-04-25)::
* Merged https://github.com/hierynomus/sshj/pulls/322[#322]: Fix regression from 40f956b (invalid length parameter on outputstream) * Merged https://github.com/hierynomus/sshj/pulls/322[#322]: Fix regression from 40f956b (invalid length parameter on outputstream)
SSHJ 0.21.0 (2017-04-14):: SSHJ 0.21.0 (2017-04-14)::

View File

@@ -15,8 +15,10 @@
*/ */
package net.schmizz.sshj.transport.compression; package net.schmizz.sshj.transport.compression;
import com.jcraft.jzlib.Deflater;
import com.jcraft.jzlib.GZIPException;
import com.jcraft.jzlib.Inflater;
import com.jcraft.jzlib.JZlib; import com.jcraft.jzlib.JZlib;
import com.jcraft.jzlib.ZStream;
import net.schmizz.sshj.common.Buffer; import net.schmizz.sshj.common.Buffer;
import net.schmizz.sshj.common.DisconnectReason; import net.schmizz.sshj.common.DisconnectReason;
import net.schmizz.sshj.common.SSHRuntimeException; import net.schmizz.sshj.common.SSHRuntimeException;
@@ -45,20 +47,24 @@ public class ZlibCompression
private final byte[] tempBuf = new byte[BUF_SIZE]; private final byte[] tempBuf = new byte[BUF_SIZE];
private ZStream stream; private Deflater deflater;
private Inflater inflater;
@Override @Override
public void init(Mode mode) { public void init(Mode mode) {
stream = new ZStream(); try {
switch (mode) { switch (mode) {
case DEFLATE: case DEFLATE:
stream.deflateInit(JZlib.Z_DEFAULT_COMPRESSION); deflater = new Deflater(JZlib.Z_DEFAULT_COMPRESSION);
break; break;
case INFLATE: case INFLATE:
stream.inflateInit(); inflater = new Inflater();
break; break;
default: default:
assert false; assert false;
}
} catch (GZIPException gze) {
} }
} }
@@ -69,44 +75,43 @@ public class ZlibCompression
@Override @Override
public void compress(Buffer buffer) { public void compress(Buffer buffer) {
stream.next_in = buffer.array(); deflater.setNextIn(buffer.array());
stream.next_in_index = buffer.rpos(); deflater.setNextInIndex(buffer.rpos());
stream.avail_in = buffer.available(); deflater.setAvailIn(buffer.available());
buffer.wpos(buffer.rpos()); buffer.wpos(buffer.rpos());
do { do {
stream.next_out = tempBuf; deflater.setNextOut(tempBuf);
stream.next_out_index = 0; deflater.setNextOutIndex(0);
stream.avail_out = BUF_SIZE; deflater.setAvailOut(BUF_SIZE);
final int status = stream.deflate(JZlib.Z_PARTIAL_FLUSH); final int status = deflater.deflate(JZlib.Z_PARTIAL_FLUSH);
if (status == JZlib.Z_OK) { if (status == JZlib.Z_OK) {
buffer.putRawBytes(tempBuf, 0, BUF_SIZE - stream.avail_out); buffer.putRawBytes(tempBuf, 0, BUF_SIZE - deflater.getAvailOut());
} else { } else {
throw new SSHRuntimeException("compress: deflate returned " + status); throw new SSHRuntimeException("compress: deflate returned " + status);
} }
} while (stream.avail_out == 0); } while (deflater.getAvailOut() == 0);
} }
@Override @Override
public void uncompress(Buffer from, Buffer to) public void uncompress(Buffer from, Buffer to)
throws TransportException { throws TransportException {
stream.next_in = from.array(); inflater.setNextIn(from.array());
stream.next_in_index = from.rpos(); inflater.setNextInIndex(from.rpos());
stream.avail_in = from.available(); inflater.setAvailIn(from.available());
while (true) { while (true) {
stream.next_out = tempBuf; inflater.setNextOut(tempBuf);
stream.next_out_index = 0; inflater.setNextOutIndex(0);
stream.avail_out = BUF_SIZE; inflater.setAvailOut(BUF_SIZE);
final int status = stream.inflate(JZlib.Z_PARTIAL_FLUSH); final int status = inflater.inflate(JZlib.Z_PARTIAL_FLUSH);
switch (status) { switch (status) {
case JZlib.Z_OK: case JZlib.Z_OK:
to.putRawBytes(tempBuf, 0, BUF_SIZE - stream.avail_out); to.putRawBytes(tempBuf, 0, BUF_SIZE - inflater.getAvailOut());
break; break;
case JZlib.Z_BUF_ERROR: case JZlib.Z_BUF_ERROR:
return; return;
default: default:
throw new TransportException(DisconnectReason.COMPRESSION_ERROR, "uncompress: inflate returned " throw new TransportException(DisconnectReason.COMPRESSION_ERROR, "uncompress: inflate returned " + status);
+ status);
} }
} }
} }