1 /************************************************************************
2 * Copyright (c) 2000-2006 The Apache Software Foundation. *
3 * All rights reserved. *
4 * ------------------------------------------------------------------- *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you *
6 * may not use this file except in compliance with the License. You *
7 * may obtain a copy of the License at: *
8 * *
9 * http://www.apache.org/licenses/LICENSE-2.0 *
10 * *
11 * Unless required by applicable law or agreed to in writing, software *
12 * distributed under the License is distributed on an "AS IS" BASIS, *
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14 * implied. See the License for the specific language governing *
15 * permissions and limitations under the License. *
16 ***********************************************************************/
17
18 package org.apache.james.core;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22
23 /***
24 * This defines a reusable datasource that can supply an input stream with
25 * MimeMessage data. This allows a MimeMessageWrapper or other classes to
26 * grab the underlying data.
27 *
28 * @see MimeMessageWrapper
29 */
30 public abstract class MimeMessageSource {
31 /***
32 * Returns a unique String ID that represents the location from where
33 * this file is loaded. This will be used to identify where the data
34 * is, primarily to avoid situations where this data would get overwritten.
35 *
36 * @return the String ID
37 */
38 public abstract String getSourceId();
39
40 /***
41 * Get an input stream to retrieve the data stored in the datasource
42 *
43 * @return a <code>InputStream</code> containing the data
44 *
45 * @throws IOException if an error occurs while generating the
46 * InputStream
47 */
48 public abstract InputStream getInputStream() throws IOException;
49
50 /***
51 * Return the size of all the data.
52 * Default implementation... others can override to do this much faster
53 *
54 * @return the size of the data represented by this source
55 * @throws IOException if an error is encountered while computing the message size
56 */
57 public long getMessageSize() throws IOException {
58 int size = 0;
59 InputStream in = null;
60 try {
61 in = getInputStream();
62 int read = 0;
63 byte[] data = new byte[1024];
64 while ((read = in.read(data)) > 0) {
65 size += read;
66 }
67 } finally {
68 try {
69 if (in != null) {
70 in.close();
71 }
72 } catch (IOException ioe) {
73
74
75 }
76 }
77 return size;
78 }
79
80 }