📄 boundarydelimitedstream.java
字号:
} else if (bwritten < len) { // need to get more data. byte[] dstbuf = readbuf; if (readbuf.length < len) { dstbuf = new byte[len]; } int movecnt = readBufEnd - readBufPos; // copy what was left over. System.arraycopy(readbuf, readBufPos, dstbuf, 0, movecnt); // Read in the new data. int readcnt = readFromStream(dstbuf, movecnt, dstbuf.length - movecnt); if (readcnt < 0) { readbuf = null; closed = true; finalClose(); throw new java.io.IOException( Messages.getMessage("eosBeforeMarker")); } readBufEnd = readcnt + movecnt; readbuf = dstbuf; readBufPos = 0; // start at the begining. // just move the boundary by what we moved if (BOUNDARY_NOT_FOUND != boundaryPos) { boundaryPos -= movecnt; } else { boundaryPos = boundaryPosition( readbuf, readBufPos, readBufEnd); // See if the boundary is now there. } } } // read till we get the amount or the stream is finished. while (!eos && (bwritten < len)); if (log.isDebugEnabled()) { if (bwritten > 0) { byte tb[] = new byte[bwritten]; System.arraycopy(b, off, tb, 0, bwritten); log.debug(Messages.getMessage("readBStream", new String[]{"" + bwritten, "" + streamNo, new String(tb)})); } } if (eos && theEnd) { readbuf = null; // dealloc even in Java. } return bwritten; } /** * Read from the boundary delimited stream. * @param b is the array to read into. Read as much as possible * into the size of this array. * @return the number of bytes read. -1 if endof stream. * * @throws java.io.IOException */ public int read(byte[] b) throws java.io.IOException { return read(b, 0, b.length); } /** * Read from the boundary delimited stream. * @return The byte read, or -1 if endof stream. * * @throws java.io.IOException */ public int read() throws java.io.IOException { byte[] b = new byte[1]; // quick and dirty. //for now int read = read(b); if (read < 0) { return -1; } else { return b[0]&0xff; } } /** * Closes the stream. * * @throws java.io.IOException */ public synchronized void close() throws java.io.IOException { if (closed) { return; } log.debug(Messages.getMessage("bStreamClosed", "" + streamNo)); closed = true; // mark it closed. if (!eos) { // We need get this off the stream. // Easy way to flush through the stream; byte[] readrest = new byte[1024 * 16]; int bread = 0; do { bread = read(readrest); } while (bread > -1); } } /** * mark the stream. * This is not supported. * * @param readlimit */ public void mark(int readlimit) { // do nothing } /** * reset the stream. * This is not supported. * * @throws java.io.IOException */ public void reset() throws java.io.IOException { throw new java.io.IOException( Messages.getMessage("attach.bounday.mns")); } /** * markSupported * return false; * * @return */ public boolean markSupported() { return false; } public int available() throws java.io.IOException { int bcopy = readBufEnd - readBufPos - boundaryBufLen; // never go past the boundary. bcopy = Math.min(bcopy, boundaryPos - readBufPos); return Math.max(0, bcopy); } /** * Read from the boundary delimited stream. * * @param searchbuf buffer to read from * @param start starting index * @param end ending index * @return The position of the boundary. Detects the end of the source stream. * @throws java.io.IOException if there was an error manipulating the * underlying stream */ protected int boundaryPosition(byte[] searchbuf, int start, int end) throws java.io.IOException { int foundAt = boundarySearch(searchbuf, start, end); // First find the boundary marker if (BOUNDARY_NOT_FOUND != foundAt) { // Something was found. if (foundAt + boundaryLen + 2 > end) { foundAt = BOUNDARY_NOT_FOUND; } else { // If the marker has a "--" at the end then this is the last boundary. if ((searchbuf[foundAt + boundaryLen] == '-') && (searchbuf[foundAt + boundaryLen + 1] == '-')) { finalClose(); } else if ((searchbuf[foundAt + boundaryLen] != 13) || (searchbuf[foundAt + boundaryLen + 1] != 10)) { // If there really was no crlf at then end then this is not a boundary. foundAt = BOUNDARY_NOT_FOUND; } } } return foundAt; } /* The below uses a standard textbook Boyer-Moore pattern search. */ private int[] skip = null; private int boundarySearch(final byte[] text, final int start, final int end) { // log.debug(">>>>" + start + "," + end); int i = 0, j = 0, k = 0; if (boundaryLen > (end - start)) { return BOUNDARY_NOT_FOUND; } if (null == skip) { skip = new int[256]; java.util.Arrays.fill(skip, boundaryLen); for (k = 0; k < boundaryLen - 1; k++) { skip[boundary[k]] = boundaryLen - k - 1; } } for (k = start + boundaryLen - 1; k < end; k += skip[text[k] & (0xff)]) { // log.debug(">>>>" + k); // printarry(text, k-boundaryLen+1, end); try { for (j = boundaryLen - 1, i = k; (j >= 0) && (text[i] == boundary[j]); j--) { i--; } } catch (ArrayIndexOutOfBoundsException e) { StringBuffer sb = new StringBuffer(); sb.append( ">>>" + e); // rr temporary till a boundary issue is resolved. sb.append("start=" + start); sb.append("k=" + k); sb.append("text.length=" + text.length); sb.append("i=" + i); sb.append("boundary.length=" + boundary.length); sb.append("j=" + j); sb.append("end=" + end); log.warn(Messages.getMessage("exception01",sb.toString())); throw e; } if (j == (-1)) { return i + 1; } } // log.debug(">>>> not found" ); return BOUNDARY_NOT_FOUND; } /** * Close the underlying stream and remove all references to it. * * @throws java.io.IOException if the stream could not be closed */ protected void finalClose() throws java.io.IOException { if(theEnd) return; theEnd= true; is.close(); is= null; } /** * Method printarry * * @param b * @param start * @param end */ public static void printarry(byte[] b, int start, int end) { if (log.isDebugEnabled()) { byte tb[] = new byte[end - start]; System.arraycopy(b, start, tb, 0, end - start); log.debug("\"" + new String(tb) + "\""); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -