001 /*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package com.google.common.io;
018
019 import com.google.common.annotations.Beta;
020 import com.google.common.base.Preconditions;
021
022 import java.io.FilterInputStream;
023 import java.io.IOException;
024 import java.io.InputStream;
025
026 /**
027 * An InputStream that limits the number of bytes which can be read.
028 *
029 * @author Charles Fry
030 * @since 1.0
031 */
032 @Beta
033 public final class LimitInputStream extends FilterInputStream {
034
035 private long left;
036 private long mark = -1;
037
038 /**
039 * Wraps another input stream, limiting the number of bytes which can be read.
040 *
041 * @param in the input stream to be wrapped
042 * @param limit the maximum number of bytes to be read
043 */
044 public LimitInputStream(InputStream in, long limit) {
045 super(in);
046 Preconditions.checkNotNull(in);
047 Preconditions.checkArgument(limit >= 0, "limit must be non-negative");
048 left = limit;
049 }
050
051
052 @Override
053 public int available() throws IOException {
054 return (int) Math.min(in.available(), left);
055 }
056
057
058 @Override
059 public synchronized void mark(int readlimit) {
060 in.mark(readlimit);
061 mark = left;
062 // it's okay to mark even if mark isn't supported, as reset won't work
063 }
064
065
066 @Override
067 public int read() throws IOException {
068 if (left == 0) {
069 return -1;
070 }
071
072 int result = in.read();
073 if (result != -1) {
074 --left;
075 }
076 return result;
077 }
078
079
080 @Override
081 public int read(byte[] b, int off, int len) throws IOException {
082 if (left == 0) {
083 return -1;
084 }
085
086 len = (int) Math.min(len, left);
087 int result = in.read(b, off, len);
088 if (result != -1) {
089 left -= result;
090 }
091 return result;
092 }
093
094
095 @Override
096 public synchronized void reset() throws IOException {
097 if (!in.markSupported()) {
098 throw new IOException("Mark not supported");
099 }
100 if (mark == -1) {
101 throw new IOException("Mark not set");
102 }
103
104 in.reset();
105 left = mark;
106 }
107
108
109 @Override
110 public long skip(long n) throws IOException {
111 n = Math.min(n, left);
112 long skipped = in.skip(n);
113 left -= skipped;
114 return skipped;
115 }
116 }