01: package org.romaframework.aspect.view.echo2;
02:
03: import java.io.File;
04: import java.io.FileInputStream;
05: import java.io.IOException;
06: import java.io.InputStream;
07: import java.io.OutputStream;
08:
09: import nextapp.echo2.app.filetransfer.AbstractDownloadProvider;
10:
11: public class FileDownloadProvider extends AbstractDownloadProvider {
12: private File file;
13:
14: private int bufferSize = DEF_BUFFER_SIZE;
15:
16: private String contentType = DEF_CONTENT_TYPE;
17:
18: private String fileName;
19:
20: private static final String DEF_CONTENT_TYPE = "text/plain";
21:
22: private static final int DEF_BUFFER_SIZE = 4096;
23:
24: public FileDownloadProvider(File iFile, String iContentType) {
25: file = iFile;
26: contentType = iContentType;
27: }
28:
29: public FileDownloadProvider(File iFile) {
30: file = iFile;
31: }
32:
33: /**
34: * @see nextapp.echo2.app.filetransfer.ResourceDownloadProvider#writeFile(java.io.OutputStream)
35: */
36: public void writeFile(OutputStream out) throws IOException {
37: InputStream in = null;
38: byte[] buffer = new byte[bufferSize];
39: int bytesRead = 0;
40:
41: try {
42: in = new FileInputStream(file);
43: if (in == null) {
44: throw new IllegalArgumentException(
45: "Specified resource does not exist: "
46: + getFileName() + ".");
47: }
48: do {
49: bytesRead = in.read(buffer);
50: if (bytesRead > 0) {
51: out.write(buffer, 0, bytesRead);
52: }
53: } while (bytesRead > 0);
54: } finally {
55: if (in != null) {
56: try {
57: in.close();
58: } catch (IOException ex) {
59: }
60: }
61: }
62: }
63:
64: @Override
65: public int getSize() {
66: return (int) file.length();
67: }
68:
69: @Override
70: public String getFileName() {
71: if (fileName != null)
72: return fileName;
73: else
74: return file.getName();
75: }
76:
77: public void setFileName(String iFileName) {
78: fileName = iFileName;
79: }
80:
81: public String getContentType() {
82: return contentType;
83: }
84:
85: public int getBufferSize() {
86: return bufferSize;
87: }
88:
89: public void setBufferSize(int bufferSize) {
90: this .bufferSize = bufferSize;
91: }
92:
93: public void setContentType(String contentType) {
94: this.contentType = contentType;
95: }
96: }
|