%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.jetspeed.util.Streams |
|
|
1 | /* |
|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
3 | * contributor license agreements. See the NOTICE file distributed with |
|
4 | * this work for additional information regarding copyright ownership. |
|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
6 | * (the "License"); you may not use this file except in compliance with |
|
7 | * the License. You may obtain a copy of the License at |
|
8 | * |
|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 | * |
|
11 | * Unless required by applicable law or agreed to in writing, software |
|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 | * See the License for the specific language governing permissions and |
|
15 | * limitations under the License. |
|
16 | */ |
|
17 | package org.apache.jetspeed.util; |
|
18 | ||
19 | import java.io.ByteArrayOutputStream; |
|
20 | import java.io.InputStream; |
|
21 | import java.io.IOException; |
|
22 | import java.io.OutputStream; |
|
23 | import java.io.OutputStreamWriter; |
|
24 | import java.io.Reader; |
|
25 | import java.io.Writer; |
|
26 | import java.io.InputStreamReader; |
|
27 | ||
28 | /** |
|
29 | * Utility functions related to Streams. |
|
30 | * |
|
31 | * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a> |
|
32 | * @version $Id: Streams.java 516448 2007-03-09 16:25:47Z ate $ |
|
33 | */ |
|
34 | 0 | public class Streams |
35 | { |
|
36 | static final int BLOCK_SIZE=4096; |
|
37 | ||
38 | public static void drain(InputStream r,OutputStream w) throws IOException |
|
39 | { |
|
40 | 0 | byte[] bytes=new byte[BLOCK_SIZE]; |
41 | try |
|
42 | { |
|
43 | 0 | int length=r.read(bytes); |
44 | 0 | while(length!=-1) |
45 | { |
|
46 | 0 | if(length!=0) |
47 | { |
|
48 | 0 | w.write(bytes,0,length); |
49 | } |
|
50 | 0 | length=r.read(bytes); |
51 | } |
|
52 | } |
|
53 | finally |
|
54 | { |
|
55 | 0 | bytes=null; |
56 | 0 | } |
57 | ||
58 | 0 | } |
59 | ||
60 | public static void drain(Reader r,Writer w) throws IOException |
|
61 | { |
|
62 | 0 | char[] bytes=new class="keyword">char[BLOCK_SIZE]; |
63 | try |
|
64 | { |
|
65 | 0 | int length=r.read(bytes); |
66 | 0 | while(length!=-1) |
67 | { |
|
68 | 0 | if(length!=0) |
69 | { |
|
70 | 0 | w.write(bytes,0,length); |
71 | } |
|
72 | 0 | length=r.read(bytes); |
73 | } |
|
74 | } |
|
75 | finally |
|
76 | { |
|
77 | 0 | bytes=null; |
78 | 0 | } |
79 | ||
80 | 0 | } |
81 | ||
82 | public static void drain(Reader r,OutputStream os) throws IOException |
|
83 | { |
|
84 | 0 | Writer w=new OutputStreamWriter(os); |
85 | 0 | drain(r,w); |
86 | 0 | w.flush(); |
87 | 0 | } |
88 | ||
89 | public static void drain(InputStream is, Writer w) throws IOException |
|
90 | { |
|
91 | 0 | Reader r = new InputStreamReader(is); |
92 | 0 | drain(r,w); |
93 | 0 | w.flush(); |
94 | 0 | } |
95 | ||
96 | public static byte[] drain(InputStream r) throws IOException |
|
97 | { |
|
98 | 0 | ByteArrayOutputStream bytes=new ByteArrayOutputStream(); |
99 | 0 | drain(r,bytes); |
100 | 0 | return bytes.toByteArray(); |
101 | } |
|
102 | ||
103 | public static String getAsString(InputStream is) |
|
104 | { |
|
105 | 0 | int c=0; |
106 | 0 | char lineBuffer[]=new class="keyword">char[128], buf[]=lineBuffer; |
107 | 0 | int room= buf.length, offset=0; |
108 | try |
|
109 | { |
|
110 | loop: while (true) |
|
111 | { |
|
112 | // read chars into a buffer which grows as needed |
|
113 | 0 | switch (c = is.read() ) |
114 | { |
|
115 | 0 | case -1: break loop; |
116 | ||
117 | 0 | default: if (--room < 0) |
118 | { |
|
119 | 0 | buf = new char[offset + 128]; |
120 | 0 | room = buf.length - offset - 1; |
121 | 0 | System.arraycopy(lineBuffer, 0, |
122 | buf, 0, offset); |
|
123 | 0 | lineBuffer = buf; |
124 | } |
|
125 | 0 | buf[offset++] = (char) c; |
126 | 0 | break; |
127 | } |
|
128 | } |
|
129 | } |
|
130 | 0 | catch(IOException ioe) |
131 | { |
|
132 | 0 | ioe.printStackTrace(); |
133 | 0 | } |
134 | 0 | if ((c == -1) && (offset == 0)) |
135 | { |
|
136 | 0 | return null; |
137 | } |
|
138 | 0 | return String.copyValueOf(buf, 0, offset); |
139 | } |
|
140 | ||
141 | ||
142 | ||
143 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |