Skip to content

Commit 3d60286

Browse files
committed
[refactor] internal BIO impl - for less cast-ing and/or byte copy-ing
1 parent 6108a9d commit 3d60286

2 files changed

Lines changed: 27 additions & 15 deletions

File tree

src/main/java/org/jruby/ext/openssl/impl/BIO.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.io.IOException;
3131
import java.io.InputStream;
3232
import java.io.OutputStream;
33+
import java.io.UnsupportedEncodingException;
3334
import java.security.MessageDigest;
3435
import javax.crypto.Cipher;
3536

@@ -202,13 +203,12 @@ public void flush() throws IOException, PKCS7Exception {
202203

203204
private final static byte[] CONTENT_TEXT;
204205
static {
205-
byte[] val = null;
206206
try {
207-
val = "Content-Type: text/plain\r\n\r\n".getBytes("ISO8859-1");
208-
} catch(Exception e) {
209-
val = null;
207+
CONTENT_TEXT = "Content-Type: text/plain\r\n\r\n".getBytes("ISO8859-1");
208+
}
209+
catch (UnsupportedEncodingException ex) {
210+
throw new AssertionError(ex);
210211
}
211-
CONTENT_TEXT = val;
212212
}
213213

214214
/** c: SMIME_crlf_copy
@@ -342,4 +342,9 @@ public String toString() {
342342
String[] names = getClass().getName().split("\\.");
343343
return "#<BIO:" + names[names.length-1] + " next=" + next() + ">";
344344
}
345+
346+
public byte[] toBytes() {
347+
throw new UnsupportedOperationException("toBytes() for " + getClass().getName());
348+
}
349+
345350
}// BIO

src/main/java/org/jruby/ext/openssl/impl/MemBIO.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ private void realloc() {
4646
}
4747

4848
@Override
49-
public int gets(byte[] in, int len) throws IOException {
50-
if(rpointer == slen) {
51-
return 0;
52-
}
49+
public int gets(byte[] in, int len) {
50+
if (rpointer == slen) return 0;
5351

5452
int i=0;
5553
for(;i<len && rpointer<slen; i++, rpointer++) {
@@ -90,11 +88,7 @@ public int write(byte[] out, int offset, int len) throws IOException {
9088

9189
@Override
9290
public String toString() {
93-
try {
94-
return "<MemBIO w:" + wpointer + " r:" + rpointer + " buf:\"" + new String(buffer,rpointer,slen-rpointer) + "\" next=" + next() + ">";
95-
} catch(Exception e) {}
96-
97-
return null;
91+
return "<MemBIO w:" + wpointer + " r:" + rpointer + " buf:\"" + new String(buffer,rpointer,slen-rpointer) + "\" next=" + next() + ">";
9892
}
9993

10094
@Override
@@ -105,12 +99,25 @@ public int getType() {
10599
return TYPE_MEM;
106100
}
107101

108-
public byte[] getMemCopy() {
102+
@Override
103+
public byte[] toBytes() {
109104
byte[] nbuf = new byte[slen];
110105
System.arraycopy(buffer, 0, nbuf, 0, slen);
111106
return nbuf;
112107
}
113108

109+
public byte[] getMemCopy() {
110+
return toBytes();
111+
}
112+
113+
public byte[] getBuffer() {
114+
return buffer;
115+
}
116+
117+
public int length() {
118+
return slen;
119+
}
120+
114121
public void reset() {
115122
this.rpointer = 0;
116123
}

0 commit comments

Comments
 (0)