This project has retired. For details please refer to its Attic page.
CappedInputStream xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.server.shared;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.UnsupportedEncodingException;
24  
25  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
26  
27  /**
28   * A stream that counts bytes and throws an exception if the given maximum is
29   * reached. Counted bytes can be deducted to excludes parts of stream from the
30   * length limitation.
31   */
32  public class CappedInputStream extends InputStream {
33  
34      private InputStream stream;
35      private long max;
36      private long counter;
37  
38      public CappedInputStream(InputStream stream, long max) {
39          this.stream = stream;
40          this.max = max;
41          this.counter = 0;
42      }
43  
44      /**
45       * Returns the counter.
46       */
47      public long getCounter() {
48          return counter;
49      }
50  
51      /**
52       * Deducts the byte counter.
53       */
54      public void deductBytes(int byteCount) {
55          counter -= byteCount;
56      }
57  
58      /**
59       * Deducts the byte counter.
60       */
61      public void deductString(String s, String encoding) throws UnsupportedEncodingException {
62          if (encoding == null) {
63              counter -= s.getBytes("UTF-8").length;
64          } else {
65              counter -= s.getBytes(encoding).length;
66          }
67      }
68  
69      private void checkLength() throws IOException {
70          if (counter > max) {
71              throw new CmisInvalidArgumentException("Limit exceeded!");
72          }
73      }
74  
75      @Override
76      public int available() throws IOException {
77          return stream.available();
78      }
79  
80      @Override
81      public synchronized void mark(int readlimit) {
82      }
83  
84      @Override
85      public synchronized void reset() throws IOException {
86      }
87  
88      @Override
89      public boolean markSupported() {
90          return false;
91      }
92  
93      @Override
94      public int read() throws IOException {
95          checkLength();
96  
97          int b = stream.read();
98          if (b > -1) {
99              counter++;
100         }
101 
102         return b;
103     }
104 
105     @Override
106     public int read(byte[] b, int off, int len) throws IOException {
107         checkLength();
108 
109         int l = stream.read(b, off, len);
110         counter += l;
111 
112         return l;
113     }
114 
115     @Override
116     public int read(byte[] b) throws IOException {
117         checkLength();
118 
119         int l = stream.read(b);
120         counter += l;
121 
122         return l;
123     }
124 
125     @Override
126     public long skip(long n) throws IOException {
127         checkLength();
128 
129         long l = stream.skip(n);
130         counter += l;
131 
132         return l;
133     }
134 
135     @Override
136     public void close() throws IOException {
137         stream.close();
138     }
139 }