001: package com.technoetic.xplanner.util;
002:
003: import org.apache.struts.upload.FormFile;
004:
005: import java.io.ByteArrayInputStream;
006: import java.io.ByteArrayOutputStream;
007: import java.io.FileNotFoundException;
008: import java.io.IOException;
009: import java.io.InputStream;
010: import java.util.Arrays;
011:
012: public class InputStreamFormFile implements FormFile {
013: private byte[] fileData;
014: private String fileName;
015: private int fileSize;
016: private String contentType;
017:
018: public InputStreamFormFile(InputStream inputStream)
019: throws IOException {
020: ByteArrayOutputStream baos = new ByteArrayOutputStream();
021:
022: byte[] buffer = new byte[8192];
023: int bytesRead = 0;
024: while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
025: baos.write(buffer, 0, bytesRead);
026: }
027:
028: fileData = baos.toByteArray();
029: }
030:
031: public String getFileName() {
032: return fileName;
033: }
034:
035: public void setFileName(String fileName) {
036: this .fileName = fileName;
037: }
038:
039: public int getFileSize() {
040: return fileSize;
041: }
042:
043: public void setFileSize(int fileSize) {
044: this .fileSize = fileSize;
045: }
046:
047: public String getContentType() {
048: return contentType;
049: }
050:
051: public void setContentType(String contentType) {
052: this .contentType = contentType;
053: }
054:
055: public byte[] getFileData() throws FileNotFoundException,
056: IOException {
057: return fileData;
058: }
059:
060: public InputStream getInputStream() throws FileNotFoundException,
061: IOException {
062: return new ByteArrayInputStream(fileData);
063: }
064:
065: public void destroy() {
066: fileName = null;
067: fileSize = -1;
068: contentType = null;
069: fileData = null;
070: }
071:
072: public boolean equals(Object obj) {
073: if (obj == null) {
074: return false;
075: }
076: if (obj == this ) {
077: return true;
078: }
079: if (this .getClass() != obj.getClass()) {
080: return false;
081: }
082:
083: InputStreamFormFile that = (InputStreamFormFile) obj;
084: return ((fileData == null ? that.fileData == null : Arrays
085: .equals(fileData, that.fileData))
086: && (fileName == null ? that.fileName == null : fileName
087: .equals(that.fileName))
088: && (fileSize == that.fileSize)
089: && (contentType == null ? that.contentType == null
090: : contentType.equals(that.contentType)) && true);
091: }
092:
093: public String toString() {
094: StringBuffer sb = new StringBuffer();
095: sb.append("InputStreamFormFile - ");
096: sb.append("fileName: " + fileName + "\n");
097: sb.append("fileSize: " + fileSize + "\n");
098: sb.append("contentType: " + contentType + "\n");
099: sb.append("fileData: " + new String(fileData) + "\n");
100: return sb.toString();
101: }
102:
103: }
|