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 public class ContentLengthStream extends StreamImpl { 40 private ReadStream _next; 42 43 private int _length; 45 46 void init(ReadStream next, int length) 47 { 48 _next = next; 49 _length = length; 50 } 51 52 public boolean canRead() 53 { 54 return true; 55 } 56 57 66 public int read(byte []buffer, int offset, int length) throws IOException 67 { 68 if (_length < length) 69 length = _length; 70 71 if (length <= 0) 72 return -1; 73 74 int len = _next.read(buffer, offset, length); 75 76 if (len > 0) { 77 _length -= len; 78 } 79 else 80 _length = -1; 81 82 return len; 83 } 84 85 public int getAvailable() 86 throws IOException 87 { 88 int available = _next.available(); 89 90 if (_length <= 0) 91 return 0; 92 else if (_length < available) 93 return _length; 94 else 95 return available; 96 } 97 } 98 | Popular Tags |