+
+ protected void copy(InputStream resourceInputStream, OutputStream ostream, Range range) throws IOException {
+ IOException exception = null;
+ InputStream istream = new BufferedInputStream(resourceInputStream, input);
+ exception = copyRange(istream, ostream, range.getStart(), range.getFinish());
+ // Clean up the input stream
+ istream.close();
+ // Rethrow any exception that has occurred
+ if (exception != null)
+ throw exception;
+ }
+ protected void copy(InputStream resourceInputStream, OutputStream ostream) throws IOException{
+ IOException exception = null;
+ InputStream istream = new BufferedInputStream(resourceInputStream, input);
+ // Copy the input stream to the output stream
+ exception = copyRange(istream, ostream);
+ // Clean up the input stream
+ istream.close();
+ // Rethrow any exception that has occurred
+ if (exception != null)
+ throw exception;
+ }
+
+ private IOException copyRange(InputStream istream, OutputStream ostream) {
+ // Copy the input stream to the output stream
+ IOException exception = null;
+ byte buffer[] = new byte[input];
+ int len = buffer.length;
+ while (true)
+ try {
+ len = istream.read(buffer);
+ if (len == -1)
+ break;
+ ostream.write(buffer, 0, len);
+ } catch (IOException e) {
+ exception = e;
+ len = -1;
+ break;
+ }
+ return exception;
+ }
+
+ private IOException copyRange(InputStream istream, OutputStream ostream, long start, long end) {
+ log.debug("Serving bytes:" + start + "-" + end);
+ try {
+ istream.skip(start);
+ } catch (IOException e) {
+ return e;
+ }
+ IOException exception = null;
+ long bytesToRead = end - start + 1;
+ byte buffer[] = new byte[input];
+ int len = buffer.length;
+ while (bytesToRead > 0 && len >= buffer.length) {
+ try {
+ len = istream.read(buffer);
+ if (bytesToRead >= len) {
+ ostream.write(buffer, 0, len);
+ bytesToRead -= len;
+ } else {
+ ostream.write(buffer, 0, (int) bytesToRead);
+ bytesToRead = 0;
+ }
+ } catch (IOException e) {
+ exception = e;
+ len = -1;
+ }
+ if (len < buffer.length)
+ break;
+ }
+ return exception;
+ }