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   */
    20  package org.apache.mina.util;
    21
    22  import java.io.InputStream;
    23  import java.nio.ByteBuffer;
    24
    25  /**
    26   * {@link InputStream} wrapper for {@link ByteBuffer}
    27   *
    28   * <p>
    29   * <i>Currently this class is only used and available in MINA's codec module</i>
    30   * </p>
    31   *
    32   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    33   *
    34   */
    35  public class ByteBufferInputStream extends InputStream {
    36
    37      private final ByteBuffer buffer;
    38
    39      /**
    40       *
    41       * @param buffer
    42       *            a buffer to be wrapped
    43       */
    44      public ByteBufferInputStream(ByteBuffer buffer) {
    45          super();
    46          this.buffer = buffer;
    47      }
    48
    49      /**
    50       * @see InputStream#available()
    51       */
    52      @Override
    53      public int available() {
    54          return buffer.remaining();
    55      }
    56
    57      /**
    58       * @see InputStream#mark(int)
    59       */
    60      @Override
    61      public synchronized void mark(int readlimit) {
    62          buffer.mark();
    63      }
    64
    65      /**
    66       * @see InputStream#markSupported()
    67       */
    68      @Override
    69      public boolean markSupported() {
    70          return true;
    71      }
    72
    73      /**
    74       * @see InputStream#read()
    75       */
    76      @Override
    77      public int read() {
    78          if (buffer.hasRemaining()) {
    79              return buffer.get() & 0xff;
    80          }
    81
    82          return -1;
    83      }
    84
    85      /**
    86       * @see InputStream#read(byte[], int, int)
    87       */
    88      @Override
    89      public int read(byte[] b, int off, int len) {
    90          int remaining = buffer.remaining();
    91          if (remaining > 0) {
    92              int readBytes = Math.min(remaining, len);
    93              buffer.get(b, off, readBytes);
    94              return readBytes;
    95          }
    96
    97          return -1;
    98      }
    99
    100     /**
    101      * @see InputStream#reset()
    102      */
    103     @Override
    104     public synchronized void reset() {
    105         buffer.reset();
    106     }
    107
    108     /**
    109      *
    110      * @param n
    111      *            the number of bytes to skip (values bigger than
    112      *            {@link Integer#MAX_VALUE} push the reading head to the end).
    113      *
    114      * @see InputStream#skip(long)
    115      */
    116     @Override
    117     public long skip(long n) {
    118         int bytes;
    119         if (n > Integer.MAX_VALUE) {
    120             bytes = buffer.remaining();
    121         } else {
    122             bytes = Math.min(buffer.remaining(), (int) n);
    123         }
    124         buffer.position(buffer.position() + bytes);
    125
    126         return bytes;
    127     }
    128
    129 }