KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > util > StreamHelper


1 package org.apache.slide.projector.util;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.OutputStream JavaDoc;
7
8 import org.apache.slide.projector.value.StreamableValue;
9
10 public class StreamHelper {
11     public static String JavaDoc streamToString(StreamableValue resource) throws IOException JavaDoc {
12         ByteArrayOutputStream JavaDoc outputStream = new ByteArrayOutputStream JavaDoc();
13         copy(resource.getInputStream(), outputStream);
14         return outputStream.toString(resource.getCharacterEncoding());
15     }
16
17     public static void copy(InputStream JavaDoc inputStream, OutputStream JavaDoc outputStream) throws IOException JavaDoc {
18         byte buffer[] = new byte[1024];
19         int len = buffer.length;
20         while (true) {
21             len = inputStream.read(buffer);
22             if (len == -1) break;
23             outputStream.write(buffer, 0, len);
24         }
25         inputStream.close();
26         outputStream.close();
27     }
28 }
Popular Tags