1 28 29 package com.opencms.core; 30 31 import java.io.IOException ; 32 33 import javax.servlet.ServletInputStream ; 34 35 47 class CmsMultipartInputStreamHandler { 48 49 private ServletInputStream m_in; 50 private int m_totalExpected; 51 private int m_totalRead; 52 private int m_newLine; 53 54 60 public CmsMultipartInputStreamHandler(ServletInputStream in, int totalExpected) { 61 m_in = in; 62 m_totalExpected = totalExpected; 63 } 64 65 72 public int read() throws IOException { 73 if (m_totalRead >= m_totalExpected) { 74 return -1; 75 } else { 76 int result = m_in.read(); 77 if (result > -1) { 78 m_totalRead++; 79 } 80 return result; 81 } 82 } 83 84 90 public String readLine() throws IOException { 91 byte[] buf = new byte[64 * 1024]; 92 StringBuffer sbuf = new StringBuffer (); 93 int result; 94 95 do { 97 98 result = this.readLine(buf, 0, buf.length); 100 if (result != -1) { 101 sbuf.append(new String (buf, 0, result, "ISO-8859-1")); 102 } 103 } while(result == buf.length); 104 if (sbuf.length() == 0) { 105 return null; 107 } 108 if (m_newLine == 0) { 110 m_newLine = (result > 1 && (buf[result - 2] == '\r' || buf[result - 2] == '\n')) ? 2 : 1; 111 } 112 buf = null; 113 sbuf.setLength(sbuf.length() - m_newLine); 114 return sbuf.toString(); 115 } 116 117 127 public int readLine(byte[] b, int off, int len) throws IOException { 128 if (m_totalRead >= m_totalExpected) { 129 return -1; 130 } else { 131 int result = m_in.readLine(b, off, len); 132 if (result > 0) { 133 m_totalRead += result; 134 } 135 return result; 136 } 137 } 138 } 139 | Popular Tags |