PageRenderTime 16ms CodeModel.GetById 11ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/hudson-utils/src/main/java/org/hudsonci/utils/io/OffsetLimitInputStream.java

http://github.com/hudson/hudson
Java | 110 lines | 59 code | 22 blank | 29 comment | 2 complexity | b1a0243878e8bc1671a78a2ae21cc3bf MD5 | raw file
  1/**
  2 * The MIT License
  3 *
  4 * Copyright (c) 2010-2011 Sonatype, Inc. All rights reserved.
  5 *
  6 * Permission is hereby granted, free of charge, to any person obtaining a copy
  7 * of this software and associated documentation files (the "Software"), to deal
  8 * in the Software without restriction, including without limitation the rights
  9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10 * copies of the Software, and to permit persons to whom the Software is
 11 * furnished to do so, subject to the following conditions:
 12 *
 13 * The above copyright notice and this permission notice shall be included in
 14 * all copies or substantial portions of the Software.
 15 *
 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22 * THE SOFTWARE.
 23 */
 24
 25package org.hudsonci.utils.io;
 26
 27import java.io.IOException;
 28import java.io.InputStream;
 29
 30import static com.google.common.base.Preconditions.checkNotNull;
 31
 32/**
 33 * Provides read-offset and read-limit for an underlying input-stream.
 34 *
 35 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
 36 * @since 2.1.0
 37 */
 38public class OffsetLimitInputStream
 39    extends InputStream
 40{
 41    public static final int UNLIMITED = -1;
 42
 43    private final InputStream delegate;
 44
 45    private final long offset;
 46
 47    private final long length;
 48
 49    private long count = 0;
 50
 51    public OffsetLimitInputStream(final InputStream delegate, final long offset, final long length) throws IOException {
 52        this.delegate = checkNotNull(delegate);
 53        this.offset = offset;
 54        this.length = length;
 55
 56        delegate.skip(offset);
 57    }
 58
 59    public InputStream getDelegate() {
 60        return delegate;
 61    }
 62
 63    public long getOffset() {
 64        return offset;
 65    }
 66
 67    public long getLength() {
 68        return length;
 69    }
 70
 71    public long getCount() {
 72        return count;
 73    }
 74
 75    @Override
 76    public int read() throws IOException {
 77        if (length > UNLIMITED && count > length) {
 78            return -1;
 79        }
 80
 81        count++;
 82
 83        return getDelegate().read();
 84    }
 85
 86    @Override
 87    public int available() throws IOException {
 88        return getDelegate().available();
 89    }
 90
 91    @Override
 92    public void close() throws IOException {
 93        getDelegate().close();
 94    }
 95
 96    @Override
 97    public synchronized void mark(final int readlimit) {
 98        getDelegate().mark(readlimit);
 99    }
100
101    @Override
102    public synchronized void reset() throws IOException {
103        getDelegate().reset();
104    }
105
106    @Override
107    public boolean markSupported() {
108        return getDelegate().markSupported();
109    }
110}