1 28 29 package com.caucho.server.http; 30 31 import com.caucho.vfs.ReadStream; 32 import com.caucho.vfs.StreamImpl; 33 34 import java.io.IOException ; 35 36 39 class ChunkedInputStream extends StreamImpl { 40 private ReadStream _next; 41 private int _available; 42 43 void init(ReadStream next) 44 { 45 _next = next; 46 _available = 0; 47 } 48 49 public boolean canRead() 50 { 51 return true; 52 } 53 54 public int getAvailable() 55 { 56 return _available; 57 } 58 59 62 public int read(byte []buf, int offset, int len) throws IOException 63 { 64 if (_available > 0) { 66 if (_available < len) 67 len = _available; 68 69 len = _next.read(buf, offset, len); 70 71 if (len > 0) 72 _available -= len; 73 } 74 else if (_available == 0) { 76 _available = readChunkLength(); 77 78 if (_available > 0) { 80 if (_available < len) 81 len = _available; 82 83 len = _next.read(buf, offset, len); 84 85 if (len > 0) 86 _available -= len; 87 } 88 else { 90 _available = -1; 91 len = -1; 92 } 93 } 94 else 95 len = -1; 96 97 return len; 98 } 99 100 103 private int readChunkLength() 104 throws IOException 105 { 106 int length = 0; 107 int ch; 108 109 for (ch = _next.read(); 111 ch == '\r' || ch == ' ' || ch == '\n'; 112 ch = _next.read()) { 113 } 114 115 for (; ch > 0 && ch != '\r' && ch != '\n'; ch = _next.read()) { 118 if (ch >= '0' && ch <= '9') 119 length = 16 * length + ch - '0'; 120 else if (ch >= 'a' && ch <= 'f') 121 length = 16 * length + ch - 'a' + 10; 122 else if (ch >= 'A' && ch <= 'F') 123 length = 16 * length + ch - 'A' + 10; 124 else if (ch == ' ' || ch == '\t') { 125 } 128 else 129 throw new IOException ("HTTP/1.1 protocol error: bad chunk"); 130 } 131 132 if (ch == '\r') 133 ch = _next.read(); 134 135 return length; 136 } 137 } 138 | Popular Tags |