-
Notifications
You must be signed in to change notification settings - Fork 685
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
Refactor: Use InputStream.transferTo #2669
Changes from 1 commit
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 |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.lang.invoke.MethodHandles; | ||
import java.nio.BufferOverflowException; | ||
import java.nio.ByteBuffer; | ||
import java.security.MessageDigest; | ||
import java.util.ArrayList; | ||
|
@@ -34,6 +35,7 @@ | |
import java.util.List; | ||
import java.util.Map; | ||
import org.apache.commons.codec.binary.Hex; | ||
import org.apache.commons.io.input.BoundedInputStream; | ||
import org.apache.lucene.document.Document; | ||
import org.apache.lucene.index.IndexableField; | ||
import org.apache.lucene.index.Term; | ||
|
@@ -111,8 +113,8 @@ public void handleRequestBody(final SolrQueryRequest req, SolrQueryResponse rsp) | |
|
||
for (ContentStream stream : req.getContentStreams()) { | ||
ByteBuffer payload; | ||
try (InputStream is = stream.getStream()) { | ||
payload = Utils.toByteArray(is, maxSize); | ||
try (InputStream is = boundedInputStream(stream.getStream(), maxSize)) { | ||
payload = Utils.toByteArray(is); | ||
} | ||
MessageDigest m = MessageDigest.getInstance("MD5"); | ||
m.update(payload.array(), payload.arrayOffset() + payload.position(), payload.limit()); | ||
|
@@ -261,6 +263,16 @@ public void write(OutputStream os) throws IOException { | |
} | ||
} | ||
|
||
private static InputStream boundedInputStream(final InputStream is, final long maxLength) | ||
throws IOException { | ||
return new BoundedInputStream(is, maxLength) { | ||
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. A commons-io utility |
||
@Override | ||
protected void onMaxLength(long maxLength, long count) { | ||
throw new BufferOverflowException(); | ||
} | ||
}; | ||
} | ||
|
||
private void verifyWithRealtimeGet( | ||
String blobName, long version, SolrQueryRequest req, Map<String, Object> doc) { | ||
for (; ; ) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,6 @@ | |
import static org.apache.solr.handler.loader.CSVLoaderBase.SEPARATOR; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.Reader; | ||
|
@@ -65,14 +64,11 @@ public class DefaultSampleDocumentsLoader implements SampleDocumentsLoader { | |
private static final int MAX_STREAM_SIZE = (5 * 1024 * 1024); | ||
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); | ||
|
||
public static byte[] streamAsBytes(final InputStream in) throws IOException { | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
byte[] buf = new byte[1024]; | ||
int r; | ||
/** Reads input completely into a byte array. Closes the stream. */ | ||
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. This feels super trappy to have this method close the stream and Utils.toByteArray leave it open, but not something we can fix here. 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. Yes; it's unusual for a method to accept an InputStream and also close it. I at least documented it. And I reduced its scope from public. I was tempted to rename the method to something like streamAsBytesAndClose so that the closing is super apparent at the caller. Can still do. 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. we have the |
||
static byte[] streamAsBytes(final InputStream in) throws IOException { | ||
try (in) { | ||
while ((r = in.read(buf)) != -1) baos.write(buf, 0, r); | ||
return in.readAllBytes(); | ||
} | ||
return baos.toByteArray(); | ||
} | ||
|
||
@Override | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
import java.io.UnsupportedEncodingException; | ||
import java.util.Arrays; | ||
|
||
@Deprecated | ||
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. unused 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. could we add the since tag to help us remember when to remove this deprecated method? There are so many deprecated methods that just kind of hang around because we don't have good mechanisms for deciding/remembering to remove them! 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. I deleted it; we don't need to wait for internal utilities |
||
public class BytesOutputStream extends OutputStream { | ||
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1175,29 +1175,10 @@ public byte[] getbuf() { | |
} | ||
} | ||
|
||
/** Reads an input stream into a byte array. Does not close the input. */ | ||
public static ByteBuffer toByteArray(InputStream is) throws IOException { | ||
return toByteArray(is, Integer.MAX_VALUE); | ||
} | ||
|
||
/** | ||
* Reads an input stream into a byte array | ||
* | ||
* @param is the input stream | ||
* @return the byte array | ||
* @throws IOException If there is a low-level I/O error. | ||
*/ | ||
public static ByteBuffer toByteArray(InputStream is, long maxSize) throws IOException { | ||
try (BAOS bos = new BAOS()) { | ||
long sz = 0; | ||
int next = is.read(); | ||
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. this code was very sad, reading one char at a time! |
||
while (next > -1) { | ||
if (++sz > maxSize) { | ||
throw new BufferOverflowException(); | ||
} | ||
bos.write(next); | ||
next = is.read(); | ||
} | ||
bos.flush(); | ||
is.transferTo(bos); | ||
return bos.getByteBuffer(); | ||
} | ||
} | ||
|
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.
A commons-io utility
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.
Love it!