View Javadoc
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.utils;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  
23  import org.apache.commons.crypto.stream.input.Input;
24  
25  /**
26   * General utility methods for working with IO.
27   */
28  public final class IoUtils {
29  
30      /**
31       * The private constructor of {@link IoUtils}.
32       */
33      private IoUtils() {
34      }
35  
36      /**
37       * Does the readFully based on the Input read.
38       *
39       * @param in the input stream of bytes.
40       * @param buf the buffer to be read.
41       * @param off the start offset in array buffer.
42       * @param len the maximum number of bytes to read.
43       * @throws IOException if an I/O error occurs.
44       */
45      public static void readFully(InputStream in, byte buf[], int off, int len)
46              throws IOException {
47          int toRead = len;
48          while (toRead > 0) {
49              int ret = in.read(buf, off, toRead);
50              if (ret < 0) {
51                  throw new IOException("Premature EOF from inputStream");
52              }
53              toRead -= ret;
54              off += ret;
55          }
56      }
57  
58      /**
59       * Does the readFully based on Input's positioned read. This does not change
60       * the current offset of the stream and is thread-safe.
61       *
62       * @param in the input source.
63       * @param position the given position.
64       * @param buffer the buffer to be read.
65       * @param length the maximum number of bytes to read.
66       * @param offset the start offset in array buffer.
67       * @throws IOException if an I/O error occurs.
68       */
69      public static void readFully(Input in, long position, byte[] buffer,
70              int offset, int length) throws IOException {
71          int nread = 0;
72          while (nread < length) {
73              int nbytes = in.read(position + nread, buffer, offset + nread,
74                      length - nread);
75              if (nbytes < 0) {
76                  throw new IOException(
77                          "End of stream reached before reading fully.");
78              }
79              nread += nbytes;
80          }
81      }
82  
83      /**
84       * Closes the Closeable objects and <b>ignore</b> any {@link IOException} or
85       * null pointers. Must only be used for cleanup in exception handlers.
86       *
87       * @param closeables the objects to close.
88       */
89      public static void cleanup(java.io.Closeable... closeables) {
90          for (java.io.Closeable c : closeables) {
91              if (c != null) {
92                  try {
93                      c.close();
94                  } catch (IOException e) { // NOPMD
95                  }
96              }
97          }
98      }
99  }