-
Notifications
You must be signed in to change notification settings - Fork 124
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
Issue127 #128
Issue127 #128
Changes from 1 commit
f7f7be8
fee4dd7
f8220b3
f71753e
7c2f3e1
ffe145d
c156b66
8de3843
6431bcf
48a6ff2
1cc013c
af03841
b127caf
55e3ef3
636d0d1
77a878e
c7d1466
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,8 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
*/ | ||
|
||
package com.jcraft.jsch.jzlib; | ||
import java.util.function.Supplier; | ||
|
||
import com.jcraft.jsch.*; | ||
|
||
public class Compression implements com.jcraft.jsch.Compression { | ||
|
@@ -39,31 +41,26 @@ public class Compression implements com.jcraft.jsch.Compression { | |
private byte[] inflated_buf; | ||
private Session session; | ||
|
||
public Compression(){ | ||
public Compression() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we remove the added space character before the opening brace? It helps to reduce diffs when comparing this implementation with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
} | ||
|
||
@Override | ||
public void init(Session session, int type, int level) throws Exception{ | ||
this.session = session; | ||
public void init(int type, int level) throws Exception{ | ||
if(type==DEFLATER){ | ||
deflater=new Deflater(level); | ||
} | ||
else if(type==INFLATER){ | ||
inflater=new Inflater(); | ||
inflated_buf=new byte[BUF_SIZE]; | ||
} | ||
if(session.getLogger().isEnabled(Logger.DEBUG)){ | ||
session.getLogger().log(Logger.DEBUG, | ||
"zlib using "+this.getClass().getCanonicalName()); | ||
} | ||
Session.logMessage(session, Logger.DEBUG, () -> "zlib using "+this.getClass().getCanonicalName()); | ||
} | ||
|
||
@Override | ||
public byte[] compress(byte[] buf, int start, int[] len){ | ||
deflater.next_in=buf; | ||
deflater.next_in_index=start; | ||
deflater.avail_in=len[0]-start; | ||
int status; | ||
int outputlen=start; | ||
byte[] outputbuf=buf; | ||
int tmp=0; | ||
|
@@ -72,7 +69,7 @@ public byte[] compress(byte[] buf, int start, int[] len){ | |
deflater.next_out=tmpbuf; | ||
deflater.next_out_index=0; | ||
deflater.avail_out=BUF_SIZE; | ||
status=deflater.deflate(JZlib.Z_PARTIAL_FLUSH); | ||
int status=deflater.deflate(JZlib.Z_PARTIAL_FLUSH); | ||
switch(status){ | ||
case JZlib.Z_OK: | ||
tmp=BUF_SIZE-deflater.avail_out; | ||
|
@@ -85,11 +82,7 @@ public byte[] compress(byte[] buf, int start, int[] len){ | |
outputlen+=tmp; | ||
break; | ||
default: | ||
if(session.getLogger().isEnabled(Logger.WARN)){ | ||
session.getLogger().log(Logger.WARN, | ||
"compress: deflate returnd "+status); | ||
} | ||
|
||
Session.logMessage(session, Logger.WARN, () -> "compress: deflate returnd "+status); | ||
} | ||
} | ||
while(deflater.avail_out==0); | ||
|
@@ -140,11 +133,8 @@ public byte[] uncompress(byte[] buffer, int start, int[] length){ | |
length[0]=inflated_end; | ||
return buffer; | ||
default: | ||
if(session.getLogger().isEnabled(Logger.WARN)){ | ||
session.getLogger().log(Logger.WARN, | ||
"uncompress: inflate returnd "+status); | ||
} | ||
return null; | ||
Session.logMessage(session, Logger.WARN, () -> "compress: deflate returnd "+status); | ||
return null; | ||
} | ||
} | ||
} | ||
|
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'd rather go with the approach of adding a
default void init(int type, int level, Session session) {init(type, level);}
approach, and let actual implementations ofCompression
handle overriding it.We would then change the
Session
class to call the newinit(type, level, session)
method.Any existing implementations that users have developed that don't specialize this default implementation continue to work.
And since the implementations that specialize the new
void init(int type, int level, Session session)
method would then internally gain access to theSession
instance and could just call thesession.getLogger()
method for their internal logging needs.That would also allow us to avoid having to add the unwieldy static
logMessage()
method to theSession
class.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.
Is it no valid use case that the Compression classes are used independently, i.e. in an application that need ZLIB compression for something else? In that case your suggestion would break this application because there is no session instance and the logging attempt would lead to a NullPointerException. That's why I've created a logMessage-method that checks for the existence of the session and falls back to the static logger if it's not there.
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.
Users can create their own
Compression
implementations and then instruct JSch to use utilize them by callingJSch.setConfig("zlib@openssh.com", "my.own.CompressionImpl")
orsession.setConfig("zlib@openssh.com", "my.own.CompressionImpl")
.For the built-in implementations we provide (
com.jcraft.jsch.jzlib.Compression
&com.jcraft.jsch.juz.Compression
), there wouldn't be an NPE thrown becauseSession
would call the newinit(type, level, this);
(thus never callinginit(type, level)
).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.
That's not the use case I've mentioned. If someone is using zlib.Compression "directly" in their application they will use init(type, level) as it's the way it had to be done. With your suggested change that application stops working due to a NullPointerException. I wasn't talking about using own implementations of Compression or the JSch-internal use of it.
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.
The built-in implementations of
com.jcraft.jsch.jzlib.Compression
&com.jcraft.jsch.juz.Compression
aren't intended to be consumed outside of JSch, so I'm not especially worried if somebody got an NPE in that case, because I don't consider them part of the public API.In fact, in the
module-info.java
I constructed, these packages (com.jcraft.jsch.jzlib
&com.jcraft.jsch.juz
) aren't even exported, to further emphasize that point.I'm far more interested in allow users to continue to instruct JSch to use their own custom
Compression
implementations without having to adapt them after this change.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.
We can also introduce
null
guards in thecom.jcraft.jsch.jzlib.Compression
&com.jcraft.jsch.juz.Compression
for whensession
is dereferenced to avoid throwing an NPE, for anybody that does happening to utilize them whilst only callinginit(type, level)
.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've done the changes you've suggested including checking for null and fall back to the jsch logger if no particular session logger is specified.