01: /****************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one *
03: * or more contributor license agreements. See the NOTICE file *
04: * distributed with this work for additional information *
05: * regarding copyright ownership. The ASF licenses this file *
06: * to you under the Apache License, Version 2.0 (the *
07: * "License"); you may not use this file except in compliance *
08: * with the License. You may obtain a copy of the License at *
09: * *
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: ****************************************************************/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: // Exception ignored because logging is
74: // unavailable
75: }
76: }
77: return size;
78: }
79:
80: }
|