Apache Solr open-source search software
SOLR:10779: JavaBinCodec should use close consistently rather than having marshal() and close() call finish() (which closes the underlying stream)
Erick64093d6df10a
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 82e203381ea..c65db372f82 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -277,6 +277,9 @@ Other Changes* SOLR-10800: Factor out HttpShardHandler.transformReplicasToShardUrls from HttpShardHandler.prepDistributed.(Domenico Fabio Marino, Christine Poerschke)+* SOLR-10779: JavaBinCodec should use close consistently rather than having marshal() and close() call finish()+ (which closes the underlying stream). (Erick Erickson)+================== 6.7.0 ==================Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release.diff --git a/solr/core/src/java/org/apache/solr/response/BinaryResponseWriter.java b/solr/core/src/java/org/apache/solr/response/BinaryResponseWriter.javaindex 11c60740cb0..b841a030966 100644--- a/solr/core/src/java/org/apache/solr/response/BinaryResponseWriter.java+++ b/solr/core/src/java/org/apache/solr/response/BinaryResponseWriter.java@@ -48,7 +48,9 @@ public class BinaryResponseWriter implements BinaryQueryResponseWriter {public void write(OutputStream out, SolrQueryRequest req, SolrQueryResponse response) throws IOException {Resolver resolver = new Resolver(req, response.getReturnFields());if (req.getParams().getBool(CommonParams.OMIT_HEADER, false)) response.removeResponseHeader();- new JavaBinCodec(resolver).setWritableDocFields(resolver).marshal(response.getValues(), out);+ try (JavaBinCodec jbc = new JavaBinCodec(resolver)) {+ jbc.setWritableDocFields(resolver).marshal(response.getValues(), out);+ }}@Override@@ -160,10 +162,14 @@ public class BinaryResponseWriter implements BinaryQueryResponseWriter {Resolver resolver = new Resolver(req, rsp.getReturnFields());ByteArrayOutputStream out = new ByteArrayOutputStream();- new JavaBinCodec(resolver).setWritableDocFields(resolver).marshal(rsp.getValues(), out);+ try (JavaBinCodec jbc = new JavaBinCodec(resolver)) {+ jbc.setWritableDocFields(resolver).marshal(rsp.getValues(), out);+ }InputStream in = out.toInputStream();- return (NamedList<Object>) new JavaBinCodec(resolver).unmarshal(in);+ try (JavaBinCodec jbc = new JavaBinCodec(resolver)) {+ return (NamedList<Object>) jbc.unmarshal(in);+ }}catch (Exception ex) {throw new RuntimeException(ex);diff --git a/solr/core/src/java/org/apache/solr/search/CursorMark.java b/solr/core/src/java/org/apache/solr/search/CursorMark.javaindex 38fced567a9..2a63da53255 100644--- a/solr/core/src/java/org/apache/solr/search/CursorMark.java+++ b/solr/core/src/java/org/apache/solr/search/CursorMark.java@@ -183,9 +183,8 @@ public final class CursorMark {List<Object> pieces = null;try {final byte[] rawData = Base64.base64ToByteArray(serialized);- ByteArrayInputStream in = new ByteArrayInputStream(rawData);- try {- pieces = (List<Object>) new JavaBinCodec().unmarshal(in);+ try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayInputStream in = new ByteArrayInputStream(rawData)){+ pieces = (List<Object>) jbc.unmarshal(in);boolean b = false;for (Object o : pieces) {if (o instanceof BytesRefBuilder || o instanceof BytesRef || o instanceof String) {@@ -196,8 +195,6 @@ public final class CursorMark {in.reset();pieces = (List<Object>) new JavaBinCodec().unmarshal(in);}- } finally {- in.close();}} catch (Exception ex) {throw new SolrException(ErrorCode.BAD_REQUEST,@@ -259,19 +256,13 @@ public final class CursorMark {// the type/name/dir from the SortFields (or a hashCode to act as a checksum)// could help provide more validation beyond just the number of clauses.- try {- ByteArrayOutputStream out = new ByteArrayOutputStream(256);- try {- new JavaBinCodec().marshal(marshalledValues, out);- byte[] rawData = out.toByteArray();- return Base64.byteArrayToBase64(rawData, 0, rawData.length);- } finally {- out.close();- }+ try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayOutputStream out = new ByteArrayOutputStream(256)) {+ jbc.marshal(marshalledValues, out);+ byte[] rawData = out.toByteArray();+ return Base64.byteArrayToBase64(rawData, 0, rawData.length);} catch (Exception ex) {throw new SolrException(ErrorCode.SERVER_ERROR,"Unable to format search after totem", ex);-}}diff --git a/solr/core/src/test/org/apache/solr/response/TestBinaryResponseWriter.java b/solr/core/src/test/org/apache/solr/response/TestBinaryResponseWriter.javaindex 05bf1534167..ae85d52cbfb 100644--- a/solr/core/src/test/org/apache/solr/response/TestBinaryResponseWriter.java+++ b/solr/core/src/test/org/apache/solr/response/TestBinaryResponseWriter.java@@ -60,7 +60,10 @@ public class TestBinaryResponseWriter extends AbstractSolrTestCase {BinaryQueryResponseWriter writer = (BinaryQueryResponseWriter) h.getCore().getQueryResponseWriter("javabin");ByteArrayOutputStream baos = new ByteArrayOutputStream();writer.write(baos, req, rsp);- NamedList res = (NamedList) new JavaBinCodec().unmarshal(new ByteArrayInputStream(baos.toByteArray()));+ NamedList res;+ try (JavaBinCodec jbc = new JavaBinCodec()) {+ res = (NamedList) jbc.unmarshal(new ByteArrayInputStream(baos.toByteArray()));+ }SolrDocumentList docs = (SolrDocumentList) res.get("response");for (Object doc : docs) {SolrDocument document = (SolrDocument) doc;diff --git a/solr/core/src/test/org/apache/solr/response/TestJavabinTupleStreamParser.java b/solr/core/src/test/org/apache/solr/response/TestJavabinTupleStreamParser.javaindex d71044603ea..265231407c2 100644--- a/solr/core/src/test/org/apache/solr/response/TestJavabinTupleStreamParser.java+++ b/solr/core/src/test/org/apache/solr/response/TestJavabinTupleStreamParser.java@@ -169,15 +169,19 @@ public class TestJavabinTupleStreamParser extends SolrTestCaseJ4 {public void testSolrDocumentList() throws IOException {SolrQueryResponse response = new SolrQueryResponse();SolrDocumentList l = constructSolrDocList(response);- ByteArrayOutputStream baos = new ByteArrayOutputStream();- new JavaBinCodec().marshal(response.getValues(), baos);+ try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {+ jbc.marshal(response.getValues(), baos);+ }byte[] bytes = serialize(response.getValues());- Object o = new JavaBinCodec().unmarshal(new ByteArrayInputStream(bytes));+ try (JavaBinCodec jbc = new JavaBinCodec()) {+ jbc.unmarshal(new ByteArrayInputStream(bytes));+ }List list = new ArrayList<>();Map m = null;- JavabinTupleStreamParser parser = new JavabinTupleStreamParser(new ByteArrayInputStream(bytes), false);- while ((m = parser.next()) != null) {- list.add(m);+ try (JavabinTupleStreamParser parser = new JavabinTupleStreamParser(new ByteArrayInputStream(bytes), false)) {+ while ((m = parser.next()) != null) {+ list.add(m);+ }}assertEquals(l.size(), list.size());for(int i =0;i<list.size();i++){@@ -189,7 +193,9 @@ public class TestJavabinTupleStreamParser extends SolrTestCaseJ4 {SolrQueryResponse response = new SolrQueryResponse();response.getValues().add("results", o);ByteArrayOutputStream baos = new ByteArrayOutputStream();- new JavaBinCodec().marshal(response.getValues(), baos);+ try (JavaBinCodec jbc = new JavaBinCodec()) {+ jbc.marshal(response.getValues(), baos);+ }return baos.toByteArray();}}diff --git a/solr/core/src/test/org/apache/solr/response/TestPushWriter.java b/solr/core/src/test/org/apache/solr/response/TestPushWriter.javaindex 043fe92db7a..9a02ee95fdc 100644--- a/solr/core/src/test/org/apache/solr/response/TestPushWriter.java+++ b/solr/core/src/test/org/apache/solr/response/TestPushWriter.java@@ -53,8 +53,12 @@ public class TestPushWriter extends SolrTestCaseJ4 {log.info(new String(baos.toByteArray(), "UTF-8"));Map m = (Map) Utils.fromJSON(baos.toByteArray());checkValues(m);- writeData(new JavaBinCodec(baos= new ByteArrayOutputStream(), null));- m = (Map) new JavaBinCodec().unmarshal(new ByteArrayInputStream(baos.toByteArray()));+ try (JavaBinCodec jbc = new JavaBinCodec(baos= new ByteArrayOutputStream(), null)) {+ writeData(jbc);+ try (JavaBinCodec jbcUn = new JavaBinCodec()) {+ m = (Map) jbcUn.unmarshal(new ByteArrayInputStream(baos.toByteArray()));+ }+ }checkValues(m);}diff --git a/solr/core/src/test/org/apache/solr/response/transform/TestSubQueryTransformer.java b/solr/core/src/test/org/apache/solr/response/transform/TestSubQueryTransformer.javaindex e9af1cff704..a9e5fa923de 100644--- a/solr/core/src/test/org/apache/solr/response/transform/TestSubQueryTransformer.java+++ b/solr/core/src/test/org/apache/solr/response/transform/TestSubQueryTransformer.java@@ -372,28 +372,27 @@ public class TestSubQueryTransformer extends SolrTestCaseJ4 {johnTwoFL.setParams(params);final NamedList<Object> unmarshalled;- {- SolrCore core = johnTwoFL.getCore();- SolrQueryResponse rsp = new SolrQueryResponse();- SolrRequestInfo.setRequestInfo(new SolrRequestInfo(johnTwoFL, rsp));-+ SolrCore core = johnTwoFL.getCore();+ SolrQueryResponse rsp = new SolrQueryResponse();+ SolrRequestInfo.setRequestInfo(new SolrRequestInfo(johnTwoFL, rsp));+SolrQueryResponse response = h.queryAndResponse(johnTwoFL.getParams().get(CommonParams.QT), johnTwoFL);-+BinaryQueryResponseWriter responseWriter = (BinaryQueryResponseWriter) core.getQueryResponseWriter(johnTwoFL);ByteArrayOutputStream bytes = new ByteArrayOutputStream();- responseWriter.write(bytes,johnTwoFL,response);-- unmarshalled = (NamedList<Object>) new JavaBinCodec().unmarshal(- new ByteArrayInputStream(bytes.toByteArray()));-- johnTwoFL.close();- SolrRequestInfo.clearRequestInfo();+ responseWriter.write(bytes, johnTwoFL, response);++ try (JavaBinCodec jbc = new JavaBinCodec()) {+ unmarshalled = (NamedList<Object>) jbc.unmarshal(+ new ByteArrayInputStream(bytes.toByteArray()));}++ johnTwoFL.close();+ SolrRequestInfo.clearRequestInfo();SolrDocumentList resultDocs = (SolrDocumentList)(unmarshalled.get("response"));- {Map<String,String> engText = new HashMap<>();engText.put("text_t", "These guys develop stuff");@@ -415,7 +414,6 @@ public class TestSubQueryTransformer extends SolrTestCaseJ4 {}}}- }}@Testdiff --git a/solr/core/src/test/org/apache/solr/update/AddBlockUpdateTest.java b/solr/core/src/test/org/apache/solr/update/AddBlockUpdateTest.javaindex 9c791c202c7..cbd91c6da83 100644--- a/solr/core/src/test/org/apache/solr/update/AddBlockUpdateTest.java+++ b/solr/core/src/test/org/apache/solr/update/AddBlockUpdateTest.java@@ -412,11 +412,15 @@ public class AddBlockUpdateTest extends SolrTestCaseJ4 {}ByteArrayOutputStream os = new ByteArrayOutputStream();- new JavaBinCodec().marshal(topDocument, os);+ try (JavaBinCodec jbc = new JavaBinCodec()) {+ jbc.marshal(topDocument, os);+ }byte[] buffer = os.toByteArray();//now read the Object back- InputStream is = new ByteArrayInputStream(buffer);- SolrInputDocument result = (SolrInputDocument) new JavaBinCodec().unmarshal(is);+ SolrInputDocument result;+ try (JavaBinCodec jbc = new JavaBinCodec(); InputStream is = new ByteArrayInputStream(buffer)) {+ result = (SolrInputDocument) jbc.unmarshal(is);+ }assertEquals(2, result.size());assertEquals("v1", result.getFieldValue("parent_f1"));assertEquals("v2", result.getFieldValue("parent_f2"));diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/StreamingBinaryResponseParser.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/StreamingBinaryResponseParser.javaindex 14be1f31a86..5f88672a7be 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/StreamingBinaryResponseParser.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/StreamingBinaryResponseParser.java@@ -45,8 +45,7 @@ public class StreamingBinaryResponseParser extends BinaryResponseParser {@Overridepublic NamedList<Object> processResponse(InputStream body, String encoding) {- try {- JavaBinCodec codec = new JavaBinCodec() {+ try (JavaBinCodec codec = new JavaBinCodec() {@Overridepublic SolrDocument readSolrDocument(DataInputInputStream dis) throws IOException {@@ -80,7 +79,7 @@ public class StreamingBinaryResponseParser extends BinaryResponseParser {}return solrDocs;}- };+ };) {return (NamedList<Object>) codec.unmarshal(body);}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/JavaBinUpdateRequestCodec.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/JavaBinUpdateRequestCodec.javaindex afa746e96a4..5759a6c3be2 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/request/JavaBinUpdateRequestCodec.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/JavaBinUpdateRequestCodec.java@@ -87,8 +87,9 @@ public class JavaBinUpdateRequestCodec {}nl.add("docs", docIter);}- JavaBinCodec codec = new JavaBinCodec();- codec.marshal(nl, os);+ try (JavaBinCodec codec = new JavaBinCodec()) {+ codec.marshal(nl, os);+ }}/**@@ -110,7 +111,7 @@ public class JavaBinUpdateRequestCodec {Map<String,Map<String,Object>> delByIdMap;List<String> delByQ;final NamedList[] namedList = new NamedList[1];- JavaBinCodec codec = new JavaBinCodec() {+ try (JavaBinCodec codec = new JavaBinCodec() {// NOTE: this only works because this is an anonymous inner class// which will only ever be used on a single stream -- if this class@@ -189,9 +190,10 @@ public class JavaBinUpdateRequestCodec {return Collections.EMPTY_LIST;}- };+ };) {- codec.unmarshal(is);+ codec.unmarshal(is);+ }// NOTE: if the update request contains only delete commands the params// must be loaded nowdiff --git a/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java b/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.javaindex a2400f39794..8f86e4cac11 100644--- a/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java+++ b/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java@@ -150,11 +150,12 @@ public class JavaBinCodec implements PushWriter {}public void marshal(Object nl, OutputStream os) throws IOException {- initWrite(os);try {+ initWrite(os);writeVal(nl);} finally {- finish();+ alreadyMarshalled = true;+ daos.flushBuffer();}}@@ -164,11 +165,6 @@ public class JavaBinCodec implements PushWriter {daos.writeByte(VERSION);}- protected void finish() throws IOException {- closed = true;- daos.flushBuffer();- alreadyMarshalled = true;- }/** expert: sets a new output stream */public void init(FastOutputStream os) {@@ -1199,11 +1195,10 @@ public class JavaBinCodec implements PushWriter {}}- private boolean closed;-@Overridepublic void close() throws IOException {- if (closed) return;- finish();+ if (daos != null) {+ daos.flushBuffer();+ }}}diff --git a/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.java b/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.javaindex 16ace15cee0..e60d8c74b90 100644--- a/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.java+++ b/solr/solrj/src/test/org/apache/solr/common/util/TestJavaBinCodec.java@@ -55,11 +55,13 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {public void testStrings() throws Exception {for (int i = 0; i < 10000 * RANDOM_MULTIPLIER; i++) {String s = TestUtil.randomUnicodeString(random());- ByteArrayOutputStream os = new ByteArrayOutputStream();- new JavaBinCodec().marshal(s, os);- ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());- Object o = new JavaBinCodec().unmarshal(is);- assertEquals(s, o);+ try (JavaBinCodec jbcO = new JavaBinCodec(); ByteArrayOutputStream os = new ByteArrayOutputStream()) {+ jbcO.marshal(s, os);+ try (JavaBinCodec jbcI = new JavaBinCodec(); ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray())) {+ Object o = jbcI.unmarshal(is);+ assertEquals(s, o);+ }+ }}}@@ -165,14 +167,13 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {@Testpublic void testBackCompat() throws IOException {- JavaBinCodec javabin = new JavaBinCodec(){+ try (InputStream is = getClass().getResourceAsStream(SOLRJ_JAVABIN_BACKCOMPAT_BIN); JavaBinCodec javabin = new JavaBinCodec(){@Overridepublic List<Object> readIterator(DataInputInputStream fis) throws IOException {return super.readIterator(fis);}- };- try {- InputStream is = getClass().getResourceAsStream(SOLRJ_JAVABIN_BACKCOMPAT_BIN);+ };)+ {List<Object> unmarshaledObj = (List<Object>) javabin.unmarshal(is);List<Object> matchObj = generateAllDataTypes();compareObjects(unmarshaledObj, matchObj);@@ -207,13 +208,13 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {@Testpublic void testBackCompatForSolrDocumentWithChildDocs() throws IOException {- JavaBinCodec javabin = new JavaBinCodec(){+ try (JavaBinCodec javabin = new JavaBinCodec(){@Overridepublic List<Object> readIterator(DataInputInputStream fis) throws IOException {return super.readIterator(fis);}- };- try {+ };)+ {InputStream is = getClass().getResourceAsStream(SOLRJ_JAVABIN_BACKCOMPAT_BIN_CHILD_DOCS);SolrDocument sdoc = (SolrDocument) javabin.unmarshal(is);SolrDocument matchSolrDoc = generateSolrDocumentWithChildDocs();@@ -225,34 +226,30 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {@Testpublic void testForwardCompat() throws IOException {- JavaBinCodec javabin = new JavaBinCodec();- ByteArrayOutputStream os = new ByteArrayOutputStream();+ try (JavaBinCodec javabin = new JavaBinCodec(); ByteArrayOutputStream os = new ByteArrayOutputStream()) {- Object data = generateAllDataTypes();- try {- javabin.marshal(data, os);- byte[] newFormatBytes = os.toByteArray();+ Object data = generateAllDataTypes();+ try {+ javabin.marshal(data, os);+ byte[] newFormatBytes = os.toByteArray();- InputStream is = getClass().getResourceAsStream(SOLRJ_JAVABIN_BACKCOMPAT_BIN);- byte[] currentFormatBytes = IOUtils.toByteArray(is);+ InputStream is = getClass().getResourceAsStream(SOLRJ_JAVABIN_BACKCOMPAT_BIN);+ byte[] currentFormatBytes = IOUtils.toByteArray(is);- for (int i = 1; i < currentFormatBytes.length; i++) {//ignore the first byte. It is version information- assertEquals(newFormatBytes[i], currentFormatBytes[i]);- }+ for (int i = 1; i < currentFormatBytes.length; i++) {//ignore the first byte. It is version information+ assertEquals(newFormatBytes[i], currentFormatBytes[i]);+ }- } catch (IOException e) {- throw e;+ } catch (IOException e) {+ throw e;+ }}-}@Testpublic void testForwardCompatForSolrDocumentWithChildDocs() throws IOException {- JavaBinCodec javabin = new JavaBinCodec();- ByteArrayOutputStream os = new ByteArrayOutputStream();-SolrDocument sdoc = generateSolrDocumentWithChildDocs();- try {+ try (JavaBinCodec javabin = new JavaBinCodec(); ByteArrayOutputStream os = new ByteArrayOutputStream()) {javabin.marshal(sdoc, os);byte[] newFormatBytes = os.toByteArray();@@ -262,11 +259,9 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {for (int i = 1; i < currentFormatBytes.length; i++) {//ignore the first byte. It is version informationassertEquals(newFormatBytes[i], currentFormatBytes[i]);}-} catch (IOException e) {throw e;}-}@Test@@ -283,14 +278,16 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {return getObject(getBytes(o));}private static byte[] getBytes(Object o) throws IOException {- JavaBinCodec javabin = new JavaBinCodec();- ByteArrayOutputStream baos = new ByteArrayOutputStream();- javabin.marshal(o, baos);- return baos.toByteArray();+ try (JavaBinCodec javabin = new JavaBinCodec(); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {+ javabin.marshal(o, baos);+ return baos.toByteArray();+ }}private static Object getObject(byte[] bytes) throws IOException {- return new JavaBinCodec().unmarshal(new ByteArrayInputStream(bytes));+ try (JavaBinCodec jbc = new JavaBinCodec()) {+ return jbc.unmarshal(new ByteArrayInputStream(bytes));+ }}@@ -343,10 +340,15 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {JavaBinCodec.StringCache stringCache = new JavaBinCodec.StringCache(new MapBackedCache<>(new HashMap<>()));- m1 = (Map) new JavaBinCodec(null, stringCache).unmarshal(new ByteArrayInputStream(b1));- m2 = (Map) new JavaBinCodec(null, stringCache).unmarshal(new ByteArrayInputStream(b2));- l1 = new ArrayList<>(m1.keySet());- l2 = new ArrayList<>(m2.keySet());+ try (JavaBinCodec c1 = new JavaBinCodec(null, stringCache);+ JavaBinCodec c2 = new JavaBinCodec(null, stringCache)) {++ m1 = (Map) c1.unmarshal(new ByteArrayInputStream(b1));+ m2 = (Map) c2.unmarshal(new ByteArrayInputStream(b2));++ l1 = new ArrayList<>(m1.keySet());+ l2 = new ArrayList<>(m2.keySet());+ }assertTrue(l1.get(0).equals(l2.get(0)));assertTrue(l1.get(0) == l2.get(0));assertTrue(l1.get(1).equals(l2.get(1)));@@ -556,11 +558,12 @@ public class TestJavaBinCodec extends SolrTestCaseJ4 {while (--iter >= 0) {if (++bufnum >= buffers.length) bufnum = 0;byte[] buf = buffers[bufnum];- JavaBinCodec javabin = new JavaBinCodec(null, stringCache);- FastInputStream in = new FastInputStream(empty, buf, 0, buf.length);- Object o = javabin.unmarshal( in );- if (o instanceof SolrDocument) {- ret += ((SolrDocument) o).size();+ try (JavaBinCodec javabin = new JavaBinCodec(null, stringCache)) {+ FastInputStream in = new FastInputStream(empty, buf, 0, buf.length);+ Object o = javabin.unmarshal(in);+ if (o instanceof SolrDocument) {+ ret += ((SolrDocument) o).size();+ }}}return ret;diff --git a/solr/solrj/src/test/org/apache/solr/common/util/TestNamedListCodec.java b/solr/solrj/src/test/org/apache/solr/common/util/TestNamedListCodec.javaindex ae2a03645db..5637ec5dbd6 100644--- a/solr/solrj/src/test/org/apache/solr/common/util/TestNamedListCodec.java+++ b/solr/solrj/src/test/org/apache/solr/common/util/TestNamedListCodec.java@@ -31,7 +31,7 @@ import java.util.HashMap;public class TestNamedListCodec extends LuceneTestCase {public void testSimple() throws Exception{- ByteArrayOutputStream baos = new ByteArrayOutputStream();+NamedList nl = new NamedList();Float fval = new Float( 10.01f );Boolean bval = Boolean.TRUE;@@ -74,10 +74,14 @@ public class TestNamedListCodec extends LuceneTestCase {list.add(doc);nl.add("zzz",doc);-- new JavaBinCodec(null).marshal(nl,baos);- byte[] arr = baos.toByteArray();- nl = (NamedList) new JavaBinCodec().unmarshal(new ByteArrayInputStream(arr));+ byte[] arr;+ try (JavaBinCodec jbc = new JavaBinCodec(null); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {+ jbc.marshal(nl, baos);+ arr = baos.toByteArray();+ }+ try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayInputStream bais = new ByteArrayInputStream(arr)) {+ nl = (NamedList) jbc.unmarshal(bais);+ }assertEquals(3, nl.size());@@ -89,7 +93,7 @@ public class TestNamedListCodec extends LuceneTestCase {}public void testIterator() throws Exception{- ByteArrayOutputStream baos = new ByteArrayOutputStream();+NamedList nl = new NamedList();Float fval = new Float( 10.01f );Boolean bval = Boolean.TRUE;@@ -114,17 +118,21 @@ public class TestNamedListCodec extends LuceneTestCase {list.add(doc);nl.add("zzz",list.iterator());-- new JavaBinCodec(null).marshal(nl,baos);- byte[] arr = baos.toByteArray();- nl = (NamedList) new JavaBinCodec().unmarshal(new ByteArrayInputStream(arr));+ byte[] arr;+ try (JavaBinCodec jbc = new JavaBinCodec(null); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {+ jbc.marshal(nl, baos);+ arr = baos.toByteArray();+ }+ try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayInputStream bais = new ByteArrayInputStream(arr)) {+ nl = (NamedList) jbc.unmarshal(bais);+ }List l = (List) nl.get("zzz");assertEquals(list.size(), l.size());}public void testIterable() throws Exception {- ByteArrayOutputStream baos = new ByteArrayOutputStream();+NamedList r = new NamedList();@@ -137,11 +145,14 @@ public class TestNamedListCodec extends LuceneTestCase {r.add("more", "less");r.add("values", map.values());r.add("finally", "the end");- new JavaBinCodec(null).marshal(r,baos);- byte[] arr = baos.toByteArray();+ byte[] arr;+ try (JavaBinCodec jbc = new JavaBinCodec(null); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {+ jbc.marshal(r,baos);+ arr = baos.toByteArray();+ }- try {- NamedList result = (NamedList) new JavaBinCodec().unmarshal(new ByteArrayInputStream(arr));+ try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayInputStream bais = new ByteArrayInputStream(arr)) {+ NamedList result = (NamedList) jbc.unmarshal(bais);assertTrue("result is null and it shouldn't be", result != null);List keys = (List) result.get("keys");assertTrue("keys is null and it shouldn't be", keys != null);@@ -247,13 +258,16 @@ public class TestNamedListCodec extends LuceneTestCase {for (int i=0; i<10000; i++) { // pump up the iterations for good stress testingnl = rNamedList(3);- ByteArrayOutputStream baos = new ByteArrayOutputStream();- new JavaBinCodec(null).marshal(nl,baos);- byte[] arr = baos.toByteArray();+ byte[] arr;+ try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {+ jbc.marshal(nl, baos);+ arr = baos.toByteArray();+ }// System.out.println(arr.length);- res = (NamedList) new JavaBinCodec().unmarshal(new ByteArrayInputStream(arr));- cmp = BaseDistributedSearchTestCase.compare(nl, res, 0, null);-+ try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayInputStream bais = new ByteArrayInputStream(arr)) {+ res = (NamedList) jbc.unmarshal(bais);+ cmp = BaseDistributedSearchTestCase.compare(nl, res, 0, null);+ }if (cmp != null) {System.out.println(nl);System.out.println(res);@@ -261,5 +275,4 @@ public class TestNamedListCodec extends LuceneTestCase {}}}-}