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
20 import java.io.Closeable;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.nio.ByteBuffer;
24 import java.nio.channels.ReadableByteChannel;
25
26 import org.apache.commons.crypto.stream.CryptoInputStream;
27
28 /**
29 * The Input interface abstract the input source of
30 * {@link CryptoInputStream} so that different implementation of input can
31 * be used. The implementation Input interface will usually wraps an input
32 * mechanism such as {@link InputStream} or
33 * {@link ReadableByteChannel}.
34 */
35 public interface Input extends Closeable {
36 /**
37 * Returns an estimate of the number of bytes that can be read (or skipped
38 * over) from this input without blocking by the next invocation of a method
39 * for this input stream. The next invocation might be the same thread or
40 * another thread. A single read or skip of this many bytes will not block,
41 * but may read or skip fewer bytes.
42 *
43 * <p>
44 * It is never correct to use the return value of this method to allocate a
45 * buffer intended to hold all data in this stream.
46 *
47 * @return an estimate of the number of bytes that can be read (or skipped
48 * over) from this input stream without blocking or {@code 0} when
49 * it reaches the end of the input stream.
50 * @throws IOException if an I/O error occurs.
51 */
52 int available() throws IOException;
53
54 /**
55 * Closes this input and releases any system resources associated with the
56 * under layer input.
57 *
58 * @throws IOException if an I/O error occurs.
59 */
60 @Override
61 void close() throws IOException;
62
63 /**
64 * Reads a sequence of bytes from input into the given buffer.
65 *
66 * <p>
67 * An attempt is made to read up to <i>r</i> bytes from the input, where
68 * <i>r</i> is the number of bytes remaining in the buffer, that is,
69 * {@code dst.remaining()}, at the moment this method is invoked.
70 *
71 * <p>
72 * Suppose that a byte sequence of length <i>n</i> is read, where {@code 0}
73 * {@code <=} <i>n</i> {@code <=} <i>r</i>.
74 * This byte sequence will be transferred into the buffer so that the first
75 * byte in the sequence is at index <i>p</i> and the last byte is at index
76 * <i>p</i> {@code +} <i>n</i> {@code -} {@code 1},
77 * where <i>p</i> is the buffer's position at the moment this method is
78 * invoked. Upon return the buffer's position will be equal to
79 * <i>p</i> {@code +} <i>n</i>; its limit will not have changed.
80 *
81 * @param dst The buffer into which bytes are to be transferred.
82 * @return the total number of bytes read into the buffer, or
83 * {@code -1} if there is no more data because the end of the
84 * stream has been reached.
85 * @throws IOException If some other I/O error occurs.
86 */
87 int read(ByteBuffer dst) throws IOException;
88
89 /**
90 * Reads up to the specified number of bytes from a given position within a
91 * stream and return the number of bytes read. This does not change the
92 * current offset of the stream and is thread-safe.
93 *
94 * An implementation may not support positioned read. If the implementation
95 * doesn't support positioned read, it throws UnsupportedOperationException.
96 *
97 * @param position the given position within a stream.
98 * @param buffer the buffer into which the data is read.
99 * @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 }