KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonicsystems > jarjar > IoUtils


1 /*
2   Jar Jar Links - A utility to repackage and embed Java libraries
3   Copyright (C) 2004 Tonic Systems, Inc.
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
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program; see the file COPYING. if not, write to
17   the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18   Boston, MA 02111-1307 USA
19 */

20
21 package com.tonicsystems.jarjar;
22
23 import java.io.*;
24
25 class IoUtils
26 {
27     public static final String JavaDoc LINE_SEPARATOR = System.getProperty("line.separator");
28     
29     private IoUtils() {
30     }
31
32     public static String JavaDoc readIntoString(InputStream in) throws IOException {
33         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
34         BufferedReader r = new BufferedReader(new InputStreamReader(in, "UTF-8"));
35         String JavaDoc line = null;
36         while ((line = r.readLine()) != null)
37             sb.append(line).append(LINE_SEPARATOR);
38         return sb.toString();
39     }
40
41     public static String JavaDoc escapeStringLiteral(String JavaDoc value) {
42         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
43         sb.append("\"");
44         char[] chars = value.toCharArray();
45         for (int i = 0, size = chars.length; i < size; i++) {
46             char ch = chars[i];
47             switch (ch) {
48             case '\n': sb.append("\\n"); break;
49             case '\r': sb.append("\\r"); break;
50             case '\b': sb.append("\\b"); break;
51             case '\f': sb.append("\\f"); break;
52             case '\t': sb.append("\\t"); break;
53             case '\"': sb.append("\\\""); break;
54             case '\\': sb.append("\\\\"); break;
55             default:
56                 sb.append(ch);
57             }
58         }
59         sb.append("\"");
60         return sb.toString();
61     }
62
63     public static byte[] toByteArray(InputStream is, byte[] buf) throws IOException {
64         ByteArrayOutputStream baos = new ByteArrayOutputStream();
65         pipe(is, baos, buf);
66         return baos.toByteArray();
67     }
68
69     public static void pipe(InputStream is, OutputStream out, byte[] buf) throws IOException {
70         for (;;) {
71             int amt = is.read(buf);
72             if (amt < 0)
73                 break;
74             out.write(buf, 0, amt);
75         }
76     }
77 }
78
Popular Tags