KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > workbench > upload > UploadResults


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.workbench.upload;
16
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.io.OutputStream JavaDoc;
20 import java.io.StringWriter JavaDoc;
21 import java.io.Writer JavaDoc;
22
23 import org.apache.hivemind.ApplicationRuntimeException;
24 import org.apache.tapestry.IRequestCycle;
25 import org.apache.tapestry.html.BasePage;
26 import org.apache.tapestry.request.IUploadFile;
27 import org.apache.tapestry.util.io.BinaryDumpOutputStream;
28
29 /**
30  * Displays the uploaded file as a hexadecimal dump.
31  *
32  * @author Howard Lewis Ship
33  */

34
35 public abstract class UploadResults extends BasePage
36 {
37     public abstract void setFile(IUploadFile file);
38
39     public abstract void setFileDump(String JavaDoc fileDump);
40
41     public void activate(IUploadFile file, boolean showAscii, int bytesPerLine)
42     {
43         setFile(file);
44
45         StringWriter JavaDoc writer = null;
46         BinaryDumpOutputStream out = null;
47         InputStream JavaDoc in = null;
48
49         try
50         {
51             in = file.getStream();
52
53             writer = new StringWriter JavaDoc();
54             out = new BinaryDumpOutputStream(writer);
55
56             out.setShowAscii(showAscii);
57             out.setBytesPerLine(bytesPerLine);
58
59             byte[] buffer = new byte[1000];
60
61             while (true)
62             {
63                 int length = in.read(buffer);
64
65                 if (length < 0)
66                     break;
67
68                 out.write(buffer, 0, length);
69             }
70
71             in.close();
72             in = null;
73
74             out.close();
75             out = null;
76
77             setFileDump(writer.getBuffer().toString());
78
79             writer.close();
80             writer = null;
81         }
82         catch (IOException JavaDoc ex)
83         {
84             throw new ApplicationRuntimeException("Unable to display file.", this, null, ex);
85         }
86         finally
87         {
88             close(in);
89             close(out);
90             close(writer);
91         }
92
93         getRequestCycle().activate(this);
94     }
95
96     private void close(InputStream JavaDoc stream)
97     {
98         if (stream != null)
99         {
100             try
101             {
102                 stream.close();
103             }
104             catch (IOException JavaDoc ex)
105             {
106             }
107         }
108     }
109
110     private void close(OutputStream JavaDoc stream)
111     {
112         if (stream != null)
113         {
114             try
115             {
116                 stream.close();
117             }
118             catch (IOException JavaDoc ex)
119             {
120             }
121         }
122     }
123
124     private void close(Writer JavaDoc writer)
125     {
126         if (writer != null)
127         {
128             try
129             {
130                 writer.close();
131             }
132             catch (IOException JavaDoc ex)
133             {
134             }
135         }
136     }
137 }
Popular Tags