Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache.kahadb.util » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.kahadb.util;
   18   
   19   import java.io.IOException;
   20   import java.io.InputStream;
   21   
   22   /**
   23    * Very similar to the java.io.ByteArrayInputStream but this version is not
   24    * thread safe.
   25    */
   26   public class ByteArrayInputStream extends InputStream {
   27   
   28       byte buffer[];
   29       int limit;
   30       int pos;
   31       int mark;
   32   
   33       public ByteArrayInputStream(byte data[]) {
   34           this(data, 0, data.length);
   35       }
   36   
   37       public ByteArrayInputStream(ByteSequence sequence) {
   38           this(sequence.getData(), sequence.getOffset(), sequence.getLength());
   39       }
   40   
   41       public ByteArrayInputStream(byte data[], int offset, int size) {
   42           this.buffer = data;
   43           this.mark = offset;
   44           this.pos = offset;
   45           this.limit = offset + size;
   46       }
   47   
   48       public int read() throws IOException {
   49           if (pos < limit) {
   50               return buffer[pos++] & 0xff;
   51           } else {
   52               return -1;
   53           }
   54       }
   55   
   56       public int read(byte[] b) throws IOException {
   57           return read(b, 0, b.length);
   58       }
   59   
   60       public int read(byte b[], int off, int len) {
   61           if (pos < limit) {
   62               len = Math.min(len, limit - pos);
   63               if (len > 0) {
   64                   System.arraycopy(buffer, pos, b, off, len);
   65                   pos += len;
   66               }
   67               return len;
   68           } else {
   69               return -1;
   70           }
   71       }
   72   
   73       public long skip(long len) throws IOException {
   74           if (pos < limit) {
   75               len = Math.min(len, limit - pos);
   76               if (len > 0) {
   77                   pos += len;
   78               }
   79               return len;
   80           } else {
   81               return -1;
   82           }
   83       }
   84   
   85       public int available() {
   86           return limit - pos;
   87       }
   88   
   89       public boolean markSupported() {
   90           return true;
   91       }
   92   
   93       public void mark(int markpos) {
   94           mark = pos;
   95       }
   96   
   97       public void reset() {
   98           pos = mark;
   99       }
  100   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache.kahadb.util » [javadoc | source]