package divya.upstream.common; import java.io.*; import java.net.*; import java.util.*; /** * Provides methods to send headers and data using MIME protocol. */ public class ProtocolInputStream extends InputStream { byte[] inBuffer = new byte[16 * 1024]; byte[] outBuffer = new byte[16 * 1024]; int outCount = 0; int outPtr = 0; protected Properties header=new Properties(); protected DataInputStream dataIn = null; protected boolean okToRead = false; protected boolean okToWrite = false; protected boolean separatorSent = false; byte buffer[] = new byte[16 * 1024]; int packetSize = 16 * 1024; InputStream in = null; public ProtocolInputStream(InputStream input) { in = input; dataIn = new DataInputStream(input); } public int read() throws IOException { if (!okToRead) return ProtocolHeaders.V_EOF; int b = ProtocolHeaders.V_EOF; if (outBuffer == null) return ProtocolHeaders.V_EOF; if (outCount == 0) underflow(); if (outCount > 0) { b = outBuffer[outPtr] & 0xff; outPtr++; outCount--; } else { okToRead = false; return ProtocolHeaders.V_EOF; } return b; } public int read(byte [] b) throws IOException { return read (b, 0, b.length); } public int read(byte b[], int offset, int len) throws IOException { if (!okToRead) return ProtocolHeaders.V_EOF; int blen = len; int fillLen = 0; if (outCount < blen) { int tempCount = outCount; for (int i = 0; i < tempCount; i++) { b[offset] = outBuffer[outPtr]; fillLen++; offset++; outPtr++; outCount--; } underflow(); if (outCount == 0) { if (fillLen == 0) { okToRead = false; return ProtocolHeaders.V_EOF; } else return fillLen; } } if (outCount > 0) { for (int i = fillLen; i < blen; i++) { if (outCount > 0) { b[offset] = outBuffer[outPtr]; offset++; fillLen++; outPtr++; outCount--; } else { underflow(); if (outCount == 0) { if (fillLen == 0) { okToRead = false; return ProtocolHeaders.V_EOF; } else return fillLen; } } } } else { okToRead = false; return ProtocolHeaders.V_EOF; } return fillLen; } /** * Reads InputStream one line at a time. Throws all header (Directive: * value) pairs in internal Properties object till a blank line is * encountered, after that the remainder is the actual data. */ public void moveToNextPart() throws java.io.IOException { StringTokenizer st; String line, directive, value; // Initialize private data members header.clear(); okToRead=true; int index; boolean firstCharFound=false; while ((line = dataIn.readLine()) != null) { if (line == null || line.trim().length() < 1) { if (firstCharFound) break; else continue; } firstCharFound=true; st = new StringTokenizer(line, ":"); if (st.countTokens() < 2) continue; header.put(directive=st.nextToken().trim(), value=st.nextToken().trim()); } } /** * Return a value from internal Properties object */ public String getHeader(String key) { if (header != null) return header.getProperty(key); return null; } /** * Return header Properties object */ public Properties getAllHeaders() { return header; } public boolean markSupported() { return false; } public void close() throws IOException { inBuffer = null; outBuffer = null; in.close(); okToRead = false; } private void underflow() throws IOException { int c = -1; int bytesToRead = dataIn.readInt(); if (bytesToRead == ProtocolHeaders.V_EOF) { okToRead = false; outPtr = 0; return; } if (bytesToRead == ProtocolHeaders.V_INTERRUPT) { okToRead = false; outPtr = 0; throw new InterruptedIOException("Read interrupted"); } int returnCount = 0; int currentCount = -1; int incoming = bytesToRead; try { while (returnCount < incoming) { currentCount = dataIn.read(outBuffer, returnCount, bytesToRead); if (currentCount == ProtocolHeaders.V_EOF) throw new IOException("Premature end of file in incoming data"); returnCount += currentCount; outCount += currentCount; bytesToRead = bytesToRead - currentCount; } } catch (IOException e) { throw new SocketException(e.getMessage()); } outPtr = 0; } public static InputStream encode(InputStream decodedStream) { return null; } public void terminate() throws Exception { if (!okToRead) return; byte [] tempBuffer = new byte[16*1024]; int c = -1; while ((c = this.read(tempBuffer, 0, tempBuffer.length)) >= 0) ; okToRead = false; } }