1 package net.matuschek.util; 2 3 import java.io.FilterInputStream ; 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 import java.util.Enumeration ; 7 import java.util.Vector ; 8 36 44 45 46 75 public class ChunkedInputStream extends FilterInputStream 76 { 77 78 private int contentLength; 79 private byte[] b1 = new byte[1]; 80 81 82 private int chunkCount = 0; 83 84 private Vector <String > footerNames = null; 85 private Vector <String > footerValues = null; 86 87 90 public ChunkedInputStream( InputStream in ) 91 { 92 super(in); 93 contentLength = 0; 94 } 95 96 97 103 public int read() throws IOException 104 { 105 if (read(b1,0,1) == -1 ) { 106 return -1; 107 } 108 109 110 return b1[0]; 111 } 112 113 114 122 public int read( byte[] b, int off, int len ) throws IOException 123 { 124 if (chunkCount == 0) { 125 startChunk(); 126 if (chunkCount == 0) { 127 return -1; 128 } 129 } 130 int toRead = Math.min( chunkCount, len ); 131 int r = in.read( b, off, toRead ); 132 133 if ( r != -1 ) { 134 chunkCount -= r; 135 } 136 return r; 137 } 138 139 140 143 private void startChunk() throws IOException 144 { 145 String line = readLine(); 146 if (line.equals("")) { 147 line=readLine(); 148 } 149 150 try { 151 chunkCount = Integer.parseInt(line.trim(),16); 152 } catch (NumberFormatException e) { 153 throw new IOException ("malformed chunk ("+line+")"); 154 } 155 contentLength += chunkCount; 156 if ( chunkCount == 0 ) { 157 readFooters(); 158 } 159 160 } 161 162 163 166 private void readFooters() throws IOException 167 { 168 footerNames = new Vector <String >(); 169 footerValues = new Vector <String >(); 170 String line; 171 while ( true ) { 172 line = readLine(); 173 if ( line.length() == 0 ) 174 break; 175 int colon = line.indexOf( ':' ); 176 if ( colon != -1 ) 177 { 178 String name = line.substring( 0, colon ).toLowerCase(); 179 String value = line.substring( colon + 1 ).trim(); 180 footerNames.addElement( name.toLowerCase() ); 181 footerValues.addElement( value ); 182 } 183 } 184 } 185 186 187 194 public String getFooter( String name ) 195 { 196 if ( ! isDone() ) 197 return null; 198 int i = footerNames.indexOf( name.toLowerCase() ); 199 if ( i == -1 ) 200 return null; 201 return (String ) footerValues.elementAt( i ); 202 } 203 204 205 208 public Enumeration getFooters() 209 { 210 if ( ! isDone() ) 211 return null; 212 return footerNames.elements(); 213 } 214 215 216 219 public int getContentLength() 220 { 221 if (! isDone()) { 222 return -1; 223 } 224 return contentLength; 225 } 226 227 228 234 public boolean isDone() 235 { 236 return footerNames != null; 237 } 238 239 240 248 protected String readLine() 249 throws IOException 250 { 251 final byte CR=13; 252 final byte LF=10; 253 254 ByteBuffer buff = new ByteBuffer(); 255 byte b=0; 256 257 int i=0; 258 do { 259 b = (byte)this.in.read(); 260 if (b != LF) { 261 buff.append(b); 262 } 263 i++; 264 } while ((b != LF)); 265 266 byte[] byteBuff = buff.getContent(); 269 270 if (byteBuff.length == 0) { 271 return ""; 272 } 273 274 if (byteBuff[byteBuff.length-1] != CR) { 275 return new String (byteBuff); 276 } else { 277 return new String (byteBuff,0,byteBuff.length-1); 278 } 279 } 280 281 } 282 | Popular Tags |