1 16 package org.apache.cocoon.servlet.multipart; 17 18 import java.io.IOException ; 19 import java.io.PushbackInputStream ; 20 21 29 class TokenStream extends PushbackInputStream { 30 31 34 public static final int STATE_NOBOUNDARY = -1; 35 36 39 public static final int STATE_NEXTPART = -2; 40 41 44 public static final int STATE_ENDMULTIPART = -3; 45 46 49 public static final int STATE_ENDOFSTREAM = -4; 50 51 54 public static final int STATE_READING = -5; 55 56 57 private PushbackInputStream in = null; 58 59 60 private byte[] boundary = null; 61 62 63 private int state = STATE_NOBOUNDARY; 64 65 70 public TokenStream(PushbackInputStream in) { 71 this(in,1); 72 } 73 74 80 public TokenStream(PushbackInputStream in, int size) { 81 super(in,size); 82 this.in = in; 83 } 84 85 92 public void setBoundary(byte[] boundary) throws MultipartException { 93 this.boundary = boundary; 94 if (state == STATE_NOBOUNDARY) { 95 state = STATE_READING; 96 } 97 } 98 99 105 public void nextPart() throws MultipartException { 106 if (state != STATE_NEXTPART) { 107 throw new MultipartException("Illegal state"); 108 } 109 state = STATE_READING; 110 } 111 112 117 public int getState() { 118 return state; 119 } 120 121 134 private int readToBoundary(byte[] out) throws IOException { 135 if (state != STATE_READING) { 136 return 0; 137 } 138 int boundaryIndex = 0; 139 int written = 0; 140 int b = in.read(); 141 142 while (true) { 143 while ((byte) b != boundary[0]) { 144 if (b == -1) { 145 state = STATE_ENDOFSTREAM; 146 return written; 147 } 148 out[written++] = (byte) b; 149 150 if (written == out.length) { 151 return written; 152 } 153 b = in.read(); 154 } 155 boundaryIndex = 0; while ((boundaryIndex < boundary.length) 158 && ((byte) b == boundary[boundaryIndex])) { 159 b = in.read(); 160 boundaryIndex++; 161 } 162 163 if (boundaryIndex == boundary.length) { if (b != -1) { 165 if (b == '\r') { state = STATE_NEXTPART; 167 in.read(); 168 } else if (b == '-') { state = STATE_ENDMULTIPART; 170 in.read(); in.read(); in.read(); } else { throw new IOException ( 175 "Unexpected character after boundary"); 176 } 177 } else { state = STATE_ENDOFSTREAM; 179 } 180 return written; 181 } else { if (b != -1) { in.unread(b); } 186 in.unread(boundary, 1, 187 boundaryIndex - 1); out[written++] = boundary[0]; 189 if (written == out.length) { 190 return written; 191 } 192 } 193 b = in.read(); 194 } 195 } 196 197 206 public int read(byte[] out) throws IOException { 207 if (state != STATE_READING) { 208 return 0; 209 } 210 return readToBoundary(out); 211 } 212 213 224 public int read(byte[] out, int off, int len) throws IOException { 225 if ((off < 0) || (off >= out.length)) { 226 throw new IOException ("Buffer offset outside buffer"); 227 } 228 if (off + len >= out.length) { 229 throw new IOException ("Buffer end outside buffer"); 230 } 231 if (len < 0) { 232 throw new IOException ("Length must be a positive integer"); 233 } 234 byte[] buf = new byte[len]; 235 int read = read(buf); 236 if (read > 0) { 237 System.arraycopy(buf, 0, out, off, read); 238 } 239 return read; 240 } 241 242 249 public int read() throws IOException { 250 byte[] buf = new byte[1]; 251 int read = read(buf); 252 253 if (read == 0) { 254 return -1; 255 } else { 256 return buf[0]; 257 } 258 } 259 } 260 | Popular Tags |