01: /*
02: * Copyright 2005 Joe Walker
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.directwebremoting.impl;
17:
18: import java.io.IOException;
19: import java.io.InputStream;
20: import java.io.OutputStream;
21:
22: import org.directwebremoting.extend.FileGenerator;
23: import org.directwebremoting.io.FileTransfer;
24:
25: /**
26: * An implementation of {@link FileGenerator} that uses a {@link FileTransfer}.
27: * @author Joe Walker [joe at getahead dot ltd dot uk]
28: */
29: public class FileTransferFileGenerator implements FileGenerator {
30: /**
31: * @param fileTransfer The FileTransfer user object that we base ourselves on
32: */
33: public FileTransferFileGenerator(FileTransfer fileTransfer) {
34: this .fileTransfer = fileTransfer;
35: }
36:
37: /* (non-Javadoc)
38: * @see org.directwebremoting.extend.FileGenerator#generateFile(java.io.OutputStream)
39: */
40: public void generateFile(OutputStream out) throws IOException {
41: InputStream in = fileTransfer.getInputStream();
42: byte[] buffer = new byte[1024];
43:
44: while (true) {
45: int length = in.read(buffer);
46:
47: if (length <= 0) {
48: break;
49: }
50:
51: out.write(buffer, 0, length);
52: }
53: }
54:
55: /* (non-Javadoc)
56: * @see org.directwebremoting.extend.FileGenerator#getMimeType()
57: */
58: public String getMimeType() {
59: return fileTransfer.getMimeType();
60: }
61:
62: /* (non-Javadoc)
63: * @see org.directwebremoting.extend.FileGenerator#getFilename()
64: */
65: public String getFilename() {
66: return fileTransfer.getName();
67: }
68:
69: private FileTransfer fileTransfer;
70: }
|