001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.xbean.classloader;
018
019import java.io.ByteArrayOutputStream;
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.OutputStream;
023import java.io.Reader;
024import java.io.Writer;
025import java.util.jar.JarFile;
026
027/**
028 * @version $Rev: 437551 $ $Date: 2006-08-28 08:14:47 +0200 (Mon, 28 Aug 2006) $
029 */
030public final class IoUtil {
031    private IoUtil() {
032    }
033
034    public static byte[] getBytes(InputStream inputStream) throws IOException {
035        try {
036            byte[] buffer = new byte[4096];
037            ByteArrayOutputStream out = new ByteArrayOutputStream();
038            for (int count = inputStream.read(buffer); count >= 0; count = inputStream.read(buffer)) {
039                out.write(buffer, 0, count);
040            }
041            byte[] bytes = out.toByteArray();
042            return bytes;
043        } finally {
044            close(inputStream);
045        }
046    }
047
048    public static void flush(OutputStream thing) {
049        if (thing != null) {
050            try {
051                thing.flush();
052            } catch(Exception ignored) {
053            }
054        }
055    }
056
057    public static void flush(Writer thing) {
058        if (thing != null) {
059            try {
060                thing.flush();
061            } catch(Exception ignored) {
062            }
063        }
064    }
065
066    public static void close(JarFile thing) {
067        if (thing != null) {
068            try {
069                thing.close();
070            } catch(Exception ignored) {
071            }
072        }
073    }
074
075    public static void close(InputStream thing) {
076        if (thing != null) {
077            try {
078                thing.close();
079            } catch(Exception ignored) {
080            }
081        }
082    }
083
084    public static void close(OutputStream thing) {
085        if (thing != null) {
086            try {
087                thing.close();
088            } catch(Exception ignored) {
089            }
090        }
091    }
092
093    public static void close(Reader thing) {
094        if (thing != null) {
095            try {
096                thing.close();
097            } catch(Exception ignored) {
098            }
099        }
100    }
101
102    public static void close(Writer thing) {
103        if (thing != null) {
104            try {
105                thing.close();
106            } catch(Exception ignored) {
107            }
108        }
109    }
110
111    public static final class EmptyInputStream extends InputStream {
112        public int read() {
113            return -1;
114        }
115
116        public int read(byte b[])  {
117            return -1;
118        }
119
120        public int read(byte b[], int off, int len) {
121            return -1;
122        }
123
124        public long skip(long n) {
125            return 0;
126        }
127
128        public int available() {
129            return 0;
130        }
131
132        public void close() {
133        }
134
135        public synchronized void mark(int readlimit) {
136        }
137
138        public synchronized void reset() {
139        }
140
141        public boolean markSupported() {
142            return false;
143        }
144    }
145}