001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.commons.crypto.stream.input; 019 020import java.io.IOException; 021import java.io.InputStream; 022import java.nio.ByteBuffer; 023 024/** 025 * The StreamInput class takes a {@code InputStream} object and wraps it as 026 * {@code Input} object acceptable by {@code CryptoInputStream}. 027 */ 028public class StreamInput implements Input { 029 private final byte[] buf; 030 private final int bufferSize; 031 final InputStream in; 032 033 /** 034 * Constructs a {@link org.apache.commons.crypto.stream.input.StreamInput}. 035 * 036 * @param inputStream the inputstream object. 037 * @param bufferSize the buffersize. 038 */ 039 public StreamInput(final InputStream inputStream, final int bufferSize) { 040 this.in = inputStream; 041 this.bufferSize = bufferSize; 042 buf = new byte[bufferSize]; 043 } 044 045 /** 046 * Overrides the 047 * {@link org.apache.commons.crypto.stream.input.Input#read(ByteBuffer)}. 048 * Reads a sequence of bytes from input into the given buffer. 049 * 050 * @param dst The buffer into which bytes are to be transferred. 051 * 052 * @return the total number of bytes read into the buffer, or 053 * {@code -1} if there is no more data because the end of the 054 * stream has been reached. 055 * @throws IOException if an I/O error occurs. 056 */ 057 @Override 058 public int read(final ByteBuffer dst) throws IOException { 059 int remaining = dst.remaining(); 060 int read = 0; 061 while (remaining > 0) { 062 final int n = in.read(buf, 0, Math.min(remaining, bufferSize)); 063 if (n == -1) { 064 if (read == 0) { 065 read = -1; 066 } 067 break; 068 } else if (n > 0) { 069 dst.put(buf, 0, n); 070 read += n; 071 remaining -= n; 072 } 073 } 074 return read; 075 } 076 077 /** 078 * Overrides the 079 * {@link org.apache.commons.crypto.stream.input.Input#skip(long)}. Skips 080 * over and discards {@code n} bytes of data from this input stream. 081 * 082 * @param n the number of bytes to be skipped. 083 * @return the actual number of bytes skipped. 084 * @throws IOException if an I/O error occurs. 085 */ 086 @Override 087 public long skip(final long n) throws IOException { 088 return in.skip(n); 089 } 090 091 /** 092 * Overrides the {@link Input#available()}. Returns an estimate of the 093 * number of bytes that can be read (or skipped over) from this input stream 094 * without blocking by the next invocation of a method for this input 095 * stream. The next invocation might be the same thread or another thread. A 096 * single read or skip of this many bytes will not block, but may read or 097 * skip fewer bytes. 098 * 099 * @return an estimate of the number of bytes that can be read (or skipped 100 * over) from this input stream without blocking or {@code 0} when 101 * it reaches the end of the input stream. 102 * @throws IOException if an I/O error occurs. 103 */ 104 @Override 105 public int available() throws IOException { 106 return in.available(); 107 } 108 109 /** 110 * Overrides the 111 * {@link org.apache.commons.crypto.stream.input.Input#read(long, byte[], int, int)} 112 * . Reads up to {@code len} bytes of data from the input stream into 113 * an array of bytes. An attempt is made to read as many as {@code len} 114 * bytes, but a smaller number may be read. The number of bytes actually 115 * read is returned as an integer. 116 * 117 * @param position the given position within a stream. 118 * @param buffer the buffer into which the data is read. 119 * @param offset the start offset in array buffer. 120 * @param length the maximum number of bytes to read. 121 * @return the total number of bytes read into the buffer, or 122 * {@code -1} if there is no more data because the end of the 123 * stream has been reached. 124 * @throws IOException if an I/O error occurs. 125 */ 126 @Override 127 public int read(final long position, final byte[] buffer, final int offset, final int length) 128 throws IOException { 129 throw new UnsupportedOperationException( 130 "Positioned read is not supported by this implementation"); 131 } 132 133 /** 134 * Overrides the 135 * {@link org.apache.commons.crypto.stream.input.Input#seek(long)}. Seeks to 136 * the given offset from the start of the stream. The next read() will be 137 * from that location. 138 * 139 * @param position the offset from the start of the stream. 140 * @throws IOException if an I/O error occurs. 141 */ 142 @Override 143 public void seek(final long position) throws IOException { 144 throw new UnsupportedOperationException( 145 "Seek is not supported by this implementation"); 146 } 147 148 /** 149 * Overrides the 150 * {@link org.apache.commons.crypto.stream.input.Input#seek(long)}. Closes 151 * this input and releases any system resources associated with the under 152 * layer input. 153 * 154 * @throws IOException if an I/O error occurs. 155 */ 156 @Override 157 public void close() throws IOException { 158 in.close(); 159 } 160}