View Javadoc
1   /*
2    * Copyright (C) 2007 The Guava Authors
3    * Copyright (C) 2014, Sasa Zivkov <sasa.zivkov@sap.com>, SAP AG
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.util.io;
46  
47  import java.io.FilterInputStream;
48  import java.io.IOException;
49  import java.io.InputStream;
50  
51  import org.eclipse.jgit.internal.JGitText;
52  
53  /**
54   * Wraps a {@link java.io.InputStream}, limiting the number of bytes which can
55   * be read.
56   *
57   * This class was copied and modifed from the Google Guava 16.0. Differently
58   * from the original Guava code, when a caller tries to read from this stream
59   * past the given limit and the wrapped stream hasn't yet reached its EOF this
60   * class will call the limitExceeded method instead of returning EOF.
61   *
62   * @since 3.3
63   */
64  public abstract class LimitedInputStream extends FilterInputStream {
65  
66  	private long left;
67  	/** Max number of bytes to be read from the wrapped stream */
68  	protected final long limit;
69  	private long mark = -1;
70  
71  	/**
72  	 * Create a new LimitedInputStream
73  	 *
74  	 * @param in an InputStream
75  	 * @param limit max number of bytes to read from the InputStream
76  	 */
77  	protected LimitedInputStream(InputStream in, long limit) {
78  		super(in);
79  		left = limit;
80  		this.limit = limit;
81  	}
82  
83  	/** {@inheritDoc} */
84  	@Override
85  	public int available() throws IOException {
86  		return (int) Math.min(in.available(), left);
87  	}
88  
89  	// it's okay to mark even if mark isn't supported, as reset won't work
90  	/** {@inheritDoc} */
91  	@Override
92  	public synchronized void mark(int readLimit) {
93  		in.mark(readLimit);
94  		mark = left;
95  	}
96  
97  	/** {@inheritDoc} */
98  	@Override
99  	public int read() throws IOException {
100 		if (left == 0) {
101 			if (in.available() == 0) {
102 				return -1;
103 			}
104 			limitExceeded();
105 		}
106 
107 		int result = in.read();
108 		if (result != -1) {
109 			--left;
110 		}
111 		return result;
112 	}
113 
114 	/** {@inheritDoc} */
115 	@Override
116 	public int read(byte[] b, int off, int len) throws IOException {
117 		if (left == 0) {
118 			if (in.available() == 0) {
119 				return -1;
120 			}
121 			limitExceeded();
122 		}
123 
124 		len = (int) Math.min(len, left);
125 		int result = in.read(b, off, len);
126 		if (result != -1) {
127 			left -= result;
128 		}
129 		return result;
130 	}
131 
132 	/** {@inheritDoc} */
133 	@Override
134 	public synchronized void reset() throws IOException {
135 		if (!in.markSupported())
136 			throw new IOException(JGitText.get().unsupportedMark);
137 
138 		if (mark == -1)
139 			throw new IOException(JGitText.get().unsetMark);
140 
141 		in.reset();
142 		left = mark;
143 	}
144 
145 	/** {@inheritDoc} */
146 	@Override
147 	public long skip(long n) throws IOException {
148 		n = Math.min(n, left);
149 		long skipped = in.skip(n);
150 		left -= skipped;
151 		return skipped;
152 	}
153 
154 	/**
155 	 * Called when trying to read past the given {@link #limit} and the wrapped
156 	 * InputStream {@link #in} hasn't yet reached its EOF
157 	 *
158 	 * @throws java.io.IOException
159 	 *             subclasses can throw an {@link java.io.IOException} when the
160 	 *             limit is exceeded. The throws java.io.IOException will be
161 	 *             forwarded back to the caller of the read method which read
162 	 *             the stream past the limit.
163 	 */
164 	protected abstract void limitExceeded() throws IOException;
165 }