001 /* 002 Copyright (c) 2009 Olivier Chafik, All Rights Reserved 003 004 This file is part of JNAerator (http://jnaerator.googlecode.com/). 005 006 JNAerator is free software: you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation, either version 3 of the License, or 009 (at your option) any later version. 010 011 JNAerator is distributed in the hope that it will be useful, 012 but WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 GNU General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with JNAerator. If not, see <http://www.gnu.org/licenses/>. 018 */ 019 package com.ochafik.io; 020 import java.io.BufferedInputStream; 021 import java.io.BufferedOutputStream; 022 import java.io.BufferedReader; 023 import java.io.ByteArrayOutputStream; 024 import java.io.File; 025 import java.io.FileInputStream; 026 import java.io.FileOutputStream; 027 import java.io.FileReader; 028 import java.io.IOException; 029 import java.io.InputStream; 030 import java.io.InputStreamReader; 031 import java.io.OutputStream; 032 import java.io.Reader; 033 import java.io.UnsupportedEncodingException; 034 import java.net.HttpURLConnection; 035 import java.net.URL; 036 import java.net.URLConnection; 037 import java.util.ArrayList; 038 import java.util.List; 039 import java.util.Properties; 040 import java.util.regex.Pattern; 041 042 //import javax.net.ssl.HostnameVerifier; 043 //import javax.net.ssl.HttpsURLConnection; 044 //import javax.net.ssl.SSLSession; 045 046 import com.ochafik.util.string.RegexUtils; 047 public class ReadText { 048 public static final String readText(Reader in) throws IOException { 049 int len; 050 char ch[]=new char[1024]; 051 StringBuffer b=new StringBuffer(); 052 while ((len=in.read(ch))>0) b.append(ch,0,len); 053 return b.toString(); 054 } 055 public static final String readText(File f) { 056 if (f == null) 057 return null; 058 059 try { 060 BufferedReader in=new BufferedReader(new FileReader(f)); 061 String l; 062 java.lang.StringBuffer buff=new java.lang.StringBuffer((int)f.length()); 063 boolean first = true; 064 while ((l=in.readLine())!=null) { 065 if (first) 066 first = false; 067 else 068 buff.append('\n'); 069 buff.append(l); 070 } 071 return buff.toString(); 072 } catch (Exception except) { 073 except.printStackTrace(); 074 return null; 075 } 076 } 077 public static final void writeProperties(Properties properties, File file) throws IOException { 078 File parentFile = file.getParentFile(); 079 if (!parentFile.exists()) parentFile.mkdirs(); 080 OutputStream out = new BufferedOutputStream(new FileOutputStream(file)); 081 properties.store(out, file.getName()); 082 out.close(); 083 } 084 public static final Properties readProperties(File file, boolean failIfNotExists) throws IOException { 085 Properties properties = new Properties(); 086 if (!file.exists()) { 087 if (failIfNotExists) { 088 return null; 089 } 090 } else { 091 InputStream in = new BufferedInputStream(new FileInputStream(file)); 092 properties.load(in); 093 in.close(); 094 } 095 return properties; 096 } 097 private static final Pattern charsetPattern = Pattern.compile(";\\s*charset\\s*=\\s*([\\w-_]+)", Pattern.CASE_INSENSITIVE | Pattern.DOTALL); 098 public static final String readText(URL u) throws IOException { 099 if (u == null) return null; 100 101 String encoding = null; 102 URLConnection con = u.openConnection(); 103 // if (con instanceof HttpsURLConnection) { 104 // ((HttpsURLConnection)con).setHostnameVerifier(new HostnameVerifier() { 105 // public boolean verify(String hostname, SSLSession session) { 106 // return true; 107 // } 108 // }); 109 // } 110 if (con instanceof HttpURLConnection) { 111 String contentType = con.getContentType(); 112 encoding = RegexUtils.findFirst(contentType, charsetPattern, 1); 113 } 114 InputStream in = con.getInputStream(); 115 String text = readText(in, encoding); 116 in.close(); 117 return text; 118 } 119 static Pattern[] embeddedCharsetPatterns = new Pattern[] { 120 Pattern.compile("CONTENT\\s*=\\s*\"text/\\w+;\\s*charset\\s*=\\s*([^\";\\s]+)\\s*\"", Pattern.CASE_INSENSITIVE | Pattern.DOTALL), 121 Pattern.compile("<?\\s*xml\\s+[^>]*encoding\\s*=\\s*\"([^\"]+)\"", Pattern.CASE_INSENSITIVE | Pattern.DOTALL), 122 }; 123 private static String readText(InputStream in, String encoding) throws IOException { 124 if (in == null) 125 return null; 126 127 if (encoding == null) { 128 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 129 IOUtils.readWrite(in, bout); 130 byte[] bytes = bout.toByteArray(); 131 String ascii = new String(bytes, "ascii"); 132 133 for (Pattern charsetPat : embeddedCharsetPatterns) { 134 encoding = RegexUtils.findFirst(ascii, charsetPat, 1); 135 if (encoding != null) 136 break; 137 } 138 if (encoding != null) { 139 try { 140 return new String(bytes, encoding); 141 } catch (UnsupportedEncodingException ex) { 142 ex.printStackTrace(); 143 } 144 } 145 return new String(bytes); 146 } else { 147 148 BufferedReader rin = new BufferedReader(encoding == null ? new InputStreamReader(in) : new InputStreamReader(in, encoding)); 149 String l; 150 StringBuffer buff=new StringBuffer(); 151 boolean first = true; 152 while ((l = rin.readLine()) != null) { 153 if (first) 154 first = false; 155 else 156 buff.append('\n'); 157 buff.append(l); 158 } 159 return buff.toString(); 160 } 161 } 162 public static final String readText(InputStream in) throws IOException { 163 return readText(in, null); 164 } 165 public static final List<String> readLines(String s) throws IOException { 166 return readLines(new FileReader(s)); 167 } 168 public static final List<String> readLines(URL u) throws IOException { 169 InputStream in = u.openStream(); 170 List<String> r = readLines(in); 171 in.close(); 172 return r; 173 } 174 public static final List<String> readLines(InputStream in) throws IOException { 175 if (in == null) 176 return null; 177 List<String> r = readLines(new InputStreamReader(in)); 178 in.close(); 179 return r; 180 } 181 public static final List<String> readLines(Reader r) throws IOException { 182 List<String> v=new ArrayList<String>(); 183 BufferedReader in=new BufferedReader(r); 184 String l; 185 while ((l=in.readLine())!=null) //if (!(l=l.trim()).equals("")) 186 v.add(l); 187 return v; 188 } 189 public static final String[] readLinesArray(String s) throws IOException { 190 List<String> v=readLines(s); 191 String ret[]=new String[v.size()]; 192 int i=0; 193 for (String line : v) { 194 ret[i++]=line; 195 } 196 return ret; 197 } 198 }