1/*
2 * Copyright 2008 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.mockftpserver.core.util;
17
18import java.io.ByteArrayOutputStream;
19import java.io.IOException;
20import java.io.InputStream;
21
22/**
23 * Contains static I/O-related utility methods.
24 *
25 * @author Chris Mair
26 * @version $Revision$ - $Date$
27 */
28public class IoUtil {
29
30    /**
31     * Read the contents of the InputStream and return as a byte[].
32     *
33     * @param input - the InputStream to read
34     * @return the contents of the InputStream as a byte[]
35     * @throws AssertFailedException - if the InputStream is null
36     * @throws java.io.IOException   - if an error occurs reading the bytes
37     */
38    public static byte[] readBytes(InputStream input) throws IOException {
39        Assert.notNull(input, "input");
40        ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
41
42        try {
43            while (true) {
44                int b = input.read();
45                if (b == -1) {
46                    break;
47                }
48                outBytes.write(b);
49            }
50        }
51        finally {
52            input.close();
53        }
54        return outBytes.toByteArray();
55    }
56
57    /**
58     * Private constructor to prevent instantiation. All members are static.
59     */
60    private IoUtil() {
61    }
62
63}
64