Skip to content

Commit 28e08a8

Browse files
committed
speedup some array creation internals + receive context on methods
1 parent 7613880 commit 28e08a8

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

src/main/java/org/jruby/ext/openssl/SSLContext.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -469,18 +469,17 @@ private RubyArray matchedCiphers(final ThreadContext context) {
469469
final Collection<CipherStrings.Def> cipherDefs =
470470
CipherStrings.matchingCiphers(this.ciphers, supported, false);
471471

472-
final RubyArray cipherList = runtime.newArray( cipherDefs.size() );
473-
474-
for ( CipherStrings.Def def : cipherDefs ) {
475-
final RubyArray cipher = runtime.newArray(4);
476-
cipher.store(0, newUTF8String(runtime, def.name));
477-
cipher.store(1, newUTF8String(runtime, sslVersionString(def.algorithms)));
478-
cipher.store(2, runtime.newFixnum(def.algStrengthBits));
479-
cipher.store(3, runtime.newFixnum(def.algBits));
480-
481-
cipherList.append(cipher);
472+
final IRubyObject[] cipherList = new IRubyObject[ cipherDefs.size() ];
473+
474+
int i = 0; for ( CipherStrings.Def def : cipherDefs ) {
475+
cipherList[i++] = runtime.newArrayNoCopy(
476+
newUTF8String(runtime, def.name), // 0
477+
newUTF8String(runtime, sslVersionString(def.algorithms)), // 1
478+
runtime.newFixnum(def.algStrengthBits), // 2
479+
runtime.newFixnum(def.algBits) // 3
480+
);
482481
}
483-
return cipherList;
482+
return runtime.newArrayNoCopy(cipherList);
484483
}
485484
catch (GeneralSecurityException gse) {
486485
throw newSSLError(runtime, gse.getMessage());

src/main/java/org/jruby/ext/openssl/SSLSocket.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -927,14 +927,14 @@ public IRubyObject sysclose(final ThreadContext context) {
927927
}
928928

929929
@JRubyMethod
930-
public IRubyObject cert() {
931-
final Ruby runtime = getRuntime();
930+
public IRubyObject cert(final ThreadContext context) {
931+
final Ruby runtime = context.runtime;
932932
if ( engine == null ) return runtime.getNil();
933933

934934
try {
935935
Certificate[] cert = engine.getSession().getLocalCertificates();
936936
if ( cert != null && cert.length > 0 ) {
937-
return X509Cert.wrap(runtime, cert[0]);
937+
return X509Cert.wrap(context, cert[0]);
938938
}
939939
}
940940
catch (CertificateEncodingException e) {
@@ -943,15 +943,20 @@ public IRubyObject cert() {
943943
return runtime.getNil();
944944
}
945945

946+
// @Deprecated
947+
public final IRubyObject cert() {
948+
return cert(getRuntime().getCurrentContext());
949+
}
950+
946951
@JRubyMethod
947-
public IRubyObject peer_cert() {
948-
final Ruby runtime = getRuntime();
952+
public IRubyObject peer_cert(final ThreadContext context) {
953+
final Ruby runtime = context.runtime;
949954
if ( engine == null ) return runtime.getNil();
950955

951956
try {
952957
Certificate[] cert = engine.getSession().getPeerCertificates();
953958
if ( cert.length > 0 ) {
954-
return X509Cert.wrap(runtime, cert[0]);
959+
return X509Cert.wrap(context, cert[0]);
955960
}
956961
}
957962
catch (CertificateEncodingException e) {
@@ -965,18 +970,23 @@ public IRubyObject peer_cert() {
965970
return runtime.getNil();
966971
}
967972

973+
// @Deprecated
974+
public final IRubyObject peer_cert() {
975+
return peer_cert(getRuntime().getCurrentContext());
976+
}
977+
968978
@JRubyMethod
969-
public IRubyObject peer_cert_chain() {
970-
final Ruby runtime = getRuntime();
979+
public IRubyObject peer_cert_chain(final ThreadContext context) {
980+
final Ruby runtime = context.runtime;
971981
if ( engine == null ) return runtime.getNil();
972982

973983
try {
974984
javax.security.cert.Certificate[] certs = engine.getSession().getPeerCertificateChain();
975-
RubyArray arr = runtime.newArray(certs.length);
985+
IRubyObject[] cert_chain = new IRubyObject[ certs.length ];
976986
for ( int i = 0; i < certs.length; i++ ) {
977-
arr.append( X509Cert.wrap(runtime, certs[i]) );
987+
cert_chain[i] = X509Cert.wrap(context, certs[i]);
978988
}
979-
return arr;
989+
return runtime.newArrayNoCopy(cert_chain);
980990
}
981991
catch (javax.security.cert.CertificateEncodingException e) {
982992
throw X509Cert.newCertificateError(getRuntime(), e);
@@ -989,6 +999,11 @@ public IRubyObject peer_cert_chain() {
989999
return runtime.getNil();
9901000
}
9911001

1002+
// @Deprecated
1003+
public final IRubyObject peer_cert_chain() {
1004+
return peer_cert_chain(getRuntime().getCurrentContext());
1005+
}
1006+
9921007
@JRubyMethod
9931008
public IRubyObject cipher() {
9941009
if ( engine == null ) return getRuntime().getNil();

src/main/java/org/jruby/ext/openssl/X509Cert.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,23 @@ public static IRubyObject wrap(Ruby runtime, Certificate cert)
151151
return wrap(runtime.getCurrentContext(), cert.getEncoded());
152152
}
153153

154+
static X509Cert wrap(ThreadContext context, Certificate cert)
155+
throws CertificateEncodingException {
156+
return wrap(context, cert.getEncoded());
157+
}
158+
154159
// this is the javax.security counterpart of the previous wrap method
155160
public static IRubyObject wrap(Ruby runtime, javax.security.cert.Certificate cert)
156161
throws javax.security.cert.CertificateEncodingException {
157162
return wrap(runtime.getCurrentContext(), cert.getEncoded());
158163
}
159164

160-
static IRubyObject wrap(final ThreadContext context, final byte[] encoded) {
165+
static X509Cert wrap(ThreadContext context, javax.security.cert.Certificate cert)
166+
throws javax.security.cert.CertificateEncodingException {
167+
return wrap(context, cert.getEncoded());
168+
}
169+
170+
static X509Cert wrap(final ThreadContext context, final byte[] encoded) {
161171
//final Ruby runtime = context.runtime;
162172
//final RubyString enc = StringHelper.newString(runtime, encoded);
163173
//return _Certificate(runtime).callMethod(context, "new", enc);

0 commit comments

Comments
 (0)