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