StreamInput.java

  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, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS,
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  * See the License for the specific language governing permissions and
  16.  * limitations under the License.
  17.  */
  18. package org.apache.commons.crypto.stream.input;

  19. import static org.apache.commons.crypto.stream.CryptoInputStream.EOS;

  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.nio.ByteBuffer;

  23. import org.apache.commons.crypto.stream.CryptoInputStream;

  24.  /**
  25.  * The StreamInput class takes a {@link InputStream} object and wraps it as
  26.  * {@link Input} object acceptable by {@link CryptoInputStream}.
  27.  */
  28. public class StreamInput implements Input {

  29.     private final byte[] buf;
  30.     private final int bufferSize;
  31.     final InputStream in;

  32.     /**
  33.      * Constructs a {@link org.apache.commons.crypto.stream.input.StreamInput}.
  34.      *
  35.      * @param inputStream the InputStream object.
  36.      * @param bufferSize the buffer size.
  37.      */
  38.     public StreamInput(final InputStream inputStream, final int bufferSize) {
  39.         this.in = inputStream;
  40.         this.bufferSize = bufferSize;
  41.         this.buf = new byte[bufferSize];
  42.     }

  43.     /**
  44.      * Overrides the {@link Input#available()}. Returns an estimate of the
  45.      * number of bytes that can be read (or skipped over) from this input stream
  46.      * without blocking by the next invocation of a method for this input
  47.      * stream. The next invocation might be the same thread or another thread. A
  48.      * single read or skip of this many bytes will not block, but may read or
  49.      * skip fewer bytes.
  50.      *
  51.      * @return an estimate of the number of bytes that can be read (or skipped
  52.      *         over) from this input stream without blocking or {@code 0} when
  53.      *         it reaches the end of the input stream.
  54.      * @throws IOException if an I/O error occurs.
  55.      */
  56.     @Override
  57.     public int available() throws IOException {
  58.         return in.available();
  59.     }

  60.     /**
  61.      * Overrides the
  62.      * {@link org.apache.commons.crypto.stream.input.Input#seek(long)}. Closes
  63.      * this input and releases any system resources associated with the under
  64.      * layer input.
  65.      *
  66.      * @throws IOException if an I/O error occurs.
  67.      */
  68.     @Override
  69.     public void close() throws IOException {
  70.         in.close();
  71.     }

  72.     /**
  73.      * Overrides the
  74.      * {@link org.apache.commons.crypto.stream.input.Input#read(ByteBuffer)}.
  75.      * Reads a sequence of bytes from input into the given buffer.
  76.      *
  77.      * @param dst The buffer into which bytes are to be transferred.
  78.      *
  79.      * @return the total number of bytes read into the buffer, or
  80.      *         {@code EOS (-1)} if there is no more data because the end of the
  81.      *         stream has been reached.
  82.      * @throws IOException if an I/O error occurs.
  83.      */
  84.     @Override
  85.     public int read(final ByteBuffer dst) throws IOException {
  86.         int remaining = dst.remaining();
  87.         int read = 0;
  88.         while (remaining > 0) {
  89.             final int n = in.read(buf, 0, Math.min(remaining, bufferSize));
  90.             if (n == EOS) {
  91.                 if (read == 0) {
  92.                     read = EOS;
  93.                 }
  94.                 break;
  95.             }
  96.             if (n > 0) {
  97.                 dst.put(buf, 0, n);
  98.                 read += n;
  99.                 remaining -= n;
  100.             }
  101.         }
  102.         return read;
  103.     }

  104.     /**
  105.      * Overrides the
  106.      * {@link org.apache.commons.crypto.stream.input.Input#read(long, byte[], int, int)}
  107.      * . Reads up to {@code len} bytes of data from the input stream into
  108.      * an array of bytes. An attempt is made to read as many as {@code len}
  109.      * bytes, but a smaller number may be read. The number of bytes actually
  110.      * read is returned as an integer.
  111.      *
  112.      * @param position the given position within a stream.
  113.      * @param buffer the buffer into which the data is read.
  114.      * @param offset the start offset in array buffer.
  115.      * @param length the maximum number of bytes to read.
  116.      * @return the total number of bytes read into the buffer, or
  117.      *         {@code EOS (-1)} if there is no more data because the end of the
  118.      *         stream has been reached.
  119.      * @throws IOException if an I/O error occurs.
  120.      */
  121.     @Override
  122.     public int read(final long position, final byte[] buffer, final int offset, final int length) throws IOException {
  123.         throw new UnsupportedOperationException("Positioned read is not supported by this implementation");
  124.     }

  125.     /**
  126.      * Overrides the
  127.      * {@link org.apache.commons.crypto.stream.input.Input#seek(long)}. Seeks to
  128.      * the given offset from the start of the stream. The next read() will be
  129.      * from that location.
  130.      *
  131.      * @param position the offset from the start of the stream.
  132.      * @throws IOException if an I/O error occurs.
  133.      */
  134.     @Override
  135.     public void seek(final long position) throws IOException {
  136.         throw new UnsupportedOperationException("Seek is not supported by this implementation");
  137.     }

  138.     /**
  139.      * Overrides the
  140.      * {@link org.apache.commons.crypto.stream.input.Input#skip(long)}. Skips
  141.      * over and discards {@code n} bytes of data from this input stream.
  142.      *
  143.      * @param n the number of bytes to be skipped.
  144.      * @return the actual number of bytes skipped.
  145.      * @throws IOException if an I/O error occurs.
  146.      */
  147.     @Override
  148.     public long skip(final long n) throws IOException {
  149.         return in.skip(n);
  150.     }
  151. }