Source code: com/RuntimeCollective/webapps/form/DBFileForm.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/form/DBFileForm.java,v 1.3 2003/09/30 15:13:13 joe Exp $
2 * $Revision: 1.3 $
3 * $Date: 2003/09/30 15:13:13 $
4 *
5 * ====================================================================
6 *
7 * Josephine : http://www.runtime-collective.com/josephine/index.html
8 *
9 * Copyright (C) 2003 Runtime Collective
10 *
11 * This product includes software developed by the
12 * Apache Software Foundation (http://www.apache.org/).
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30 package com.RuntimeCollective.webapps.form;
31
32 import com.RuntimeCollective.webapps.form.DataTypeForm;
33 import com.RuntimeCollective.webapps.RuntimeParameters;
34
35 import org.apache.struts.upload.FormFile;
36 import java.io.IOException;
37
38 import javax.servlet.http.HttpServletRequest;
39 import org.apache.struts.action.ActionMapping;
40
41 /** Represents the information stored in a DBFile.
42 *
43 * <p>By default, no validation is performed.</p>
44 *
45 * @author Lee Denison (lee@runtime-collective.com)
46 * @version $Id: DBFileForm.java,v 1.3 2003/09/30 15:13:13 joe Exp $
47 */
48 public abstract class DBFileForm extends DataTypeForm {
49
50 // == Contstructors ====================================================
51
52 public DBFileForm() {
53 initialise();
54 }
55
56
57 // == Properties =======================================================
58
59 private FormFile file;
60 /** get the file. */
61 public FormFile getFile() { return file; }
62 /** set the file. */
63 public void setFile(FormFile file) { this.file = file; }
64
65 // These variable allow the form bean to cache the file data
66 // from the last time getFileData() was called and only recalculate
67 // it if something different is being asked for.
68 private byte[] fileData;
69 private int bytesRead;
70 private boolean eof;
71
72 public byte[] getFileData(int maxBytes) {
73 if (null != file &&
74 (null == fileData ||
75 bytesRead > maxBytes ||
76 (bytesRead < maxBytes && !eof))) {
77
78 try {
79 byte[] buffer = new byte[maxBytes];
80
81 bytesRead = file.getInputStream().read(buffer);
82
83 eof = maxBytes > bytesRead;
84
85 if (-1 == bytesRead) {
86 fileData = new byte[0];
87 } else if (eof) {
88 fileData = new byte[bytesRead];
89
90 System.arraycopy(buffer, 0, fileData, 0, fileData.length);
91 } else {
92 fileData = buffer;
93 }
94 } catch (IOException e) {
95 RuntimeParameters.logError(this,
96 "invalid FormFile",
97 e);
98 fileData = new byte[0];
99 bytesRead = 0;
100 eof = true;
101 }
102
103 }
104
105 return fileData;
106 }
107
108 public boolean fileLongerThan(int size) {
109 getFileData(size);
110
111 return !eof;
112 }
113
114 // == Form Methods =====================================================
115
116 /** Reset all properties to their default values.
117 * @param mapping The mapping used to select this instance
118 * @param request The servlet request we are processing
119 */
120 public void reset(ActionMapping mapping, HttpServletRequest request) {
121 initialise();
122 }
123
124
125 // == Private Methods ==================================================
126
127 /* initialise the form properties. */
128 private void initialise() {
129 file = null;
130 fileData = null;
131 bytesRead = -1;
132 eof = false;
133 }
134
135 }