1 3 package jodd.servlet.upload; 4 5 import jodd.io.FastByteArrayOutputStream; 6 7 import java.io.BufferedInputStream ; 8 import java.io.InputStream ; 9 import java.io.IOException ; 10 import java.io.OutputStream ; 11 12 17 public class MultipartRequestInputStream extends BufferedInputStream { 18 19 public MultipartRequestInputStream(InputStream in) { 20 super(in); 21 } 22 23 public byte readByte() throws IOException { 24 int i = super.read(); 25 if (i == -1) { 26 throw new IOException ("Unable to read data from HTTP request."); 27 } 28 return (byte) i; 29 } 30 31 public void skipBytes(int i) throws IOException { 32 long len = super.skip(i); 33 if (len != i) { 34 throw new IOException ("Unable to skip data in HTTP request."); 35 } 36 } 37 38 39 41 protected byte[] boundary; 42 43 46 public byte[] readBoundary() throws IOException { 47 FastByteArrayOutputStream boundaryOutput = new FastByteArrayOutputStream(); 48 byte b; 49 while ((b = readByte()) != '\r') { 50 boundaryOutput.write(b); 51 } 52 if (boundaryOutput.size() == 0) { 53 throw new IOException ("Problems with parsing request: invalid boundary."); 54 } 55 skipBytes(1); 56 boundary = new byte[boundaryOutput.size() + 2]; 57 System.arraycopy(boundaryOutput.toByteArray(), 0, boundary, 2, boundary.length - 2); 58 boundary[0] = '\r'; 59 boundary[1] = '\n'; 60 return boundary; 61 } 62 63 64 65 67 protected FileUploadHeader lastHeader; 68 69 public FileUploadHeader getLastHeader() { 70 return lastHeader; 71 } 72 73 77 public FileUploadHeader readDataHeader(String encoding) throws IOException { 78 String dataHeader = readDataHeaderString(encoding); 79 if (dataHeader != null) { 80 lastHeader = new FileUploadHeader(dataHeader); 81 } else { 82 lastHeader = null; 83 } 84 return lastHeader; 85 } 86 87 88 protected String readDataHeaderString(String encoding) throws IOException { 89 FastByteArrayOutputStream data = new FastByteArrayOutputStream(); 90 byte b; 91 while (true) { 92 if ((b = readByte()) != '\r') { 94 data.write(b); 95 continue; 96 } 97 mark(4); 98 skipBytes(1); 99 int i = read(); 100 if (i == -1) { 101 return null; 103 } 104 if (i == '\r') { 105 reset(); 106 break; 107 } 108 reset(); 109 data.write(b); 110 } 111 skipBytes(3); 112 if (encoding != null) { 113 return data.toString(encoding); 114 } else { 115 return data.toString(); 116 } 117 } 118 119 120 122 127 public int copyAll(OutputStream out) throws IOException { 128 int count = 0; 129 while (true) { 130 byte b = readByte(); 131 if (isBoundary(b)) { 132 break; 133 } 134 out.write(b); 135 count++; 136 } 137 return count; 138 } 139 140 144 public int copyMax(OutputStream out, int maxBytes) throws IOException { 145 int count = 0; 146 while (true) { 147 byte b = readByte(); 148 if (isBoundary(b)) { 149 break; 150 } 151 out.write(b); 152 count++; 153 if (count == maxBytes) { 154 return count; 155 } 156 } 157 return count; 158 } 159 160 163 public int skipToBoundary() throws IOException { 164 int count = 0; 165 while (true) { 166 byte b = readByte(); 167 count++; 168 if (isBoundary(b)) { 169 break; 170 } 171 } 172 return count; 173 } 174 175 179 public boolean isBoundary(byte b) throws IOException { 180 int boundaryLen = boundary.length; 181 mark(boundaryLen + 1); 182 int bpos = 0; 183 while (b == boundary[bpos]) { 184 b = readByte(); 185 bpos++; 186 if (bpos == boundaryLen) { 187 return true; } 189 } 190 reset(); 191 return false; 192 } 193 } 194 | Popular Tags |