1 16 package org.apache.cocoon.util; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 27 28 public class PostInputStream extends InputStream { 29 30 33 public static final String CLASS = PostInputStream.class.getName(); 34 35 36 private InputStream m_inputStream = null; 37 38 39 private int m_contentLen = 0; 40 41 42 protected int m_bytesRead = 0; 43 44 45 48 public PostInputStream() { 49 super(); 50 } 51 60 61 public PostInputStream(final InputStream input, final int len) throws IllegalArgumentException { 62 super(); 63 init(input, len ); 64 } 65 73 protected void init(final InputStream input, final int len) throws IllegalArgumentException { 74 if (len <= 0) { 75 throw new IllegalArgumentException ("contentLen <= 0 "); 76 } 77 this.m_inputStream = input; 78 this.m_contentLen = len; 79 } 80 81 89 public synchronized void setInputStream(final InputStream input, final int len) throws IOException { 90 if (m_inputStream != null) { 91 close(); 92 } 93 init(input, len); 94 } 95 96 101 public synchronized InputStream getInputStream() { 102 return m_inputStream; 103 } 104 105 110 111 public int getContentLen() { 112 return( m_contentLen ); 113 } 114 115 122 public synchronized int read() throws IOException { 123 124 checkOpen(); 125 if (m_bytesRead == m_contentLen) { 126 return -1; 127 } 128 int byt = m_inputStream.read(); 129 if (byt != -1) { 130 m_bytesRead++; 131 } 132 return byt; 133 } 134 135 157 158 public synchronized int read(byte[] buffer, int offset, int len) throws IOException { 159 checkOpen(); 160 if (m_bytesRead == m_contentLen) { 161 return -1; 162 } 163 int available = Math.min( available(), len ); 164 int num = m_inputStream.read( buffer, offset, available ); 165 if (num > 0) { 166 m_bytesRead += num; 167 } 168 return num; 169 } 170 171 public synchronized int read(byte[] buffer) throws IOException { 172 173 return read( buffer, 0, buffer.length); 174 } 175 176 177 182 protected void checkOpen() throws IOException { 183 if (m_inputStream == null) { 184 throw new IOException ("InputStream closed"); 185 } 186 } 187 188 197 public synchronized long skip(long n) throws IOException { 198 checkOpen(); 199 if ( m_bytesRead == m_contentLen ) 200 { 201 return ( 0 ); 202 } 203 else 204 { 205 return ( m_inputStream.skip( n ) ); 206 } 207 208 } 209 210 217 public synchronized int available() throws IOException { 218 checkOpen(); 219 int avail = m_inputStream.available(); 220 return (avail == 0 ? (m_contentLen - m_bytesRead) : avail); 221 } 222 223 234 public boolean markSupported() { 235 return false; 236 } 237 238 243 public synchronized void close() throws IOException { 244 if (m_inputStream == null) { 245 return; 246 } 247 m_inputStream.close(); 248 m_inputStream = null; 249 m_contentLen = 0; 250 m_bytesRead = 0; 251 } 252 253 258 public String toString() { 259 return new StringBuffer (getClass().getName()) 260 .append("[inputStream=").append(m_inputStream) 261 .append(", contentLen=").append(m_contentLen) 262 .append("bytesRead=").append(m_bytesRead) 263 .append("]").toString(); 264 } 265 } 266 | Popular Tags |