001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.core.util.io;
025    
026    import java.io.BufferedInputStream;
027    import java.io.File;
028    import java.io.FileInputStream;
029    import java.io.FileReader;
030    import java.io.IOException;
031    import java.io.InputStream;
032    import java.io.InputStreamReader;
033    import java.io.LineNumberReader;
034    import java.io.Reader;
035    import java.net.URLConnection;
036    import java.util.logging.Level;
037    import java.util.logging.Logger;
038    import java.util.zip.GZIPInputStream;
039    import java.util.zip.ZipInputStream;
040    
041    public class IntelligentFileReader extends Reader {
042    
043            private static final Logger logger = Logger.getLogger(IntelligentFileReader.class.getName());
044    
045            private FileReader fr = null;
046    
047            private InputStream zip = null;
048    
049            private LineNumberReader lr = null;
050    
051            private String encoding = "UTF-8";
052    
053            public IntelligentFileReader(String file) {
054                    this(new File(file));
055            }
056    
057            public IntelligentFileReader(InputStream inputStream) {
058                    try {
059                            lr = new LineNumberReader(new InputStreamReader(inputStream, encoding));
060                    } catch (Exception e) {
061                            logger.log(Level.WARNING, "could not open stream", e);
062                    }
063            }
064    
065            public IntelligentFileReader(Reader reader) {
066                    try {
067                            lr = new LineNumberReader(reader);
068                    } catch (Exception e) {
069                            logger.log(Level.WARNING, "could not open stream", e);
070                    }
071            }
072    
073            public IntelligentFileReader(File file) {
074                    if (file != null && file.exists()) {
075                            if (file.getAbsolutePath().toLowerCase().endsWith(".gz")) {
076                                    try {
077                                            zip = new GZIPInputStream(new FileInputStream(file));
078                                            lr = new LineNumberReader(new InputStreamReader(zip, encoding));
079                                    } catch (Exception e) {
080                                            logger.log(Level.WARNING, "could not open file " + file, e);
081                                    }
082                            } else if (file.getAbsolutePath().toLowerCase().endsWith(".z")) {
083                                    try {
084                                            zip = new ZipInputStream(new FileInputStream(file));
085                                            lr = new LineNumberReader(new InputStreamReader(zip, encoding));
086                                    } catch (Exception e) {
087                                            logger.log(Level.WARNING, "could not open file " + file, e);
088                                    }
089                            } else {
090                                    try {
091                                            fr = new FileReader(file);
092                                            lr = new LineNumberReader(fr);
093                                    } catch (Exception e) {
094                                            logger.log(Level.WARNING, "could not open file " + file, e);
095                                    }
096                            }
097                    } else {
098                            logger.log(Level.WARNING, "cannot open file: " + file);
099                    }
100            }
101    
102            public IntelligentFileReader(URLConnection connection) throws IOException {
103                    this(connection.getInputStream());
104            }
105    
106            public String readLine() {
107                    if (lr == null)
108                            return null;
109                    try {
110                            return lr.readLine();
111                    } catch (Exception e) {
112                            logger.log(Level.WARNING, "could not read line", e);
113                    }
114                    return null;
115            }
116    
117            
118            public void close() {
119                    try {
120                            if (lr != null)
121                                    lr.close();
122                    } catch (Exception e) {
123                    }
124                    try {
125                            if (fr != null)
126                                    fr.close();
127                    } catch (Exception e) {
128                    }
129                    try {
130                            if (zip != null)
131                                    zip.close();
132                    } catch (Exception e) {
133                    }
134            }
135    
136            public int getLineNumber() {
137                    if (lr != null)
138                            return lr.getLineNumber();
139                    else
140                            return -1;
141            }
142    
143            
144            public int read(char[] cbuf, int off, int len) throws IOException {
145                    return lr.read(cbuf, off, len);
146            }
147    
148            public static String load(String filename) {
149                    StringBuilder s = new StringBuilder();
150                    IntelligentFileReader fr = new IntelligentFileReader(filename);
151                    String line = null;
152                    while ((line = fr.readLine()) != null) {
153                            s.append(line + "\n");
154                    }
155                    fr.close();
156                    return s.toString();
157            }
158    
159            public static String load(File filename) {
160                    StringBuilder s = new StringBuilder();
161                    IntelligentFileReader fr = new IntelligentFileReader(filename);
162                    String line = null;
163                    while ((line = fr.readLine()) != null) {
164                            s.append(line + "\n");
165                    }
166                    fr.close();
167                    return s.toString();
168            }
169    
170            public static String load(InputStream stream) {
171                    StringBuilder s = new StringBuilder();
172                    IntelligentFileReader fr = new IntelligentFileReader(stream);
173                    String line = null;
174                    while ((line = fr.readLine()) != null) {
175                            s.append(line + "\n");
176                    }
177                    fr.close();
178                    return s.toString();
179            }
180    
181            public static String load(Reader reader) {
182                    StringBuilder s = new StringBuilder();
183                    IntelligentFileReader fr = new IntelligentFileReader(reader);
184                    String line = null;
185                    while ((line = fr.readLine()) != null) {
186                            s.append(line + "\n");
187                    }
188                    fr.close();
189                    return s.toString();
190            }
191    
192            public static byte[] readBytes(File file) {
193                    try {
194                            byte[] data = new byte[(int) file.length()];
195                            FileInputStream fi = new FileInputStream(file);
196                            BufferedInputStream bi = new BufferedInputStream(fi);
197                            bi.read(data);
198                            bi.close();
199                            fi.close();
200                            return data;
201                    } catch (Exception e) {
202                            e.printStackTrace();
203                            return null;
204                    }
205            }
206    }