Apache Lucene open-source search software
SOLR-902 -- FastInputStream#read(byte b[], int off, int len) gives incorrect results when amount left to read is less than buffer size
Shalin Shekhar Mangarfd89aab62f1e
diff --git a/CHANGES.txt b/CHANGES.txtindex 906eb314129..5af4badd941 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -118,7 +118,7 @@ New Featuresoptimized distributed faceting refinement by lowering parsing overhead andby making requests and responses smaller.-25. SOLR-876: WordDelimiterFilter now supports a splitOnNumerics+25. SOLR-876: WordDelimiterFilter now supports a splitOnNumericsoption, as well as a list of protected terms.(Dan Rosher via hossman)@@ -147,7 +147,7 @@ Bug Fixes2. SOLR-771: CoreAdminHandler STATUS should display 'normalized' paths (koji, hossman, shalin)- 3. SOLR-532: WordDelimiterFilter now respects payloads and other attributes of the original Token by+ 3. SOLR-532: WordDelimiterFilter now respects payloads and other attributes of the original Token byusing Token.clone() (Tricia Williams, gsingers)4. SOLR-805: DisMax queries are not being cached in QueryResultCache (Todd Feak via koji)@@ -193,7 +193,7 @@ Bug Fixes19. SOLR-802: Fix a potential null pointer error in the distributed FacetComponent(David Bowen via ryan)-+20. SOLR-346: Use perl regex to improve accuracy of finding latest snapshot in snapinstaller (billa)21. SOLR-830: Use perl regex to improve accuracy of finding latest snapshot in snappuller (billa)@@ -202,6 +202,9 @@ Bug Fixes23. SOLR-925: Fixed highlighting on fields with multiValued="true" and termOffsets="true" (koji)+24. SOLR-902: FastInputStream#read(byte b[], int off, int len) gives incorrect results when amount left to read is less+ than buffer size (Noble Paul via shalin)+Other Changes----------------------diff --git a/src/common/org/apache/solr/common/util/FastInputStream.java b/src/common/org/apache/solr/common/util/FastInputStream.javaindex 3abddc54797..56442f665ab 100755--- a/src/common/org/apache/solr/common/util/FastInputStream.java+++ b/src/common/org/apache/solr/common/util/FastInputStream.java@@ -105,7 +105,7 @@ public class FastInputStream extends InputStream implements DataInput {return r;}- return -1;+ return r > 0 ? r : -1;}@Overridediff --git a/src/test/org/apache/solr/common/util/TestFastInputStream.java b/src/test/org/apache/solr/common/util/TestFastInputStream.javanew file mode 100644index 00000000000..c2196283cb6--- /dev/null+++ b/src/test/org/apache/solr/common/util/TestFastInputStream.java@@ -0,0 +1,94 @@+/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.common.util;++import static org.junit.Assert.assertEquals;+import org.junit.Test;++import java.io.*;+import java.util.zip.GZIPInputStream;+import java.util.zip.GZIPOutputStream;++/**+ * Test for FastInputStream.+ *+ * @version $Id$+ * @see org.apache.solr.common.util.FastInputStream+ */+public class TestFastInputStream {+ @Test+ public void testgzip() throws Exception {+ ByteArrayOutputStream b = new ByteArrayOutputStream();+ FastOutputStream fos = new FastOutputStream(b);+ GZIPOutputStream gzos = new GZIPOutputStream(fos);+ String ss = "Helloooooooooooooooooooo";+ writeChars(gzos, ss, 0, ss.length());+ gzos.close();+ NamedListCodec.writeVInt(10, fos);+ fos.flushBuffer();+ GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(b.toByteArray(), 0, b.size()));+ char[] cbuf = new char[ss.length()];+ readChars(gzis, cbuf, 0, ss.length());+ assertEquals(new String(cbuf), ss);+ System.out.println("passes w/o FastInputStream");++ ByteArrayInputStream bis = new ByteArrayInputStream(b.toByteArray(), 0, b.size());+ gzis = new GZIPInputStream(new FastInputStream(bis));+ cbuf = new char[ss.length()];+ readChars(gzis, cbuf, 0, ss.length());+ assertEquals(new String(cbuf), ss);+ System.out.println("passes w FastInputStream");+++ }++ //code copied from NamedListCodec#readChars+ public static void readChars(InputStream in, char[] buffer, int start, int length)+ throws IOException {+ final int end = start + length;+ for (int i = start; i < end; i++) {+ int b = in.read();+ if ((b & 0x80) == 0)+ buffer[i] = (char) b;+ else if ((b & 0xE0) != 0xE0) {+ buffer[i] = (char) (((b & 0x1F) << 6)+ | (in.read() & 0x3F));+ } else+ buffer[i] = (char) (((b & 0x0F) << 12)+ | ((in.read() & 0x3F) << 6)+ | (in.read() & 0x3F));+ }+ }++ // code copied rfrom NamedlistCode#writechars+ public static void writeChars(OutputStream os, String s, int start, int length) throws IOException {+ final int end = start + length;+ for (int i = start; i < end; i++) {+ final int code = (int) s.charAt(i);+ if (code >= 0x01 && code <= 0x7F)+ os.write(code);+ else if (((code >= 0x80) && (code <= 0x7FF)) || code == 0) {+ os.write(0xC0 | (code >> 6));+ os.write(0x80 | (code & 0x3F));+ } else {+ os.write(0xE0 | (code >>> 12));+ os.write(0x80 | ((code >> 6) & 0x3F));+ os.write(0x80 | (code & 0x3F));+ }+ }+ }+}