001    package ezvcard.util;
002    
003    import java.io.ByteArrayOutputStream;
004    import java.io.Closeable;
005    import java.io.File;
006    import java.io.IOException;
007    import java.io.InputStream;
008    
009    /*
010     Copyright (c) 2012, Michael Angstadt
011     All rights reserved.
012    
013     Redistribution and use in source and binary forms, with or without
014     modification, are permitted provided that the following conditions are met: 
015    
016     1. Redistributions of source code must retain the above copyright notice, this
017     list of conditions and the following disclaimer. 
018     2. Redistributions in binary form must reproduce the above copyright notice,
019     this list of conditions and the following disclaimer in the documentation
020     and/or other materials provided with the distribution. 
021    
022     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
023     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
024     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
025     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
026     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
027     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
028     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
029     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
030     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
031     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
032    
033     The views and conclusions contained in the software and documentation are those
034     of the authors and should not be interpreted as representing official policies, 
035     either expressed or implied, of the FreeBSD Project.
036     */
037    
038    /**
039     * I/O helper classes.
040     * @author Michael Angstadt
041     */
042    public class IOUtils {
043            /**
044             * Gets the extension off a file's name.
045             * @param file the file
046             * @return its extension (e.g. "jpg") or null if it doesn't have one
047             */
048            public static String getFileExtension(File file) {
049                    String fileName = file.getName();
050                    int dot = fileName.lastIndexOf('.');
051                    if (dot >= 0 && dot < fileName.length() - 1) {
052                            return fileName.substring(dot + 1);
053                    }
054                    return null;
055            }
056    
057            /**
058             * Reads all the bytes from an input stream.
059             * @param in the input stream
060             * @return the bytes
061             * @throws IOException if there's a problem reading from the input stream
062             */
063            public static byte[] toByteArray(InputStream in) throws IOException {
064                    return toByteArray(in, false);
065            }
066    
067            /**
068             * Reads all the bytes from an input stream.
069             * @param in the input stream
070             * @param close true to close the input stream when done, false not to
071             * @return the bytes
072             * @throws IOException if there's a problem reading from the input stream
073             */
074            public static byte[] toByteArray(InputStream in, boolean close) throws IOException {
075                    try {
076                            ByteArrayOutputStream out = new ByteArrayOutputStream();
077                            byte[] buffer = new byte[4096];
078                            int read;
079                            while ((read = in.read(buffer)) != -1) {
080                                    out.write(buffer, 0, read);
081                            }
082                            return out.toByteArray();
083                    } finally {
084                            in.close();
085                    }
086            }
087    
088            /**
089             * Closes a closeable resource, catching its {@link IOException}.
090             * @param closeable the resource to close (can be null)
091             */
092            public static void closeQuietly(Closeable closeable) {
093                    try {
094                            if (closeable != null) {
095                                    closeable.close();
096                            }
097                    } catch (IOException e) {
098                            //ignore
099                    }
100            }
101    }