PageRenderTime 27ms CodeModel.GetById 14ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/WebAccess/src/com/ideal/webreader/Util.java

http://eyes-free.googlecode.com/
Java | 122 lines | 82 code | 15 blank | 25 comment | 4 complexity | de0fb89e3c9ac44ac2447c0291d249b8 MD5 | raw file
  1/*
  2	Copyright (C) 2008 Jeffrey Sharkey, http://jsharkey.org/
  3	
  4	This program is free software: you can redistribute it and/or modify
  5	it under the terms of the GNU General Public License as published by
  6	the Free Software Foundation, either version 3 of the License, or
  7	(at your option) any later version.
  8	
  9	This program is distributed in the hope that it will be useful,
 10	but WITHOUT ANY WARRANTY; without even the implied warranty of
 11	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12	GNU General Public License for more details.
 13	
 14	You should have received a copy of the GNU General Public License
 15	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 16 */
 17
 18package com.ideal.webreader;
 19
 20import java.io.ByteArrayOutputStream;
 21import java.io.File;
 22import java.io.FileInputStream;
 23import java.io.InputStream;
 24import java.net.URL;
 25import java.net.URLConnection;
 26
 27import android.content.res.Resources;
 28
 29public class Util {
 30
 31    public final static String TAG = Util.class.toString();
 32
 33    /**
 34     * Return a specific raw resource contents as a String value.
 35     */
 36    public static String getRawString(Resources res, int id) throws Exception {
 37        String result = null;
 38        InputStream is = null;
 39        try {
 40            is = res.openRawResource(id);
 41            byte[] raw = new byte[is.available()];
 42            is.read(raw);
 43            result = new String(raw);
 44        } catch (Exception e) {
 45            throw new Exception("Problem while trying to read raw", e);
 46        } finally {
 47            try {
 48                is.close();
 49            } catch (Exception e) {
 50            }
 51        }
 52        return result;
 53
 54    }
 55
 56    /**
 57     * Return a specific file contents as a String value.
 58     */
 59    public static String getFileString(File file) throws Exception {
 60        String result = null;
 61        InputStream is = null;
 62        ByteArrayOutputStream os = new ByteArrayOutputStream();
 63        try {
 64            is = new FileInputStream(file);
 65
 66            int bytesRead;
 67            byte[] buffer = new byte[1024];
 68            while ((bytesRead = is.read(buffer)) != -1) {
 69                os.write(buffer, 0, bytesRead);
 70            }
 71
 72            os.flush();
 73            result = new String(os.toByteArray());
 74        } catch (Exception e) {
 75            throw new Exception("Problem while trying to read file", e);
 76        } finally {
 77            try {
 78                os.close();
 79                is.close();
 80            } catch (Exception e) {
 81            }
 82        }
 83        return result;
 84
 85    }
 86
 87    /**
 88     * Return a specific url contents as a String value.
 89     */
 90    public static String getUrlString(String remoteUrl) throws Exception {
 91        String result = null;
 92        InputStream is = null;
 93        ByteArrayOutputStream os = new ByteArrayOutputStream();
 94        try {
 95            URL url = new URL(remoteUrl);
 96            URLConnection connection = url.openConnection();
 97            connection.setConnectTimeout(5000);
 98            connection.setReadTimeout(5000);
 99            connection.connect();
100            is = connection.getInputStream();
101
102            int bytesRead;
103            byte[] buffer = new byte[1024];
104            while ((bytesRead = is.read(buffer)) != -1) {
105                os.write(buffer, 0, bytesRead);
106            }
107
108            os.flush();
109            result = new String(os.toByteArray());
110        } catch (Exception e) {
111            throw new Exception("Problem while trying to read url", e);
112        } finally {
113            try {
114                os.close();
115                is.close();
116            } catch (Exception e) {
117            }
118        }
119        return result;
120
121    }
122}