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