⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multipartstream.java

📁 apache commons-fileupload-1.2.jar
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * Searches for the <code>boundary</code> in the <code>buffer</code>     * region delimited by <code>head</code> and <code>tail</code>.     *     * @return The position of the boundary found, counting from the     *         beginning of the <code>buffer</code>, or <code>-1</code> if     *         not found.     */    protected int findSeparator() {        int first;        int match = 0;        int maxpos = tail - boundaryLength;        for (first = head;        (first <= maxpos) && (match != boundaryLength);        first++) {            first = findByte(boundary[0], first);            if (first == -1 || (first > maxpos)) {                return -1;            }            for (match = 1; match < boundaryLength; match++) {                if (buffer[first + match] != boundary[match]) {                    break;                }            }        }        if (match == boundaryLength) {            return first - 1;        }        return -1;    }    /**     * Thrown to indicate that the input stream fails to follow the     * required syntax.     */    public static class MalformedStreamException    extends IOException {        /**         * Constructs a <code>MalformedStreamException</code> with no         * detail message.         */        public MalformedStreamException() {            super();        }        /**         * Constructs an <code>MalformedStreamException</code> with         * the specified detail message.         *         * @param message The detail message.         */        public MalformedStreamException(String message) {            super(message);        }    }    /**     * Thrown upon attempt of setting an invalid boundary token.     */    public static class IllegalBoundaryException            extends IOException {        /**         * Constructs an <code>IllegalBoundaryException</code> with no         * detail message.         */        public IllegalBoundaryException() {            super();        }        /**         * Constructs an <code>IllegalBoundaryException</code> with         * the specified detail message.         *         * @param message The detail message.         */        public IllegalBoundaryException(String message) {            super(message);        }    }    /**     * An {@link InputStream} for reading an items contents.     */    public class ItemInputStream extends InputStream implements Closeable {        /** The number of bytes, which have been read so far.         */        private long total;        /** The number of bytes, which must be hold, because         * they might be a part of the boundary.         */        private int pad;        /** The current offset in the buffer.         */        private int pos;        /** Whether the stream is already closed.         */        private boolean closed;        /**         * Creates a new instance.         */        ItemInputStream() {            findSeparator();        }        /**         * Called for finding the separator.         */        private void findSeparator() {            pos = MultipartStream.this.findSeparator();            if (pos == -1) {                if (tail - head > keepRegion) {                    pad = keepRegion;                } else {                    pad = tail - head;                }            }        }        /**         * Returns the number of bytes, which have been read         * by the stream.         * @return Number of bytes, which have been read so far.         */        public long getBytesRead() {            return total;        }        /**         * Returns the number of bytes, which are currently         * available, without blocking.         * @throws IOException An I/O error occurs.         * @return Number of bytes in the buffer.         */        public int available() throws IOException {            if (pos == -1) {                return tail - head - pad;            }            return pos - head;        }        /** Offset when converting negative bytes to integers.         */        private static final int BYTE_POSITIVE_OFFSET = 256;        /**         * Returns the next byte in the stream.         * @return The next byte in the stream, as a non-negative         *   integer, or -1 for EOF.         * @throws IOException An I/O error occurred.         */        public int read() throws IOException {            if (closed) {                throw new FileItemStream.ItemSkippedException();            }            if (available() == 0) {                if (makeAvailable() == 0) {                    return -1;                }            }            ++total;            int b = buffer[head++];            if (b >= 0) {                return b;            }            return b + BYTE_POSITIVE_OFFSET;        }        /**         * Reads bytes into the given buffer.         * @param b The destination buffer, where to write to.         * @param off Offset of the first byte in the buffer.         * @param len Maximum number of bytes to read.         * @return Number of bytes, which have been actually read,         *   or -1 for EOF.         * @throws IOException An I/O error occurred.         */        public int read(byte[] b, int off, int len) throws IOException {            if (closed) {                throw new FileItemStream.ItemSkippedException();            }            if (len == 0) {                return 0;            }            int res = available();            if (res == 0) {                res = makeAvailable();                if (res == 0) {                    return -1;                }            }            res = Math.min(res, len);            System.arraycopy(buffer, head, b, off, res);            head += res;            total += res;            return res;        }        /**         * Closes the input stream.         * @throws IOException An I/O error occurred.         */        public void close() throws IOException {            if (closed) {                return;            }            for (;;) {                int av = available();                if (av == 0) {                    av = makeAvailable();                    if (av == 0) {                        break;                    }                }                skip(av);            }            closed = true;        }        /**         * Skips the given number of bytes.         * @param bytes Number of bytes to skip.         * @return The number of bytes, which have actually been         *   skipped.         * @throws IOException An I/O error occurred.         */        public long skip(long bytes) throws IOException {            if (closed) {                throw new FileItemStream.ItemSkippedException();            }            int av = available();            if (av == 0) {                av = makeAvailable();                if (av == 0) {                    return 0;                }            }            long res = Math.min(av, bytes);            head += res;            return res;        }        /**         * Attempts to read more data.         * @return Number of available bytes         * @throws IOException An I/O error occurred.         */        private int makeAvailable() throws IOException {            if (pos != -1) {                return 0;            }            // Move the data to the beginning of the buffer.            total += tail - head - pad;            System.arraycopy(buffer, tail - pad, buffer, 0, pad);            // Refill buffer with new data.            head = 0;            int bytesRead = input.read(buffer, pad, bufSize - pad);            if (bytesRead == -1) {                // The last pad amount is left in the buffer.                // Boundary can't be in there so signal an error                // condition.                throw new MalformedStreamException(                "Stream ended unexpectedly");            }            notifier.noteBytesRead(bytesRead);            tail = pad + bytesRead;            findSeparator();            return available();        }        /**         * Returns, whether the stream is closed.         * @return True, if the stream is closed, otherwise false.         */        public boolean isClosed() {            return closed;        }    }    // ------------------------------------------------------ Debugging methods    // These are the methods that were used to debug this stuff.    /*    // Dump data.    protected void dump()    {        System.out.println("01234567890");        byte[] temp = new byte[buffer.length];        for(int i=0; i<buffer.length; i++)        {            if (buffer[i] == 0x0D || buffer[i] == 0x0A)            {                temp[i] = 0x21;            }            else            {                temp[i] = buffer[i];            }        }        System.out.println(new String(temp));        int i;        for (i=0; i<head; i++)            System.out.print(" ");        System.out.println("h");        for (i=0; i<tail; i++)            System.out.print(" ");        System.out.println("t");        System.out.flush();    }    // Main routine, for testing purposes only.    //    // @param args A String[] with the command line arguments.    // @throws Exception, a generic exception.    public static void main( String[] args )        throws Exception    {        File boundaryFile = new File("boundary.dat");        int boundarySize = (int)boundaryFile.length();        byte[] boundary = new byte[boundarySize];        FileInputStream input = new FileInputStream(boundaryFile);        input.read(boundary,0,boundarySize);        input = new FileInputStream("multipart.dat");        MultipartStream chunks = new MultipartStream(input, boundary);        int i = 0;        String header;        OutputStream output;        boolean nextChunk = chunks.skipPreamble();        while (nextChunk)        {            header = chunks.readHeaders();            System.out.println("!"+header+"!");            System.out.println("wrote part"+i+".dat");            output = new FileOutputStream("part"+(i++)+".dat");            chunks.readBodyData(output);            nextChunk = chunks.readBoundary();        }    }     */}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -