1 15 package org.apache.tapestry.workbench.upload; 16 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.io.OutputStream ; 20 import java.io.StringWriter ; 21 import java.io.Writer ; 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 34 35 public abstract class UploadResults extends BasePage 36 { 37 public abstract void setFile(IUploadFile file); 38 39 public abstract void setFileDump(String fileDump); 40 41 public void activate(IUploadFile file, boolean showAscii, int bytesPerLine) 42 { 43 setFile(file); 44 45 StringWriter writer = null; 46 BinaryDumpOutputStream out = null; 47 InputStream in = null; 48 49 try 50 { 51 in = file.getStream(); 52 53 writer = new StringWriter (); 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 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 stream) 97 { 98 if (stream != null) 99 { 100 try 101 { 102 stream.close(); 103 } 104 catch (IOException ex) 105 { 106 } 107 } 108 } 109 110 private void close(OutputStream stream) 111 { 112 if (stream != null) 113 { 114 try 115 { 116 stream.close(); 117 } 118 catch (IOException ex) 119 { 120 } 121 } 122 } 123 124 private void close(Writer writer) 125 { 126 if (writer != null) 127 { 128 try 129 { 130 writer.close(); 131 } 132 catch (IOException ex) 133 { 134 } 135 } 136 } 137 } | Popular Tags |