ChannelInput.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 java.io.IOException;
  20. import java.nio.ByteBuffer;
  21. import java.nio.channels.ReadableByteChannel;

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

  23. /**
  24.  * The ChannelInput class takes a {@link ReadableByteChannel} object and
  25.  * wraps it as {@link Input} object acceptable by
  26.  * {@link CryptoInputStream}.
  27.  */
  28. public class ChannelInput implements Input {
  29.     private static final int SKIP_BUFFER_SIZE = 2048;

  30.     private ByteBuffer buf;
  31.     private final ReadableByteChannel channel;

  32.     /**
  33.      * Constructs the
  34.      * {@link org.apache.commons.crypto.stream.input.ChannelInput}.
  35.      *
  36.      * @param channel the ReadableByteChannel object.
  37.      */
  38.     public ChannelInput(final ReadableByteChannel channel) {
  39.         this.channel = channel;
  40.     }

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

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

  70.     /**
  71.      * Gets the skip buffer.
  72.      *
  73.      * @return the buffer.
  74.      */
  75.     private ByteBuffer getSkipBuf() {
  76.         if (buf == null) {
  77.             buf = ByteBuffer.allocate(SKIP_BUFFER_SIZE);
  78.         }
  79.         return buf;
  80.     }

  81.     /**
  82.      * Overrides the
  83.      * {@link org.apache.commons.crypto.stream.input.Input#read(ByteBuffer)}.
  84.      * Reads a sequence of bytes from input into the given buffer.
  85.      *
  86.      * @param dst The buffer into which bytes are to be transferred.
  87.      * @return the total number of bytes read into the buffer, or
  88.      *         {@code -1} if there is no more data because the end of the
  89.      *         stream has been reached.
  90.      * @throws IOException if an I/O error occurs.
  91.      */
  92.     @Override
  93.     public int read(final ByteBuffer dst) throws IOException {
  94.         return channel.read(dst);
  95.     }

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

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

  133.     /**
  134.      * Overrides the
  135.      * {@link org.apache.commons.crypto.stream.input.Input#skip(long)}. Skips
  136.      * over and discards {@code n} bytes of data from this input stream.
  137.      *
  138.      * @param n the number of bytes to be skipped.
  139.      * @return the actual number of bytes skipped.
  140.      * @throws IOException if an I/O error occurs.
  141.      */
  142.     @Override
  143.     public long skip(final long n) throws IOException {
  144.         long remaining = n;
  145.         int nr;

  146.         if (n <= 0) {
  147.             return 0;
  148.         }

  149.         final int size = (int) Math.min(SKIP_BUFFER_SIZE, remaining);
  150.         final ByteBuffer skipBuffer = getSkipBuf();
  151.         while (remaining > 0) {
  152.             skipBuffer.clear();
  153.             skipBuffer.limit((int) Math.min(size, remaining));
  154.             nr = read(skipBuffer);
  155.             if (nr < 0) {
  156.                 break;
  157.             }
  158.             remaining -= nr;
  159.         }

  160.         return n - remaining;
  161.     }
  162. }