001: package org.enhydra.shark.webclient.spec;
002:
003: import java.io.BufferedInputStream;
004: import java.io.InputStream;
005: import java.util.ArrayList;
006: import java.util.Iterator;
007:
008: /**
009: * <p>Title: EDMS</p>
010: * <p>Description: Document Management Project</p>
011: * <p>Copyright: Copyright (c) 2004</p>
012: * <p>Company: Prozone</p>
013: * @author Vladimir Radisic
014: * @version 1.0
015: */
016: public class MultipartReader {
017:
018: private String boundaryStr = null;
019: private byte[] boundaryByt = null;
020:
021: private BufferedInputStream bufStream = null;
022: private byte[] streamData = null;
023: private ArrayList entitys = new ArrayList();
024:
025: private String encoding = null;
026:
027: /**
028: *
029: * @param stream
030: */
031: public MultipartReader(InputStream stream) {
032: this .bufStream = new BufferedInputStream(stream);
033: }
034:
035: /**
036: *
037: * @param stream
038: * @param boundary
039: */
040: public MultipartReader(InputStream stream, String boundary) {
041: this .bufStream = new BufferedInputStream(stream);
042: this .setBoundary(boundary);
043: }
044:
045: public void setBoundary(String boundary) {
046: this .boundaryStr = "--" + boundary;
047: this .boundaryByt = boundaryStr.getBytes();
048: }
049:
050: /**
051: * Reads input stream into inner byte array
052: * @param close true if this method should close the stream
053: * or false otherwise
054: */
055: public void readStream(boolean close) throws Exception {
056: ArrayList streamBuffers = new ArrayList();
057: byte[] buffer = null;
058: int length = -1;
059: int lengthCommon = 0;
060:
061: try {
062: do {
063: buffer = new byte[1024];
064: length = this .bufStream.read(buffer);
065:
066: if (length == buffer.length) {
067: streamBuffers.add(buffer);
068: } else if (length != -1) {
069: byte[] toAdd = ByteArrayUtility.cutArray(buffer, 0,
070: length);
071: streamBuffers.add(toAdd);
072: }
073:
074: if (length != -1) {
075: lengthCommon = lengthCommon + length;
076:
077: }
078: } while (length != -1);
079:
080: if (close)
081: bufStream.close();
082: bufStream = null;
083:
084: this .streamData = new byte[lengthCommon];
085:
086: length = 0;
087: for (int i = 0; i < streamBuffers.size(); i++) {
088: byte[] part = (byte[]) streamBuffers.get(i);
089: System.arraycopy(part, 0, this .streamData, length,
090: part.length);
091: length = length + part.length;
092: }
093: streamBuffers = null;
094:
095: }
096:
097: catch (Exception ex) {
098: ex.printStackTrace();
099: }
100: }
101:
102: public void setEncoding(String encoding) {
103: this .encoding = encoding;
104: }
105:
106: public void makeEntityParts() {
107: int[] indexes = ByteArrayUtility.findAllIndexes(
108: this .boundaryByt, this .streamData);
109: if (indexes == null || indexes.length == 0)
110: return;
111: else if (indexes[0] != 0)
112: return;
113:
114: byte[] entytyData = null;
115: for (int i = 0; i < indexes.length - 1; i++) { // last boundary should be end boundary
116: entytyData = ByteArrayUtility.cutArray(this .streamData,
117: indexes[i] + this .boundaryByt.length + 2,
118: indexes[i + 1] - 2);
119: EntityPart entPart = new EntityPart(entytyData,
120: this .encoding);
121: this .entitys.add(entPart);
122: }
123:
124: }
125:
126: public Iterator getEntityParts() {
127: return this .entitys.iterator();
128: }
129:
130: public void clearStreamContent() {
131: this .streamData = null;
132: }
133:
134: public byte[] getStreamContent() {
135: return this.streamData;
136: }
137:
138: }
|