1 /**
2 * AUTOHIT 2003
3 * Copyright Erich P Gatejen (c) 1989,1997,2003,2004
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Additional license information can be found in the documentation.
19 * @author Erich P Gatejen
20 */
21 package autohit.universe;
22 import java.io.BufferedReader;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.io.OutputStream;
26 import java.io.OutputStreamWriter;
27 import java.io.BufferedWriter;
28
29 /**
30 * Utilities for universe interaction. Universe clients are welcome to use
31 * them.
32 *
33 * @author Erich P. Gatejen
34 * @version 1.0 <i>Version History</i><code>EPG - Rewrite - 13Sep03</code>
35 */
36 public class UniverseUtils {
37
38 // CONSTANTS
39 private final static int BUFFER_SIZE = 1024;
40
41 /**
42 * Read from an InputStream into a String, using a default encoding.
43 *
44 * @param is
45 * the InputStream
46 * @return string if successful
47 * @throws UniverseException
48 */
49 public static String load2String(InputStream is) throws UniverseException {
50 return load2String(is, null);
51 }
52
53 /**
54 * Read from an InputStream into a String, using a specific encoding.
55 *
56 * @param is
57 * the InputStream
58 * @param charSetName
59 * is the charset to use for encoding the stream.
60 * @return string if successful
61 * @throws UniverseException
62 */
63 public static String load2String(InputStream is, String charSetName) throws UniverseException {
64
65 StringBuffer buffer = new StringBuffer();
66 try {
67
68 char[] buf = new char[BUFFER_SIZE];
69 int sbuf;
70
71 BufferedReader bis;
72 if (charSetName != null) {
73 bis = new BufferedReader(new InputStreamReader(is, charSetName));
74 } else {
75 bis = new BufferedReader(new InputStreamReader(is));
76 }
77
78 sbuf = bis.read(buf, 0, BUFFER_SIZE);
79 while (sbuf > 0) {
80 buffer.append(buf, 0, sbuf);
81 sbuf = bis.read(buf, 0, BUFFER_SIZE);
82 }
83 } catch (Exception e) {
84 throw new UniverseException(
85 "Exception loading to String. message=" + e.getMessage(),
86 UniverseException.UE_IO_ERROR);
87 } finally {
88 try {
89 is.close();
90 } catch (Exception e) {
91 // don't care;
92 }
93 }
94 return buffer.toString();
95 }
96
97 /**
98 * Write from a String to an OutputStream, using a default encoding.
99 *
100 * @param os
101 * the OutputStream
102 * @param text
103 * the string to save
104 * @throws UniverseException
105 */
106 public static void saveString(OutputStream os, String text) throws UniverseException {
107 saveString(os, text, null);
108 }
109
110 /**
111 * Write from a String to an OutputStream, using a specific encoding.
112 *
113 * @param os
114 * the OutputStream
115 * @param text
116 * the string to save
117 * @param charSetName
118 * is the charset to use for encoding the stream.
119 * @throws UniverseException
120 */
121 public static void saveString(OutputStream os, String text, String charSetName) throws UniverseException {
122
123 BufferedWriter bw = null;
124 try {
125
126 if (charSetName != null) {
127 bw = new BufferedWriter(new OutputStreamWriter(os, charSetName));
128 } else {
129 bw = new BufferedWriter(new OutputStreamWriter(os));
130 }
131
132 bw.write(text);
133
134 } catch (Exception e) {
135 throw new UniverseException(
136 "Exception saving String. message=" + e.getMessage(),
137 UniverseException.UE_IO_ERROR);
138 } finally {
139 try {
140 bw.close();
141 } catch (Exception e) {
142 // don't care;
143 }
144 }
145 }
146
147 }
|