001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package org.apache.hadoop.lib.wsrs;
020
021 import org.apache.hadoop.io.IOUtils;
022
023 import javax.ws.rs.core.StreamingOutput;
024 import java.io.IOException;
025 import java.io.InputStream;
026 import java.io.OutputStream;
027
028 public class InputStreamEntity implements StreamingOutput {
029 private InputStream is;
030 private long offset;
031 private long len;
032
033 public InputStreamEntity(InputStream is, long offset, long len) {
034 this.is = is;
035 this.offset = offset;
036 this.len = len;
037 }
038
039 public InputStreamEntity(InputStream is) {
040 this(is, 0, -1);
041 }
042
043 @Override
044 public void write(OutputStream os) throws IOException {
045 long skipped = is.skip(offset);
046 if (skipped < offset) {
047 throw new IOException("Requested offset beyond stream size");
048 }
049 if (len == -1) {
050 IOUtils.copyBytes(is, os, 4096, true);
051 } else {
052 IOUtils.copyBytes(is, os, len, true);
053 }
054 }
055 }