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