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.Closeable; 021import java.io.IOException; 022import java.io.InputStream; 023import java.nio.ByteBuffer; 024import java.nio.channels.ReadableByteChannel; 025 026import org.apache.commons.crypto.stream.CryptoInputStream; 027 028/** 029 * The Input interface abstract the input source of 030 * {@link CryptoInputStream} so that different implementation of input can 031 * be used. The implementation Input interface will usually wraps an input 032 * mechanism such as {@link InputStream} or 033 * {@link ReadableByteChannel}. 034 */ 035public interface Input extends Closeable { 036 /** 037 * Returns an estimate of the number of bytes that can be read (or skipped 038 * over) from this input without blocking by the next invocation of a method 039 * for this input stream. The next invocation might be the same thread or 040 * another thread. A single read or skip of this many bytes will not block, 041 * but may read or skip fewer bytes. 042 * 043 * <p> 044 * It is never correct to use the return value of this method to allocate a 045 * buffer intended to hold all data in this stream. 046 * 047 * @return an estimate of the number of bytes that can be read (or skipped 048 * over) from this input stream without blocking or {@code 0} when 049 * it reaches the end of the input stream. 050 * @throws IOException if an I/O error occurs. 051 */ 052 int available() throws IOException; 053 054 /** 055 * Closes this input and releases any system resources associated with the 056 * under layer input. 057 * 058 * @throws IOException if an I/O error occurs. 059 */ 060 @Override 061 void close() throws IOException; 062 063 /** 064 * Reads a sequence of bytes from input into the given buffer. 065 * 066 * <p> 067 * An attempt is made to read up to <i>r</i> bytes from the input, where 068 * <i>r</i> is the number of bytes remaining in the buffer, that is, 069 * {@code dst.remaining()}, at the moment this method is invoked. 070 * 071 * <p> 072 * Suppose that a byte sequence of length <i>n</i> is read, where {@code 0} 073 * {@code <=} <i>n</i> {@code <=} <i>r</i>. 074 * This byte sequence will be transferred into the buffer so that the first 075 * byte in the sequence is at index <i>p</i> and the last byte is at index 076 * <i>p</i> {@code +} <i>n</i> {@code -} {@code 1}, 077 * where <i>p</i> is the buffer's position at the moment this method is 078 * invoked. Upon return the buffer's position will be equal to 079 * <i>p</i> {@code +} <i>n</i>; its limit will not have changed. 080 * 081 * @param dst The buffer into which bytes are to be transferred. 082 * @return the total number of bytes read into the buffer, or 083 * {@code -1} if there is no more data because the end of the 084 * stream has been reached. 085 * @throws IOException If some other I/O error occurs. 086 */ 087 int read(ByteBuffer dst) throws IOException; 088 089 /** 090 * Reads up to the specified number of bytes from a given position within a 091 * stream and return the number of bytes read. This does not change the 092 * current offset of the stream and is thread-safe. 093 * 094 * An implementation may not support positioned read. If the implementation 095 * doesn't support positioned read, it throws UnsupportedOperationException. 096 * 097 * @param position the given position within a stream. 098 * @param buffer the buffer into which the data is read. 099 * @param offset the start offset in array buffer. 100 * @param length the maximum number of bytes to read. 101 * @return the total number of bytes read into the buffer, or 102 * {@code -1} if there is no more data because the end of the 103 * stream has been reached. 104 * @throws IOException if an I/O error occurs. 105 */ 106 int read(long position, byte[] buffer, int offset, int length) 107 throws IOException; 108 109 /** 110 * Seeks to the given offset from the start of the stream. The next read() 111 * will be from that location. 112 * 113 * An implementation may not support seek. If the implementation doesn't 114 * support seek, it throws UnsupportedOperationException. 115 * 116 * @param position the offset from the start of the stream. 117 * @throws IOException if an I/O error occurs. 118 */ 119 void seek(long position) throws IOException; 120 121 /** 122 * Skips over and discards {@code n} bytes of data from this input The 123 * {@code skip} method may, for a variety of reasons, end up skipping 124 * over some smaller number of bytes, possibly {@code 0}. This may 125 * result from any of a number of conditions; reaching end of file before 126 * {@code n} bytes have been skipped is only one possibility. The 127 * actual number of bytes skipped is returned. If {@code n} is 128 * negative, no bytes are skipped. 129 * 130 * <p> 131 * The {@code skip} method of this class creates a byte array and then 132 * repeatedly reads into it until {@code n} bytes have been read or the 133 * end of the stream has been reached. Subclasses are encouraged to provide 134 * a more efficient implementation of this method. For instance, the 135 * implementation may depend on the ability to seek. 136 * 137 * @param n the number of bytes to be skipped. 138 * @return the actual number of bytes skipped. 139 * @throws IOException if the stream does not support seek, or if some 140 * other I/O error occurs. 141 */ 142 long skip(long n) throws IOException; 143}