KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > util > ResponseUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  *
23  * Portions Copyright Apache Software Foundation.
24  */

25
26 package org.apache.catalina.util;
27
28 import java.io.*;
29 import javax.servlet.*;
30
31 public final class ResponseUtil {
32
33     /**
34      * Copies the contents of the specified input stream to the specified
35      * output stream.
36      *
37      * @param istream The input stream to read from
38      * @param ostream The output stream to write to
39      *
40      * @return Exception that occurred during processing, or null
41      */

42     public static IOException copy(InputStream istream,
43                                    ServletOutputStream ostream) {
44
45         IOException exception = null;
46         byte buffer[] = new byte[2048];
47         int len = buffer.length;
48         while (true) {
49             try {
50                 len = istream.read(buffer);
51                 if (len == -1)
52                     break;
53                 ostream.write(buffer, 0, len);
54             } catch (IOException e) {
55                 exception = e;
56                 len = -1;
57                 break;
58             }
59         }
60         return exception;
61
62     }
63
64
65     /**
66      * Copies the contents of the specified input stream to the specified
67      * output stream.
68      *
69      * @param reader The reader to read from
70      * @param writer The writer to write to
71      *
72      * @return Exception that occurred during processing, or null
73      */

74     public static IOException copy(Reader reader, PrintWriter writer) {
75
76         IOException exception = null;
77         char buffer[] = new char[2048];
78         int len = buffer.length;
79         while (true) {
80             try {
81                 len = reader.read(buffer);
82                 if (len == -1)
83                     break;
84                 writer.write(buffer, 0, len);
85             } catch (IOException e) {
86                 exception = e;
87                 len = -1;
88                 break;
89             }
90         }
91         return exception;
92
93     }
94
95 }
96
Popular Tags