src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/ChunkInputStream.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
equal deleted inserted replaced
-1:000000000000 0:373ffda63c9a
1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.xml.internal.org.jvnet.mimepull;
27
28 import java.io.InputStream;
29 import java.io.IOException;
30
31 /**
32 * Constructs a InputStream from a linked list of {@link Chunk}s.
33 *
34 * @author Kohsuke Kawaguchi
35 * @author Jitendra Kotamraju
36 */
37 final class ChunkInputStream extends InputStream {
38 Chunk current;
39 int offset;
40 int len;
41 final MIMEMessage msg;
42 final MIMEPart part;
43 byte[] buf;
44
45 public ChunkInputStream(MIMEMessage msg, MIMEPart part, Chunk startPos) {
46 this.current = startPos;
47 len = current.data.size();
48 buf = current.data.read();
49 this.msg = msg;
50 this.part = part;
51 }
52
53 @Override
54 public int read(byte b[], int off, int sz) throws IOException {
55 if (!fetch()) {
56 return -1;
57 }
58
59 sz = Math.min(sz, len-offset);
60 System.arraycopy(buf,offset,b,off,sz);
61 return sz;
62 }
63
64 public int read() throws IOException {
65 if (!fetch()) {
66 return -1;
67 }
68 return (buf[offset++] & 0xff);
69 }
70
71 /**
72 * Gets to the next chunk if we are done with the current one.
73 * @return
74 */
75 private boolean fetch() {
76 if (current == null) {
77 throw new IllegalStateException("Stream already closed");
78 }
79 while(offset==len) {
80 while(!part.parsed && current.next == null) {
81 msg.makeProgress();
82 }
83 current = current.next;
84
85 if (current == null) {
86 return false;
87 }
88 this.offset = 0;
89 this.buf = current.data.read();
90 this.len = current.data.size();
91 }
92 return true;
93 }
94
95 @Override
96 public void close() throws IOException {
97 super.close();
98 current = null;
99 }
100 }

mercurial