1 /* |
1 /* |
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
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. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
31 /** |
31 /** |
32 * Represents an attachment part in a MIME message. MIME message parsing is done |
32 * Represents an attachment part in a MIME message. MIME message parsing is done |
33 * lazily using a pull parser, so the part may not have all the data. {@link #read} |
33 * lazily using a pull parser, so the part may not have all the data. {@link #read} |
34 * and {@link #readOnce} may trigger the actual parsing the message. In fact, |
34 * and {@link #readOnce} may trigger the actual parsing the message. In fact, |
35 * parsing of an attachment part may be triggered by calling {@link #read} methods |
35 * parsing of an attachment part may be triggered by calling {@link #read} methods |
36 * on some other attachemnt parts. All this happens behind the scenes so the |
36 * on some other attachment parts. All this happens behind the scenes so the |
37 * application developer need not worry about these details. |
37 * application developer need not worry about these details. |
38 * |
38 * |
39 * @author Jitendra Kotamraju |
39 * @author Jitendra Kotamraju |
40 */ |
40 */ |
41 final class DataHead { |
41 final class DataHead { |
82 if (dataFile != null) { |
82 if (dataFile != null) { |
83 dataFile.renameTo(f); |
83 dataFile.renameTo(f); |
84 } else { |
84 } else { |
85 try { |
85 try { |
86 OutputStream os = new FileOutputStream(f); |
86 OutputStream os = new FileOutputStream(f); |
87 InputStream in = readOnce(); |
87 try { |
88 byte[] buf = new byte[8192]; |
88 InputStream in = readOnce(); |
89 int len; |
89 byte[] buf = new byte[8192]; |
90 while((len=in.read(buf)) != -1) { |
90 int len; |
91 os.write(buf, 0, len); |
91 while((len=in.read(buf)) != -1) { |
|
92 os.write(buf, 0, len); |
|
93 } |
|
94 } finally { |
|
95 if (os != null) { |
|
96 os.close(); |
|
97 } |
92 } |
98 } |
93 os.close(); |
|
94 } catch(IOException ioe) { |
99 } catch(IOException ioe) { |
95 throw new MIMEParsingException(ioe); |
100 throw new MIMEParsingException(ioe); |
96 } |
101 } |
97 } |
102 } |
98 } |
103 } |
139 * <p> |
144 * <p> |
140 * Calling this method also marks the stream as 'consumed' |
145 * Calling this method also marks the stream as 'consumed' |
141 * |
146 * |
142 * @return true if readOnce() is not called before |
147 * @return true if readOnce() is not called before |
143 */ |
148 */ |
|
149 @SuppressWarnings("ThrowableInitCause") |
144 private boolean unconsumed() { |
150 private boolean unconsumed() { |
145 if (consumedAt != null) { |
151 if (consumedAt != null) { |
146 AssertionError error = new AssertionError("readOnce() is already called before. See the nested exception from where it's called."); |
152 AssertionError error = new AssertionError("readOnce() is already called before. See the nested exception from where it's called."); |
147 error.initCause(consumedAt); |
153 error.initCause(consumedAt); |
148 throw error; |
154 throw error; |
193 buf = current.data.read(); |
199 buf = current.data.read(); |
194 } |
200 } |
195 |
201 |
196 @Override |
202 @Override |
197 public int read(byte b[], int off, int sz) throws IOException { |
203 public int read(byte b[], int off, int sz) throws IOException { |
198 if(!fetch()) return -1; |
204 if (!fetch()) { |
|
205 return -1; |
|
206 } |
199 |
207 |
200 sz = Math.min(sz, len-offset); |
208 sz = Math.min(sz, len-offset); |
201 System.arraycopy(buf,offset,b,off,sz); |
209 System.arraycopy(buf,offset,b,off,sz); |
202 offset += sz; |
210 offset += sz; |
203 return sz; |
211 return sz; |
204 } |
212 } |
205 |
213 |
|
214 @Override |
206 public int read() throws IOException { |
215 public int read() throws IOException { |
207 if (!fetch()) { |
216 if (!fetch()) { |
208 return -1; |
217 return -1; |
209 } |
218 } |
210 return (buf[offset++] & 0xff); |
219 return (buf[offset++] & 0xff); |