View Javadoc

1   /*
2    * Copyright 2007-2008 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kns.util.cache;
17  
18  import java.io.InputStream;
19  
20  /**
21   * ByteArrayInputStream implementation that does not synchronize methods.
22   */
23  public class FastByteArrayInputStream extends InputStream {
24      /**
25       * Our byte buffer
26       */
27      protected byte[] buf = null;
28  
29      /**
30       * Number of bytes that we can read from the buffer
31       */
32      protected int count = 0;
33  
34      /**
35       * Number of bytes that have been read from the buffer
36       */
37      protected int pos = 0;
38  
39      public FastByteArrayInputStream(byte[] buf, int count) {
40          this.buf = buf;
41          this.count = count;
42      }
43  
44      public final int available() {
45          return count - pos;
46      }
47  
48      public final int read() {
49          return (pos < count) ? (buf[pos++] & 0xff) : -1;
50      }
51  
52      public final int read(byte[] b, int off, int len) {
53          if (pos >= count)
54              return -1;
55  
56          if ((pos + len) > count)
57              len = (count - pos);
58  
59          System.arraycopy(buf, pos, b, off, len);
60          pos += len;
61          return len;
62      }
63  
64      public final long skip(long n) {
65          if ((pos + n) > count)
66              n = count - pos;
67          if (n < 0)
68              return 0;
69          pos += n;
70          return n;
71      }
72  
73  }