View Javadoc

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  package org.apache.james.core;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  
25  /***
26   * This defines a reusable datasource that can supply an input stream with
27   * MimeMessage data.  This allows a MimeMessageWrapper or other classes to
28   * grab the underlying data.
29   *
30   * @see MimeMessageWrapper
31   */
32  public abstract class MimeMessageSource {
33      /***
34       * Returns a unique String ID that represents the location from where 
35       * this file is loaded.  This will be used to identify where the data 
36       * is, primarily to avoid situations where this data would get overwritten.
37       *
38       * @return the String ID
39       */
40      public abstract String getSourceId();
41  
42      /***
43       * Get an input stream to retrieve the data stored in the datasource
44       *
45       * @return a <code>InputStream</code> containing the data
46       *
47       * @throws IOException if an error occurs while generating the
48       *                     InputStream
49       */
50      public abstract InputStream getInputStream() throws IOException;
51  
52      /***
53       * Return the size of all the data.
54       * Default implementation... others can override to do this much faster
55       *
56       * @return the size of the data represented by this source
57       * @throws IOException if an error is encountered while computing the message size
58       */
59      public long getMessageSize() throws IOException {
60          int size = 0;
61          InputStream in = null;
62          try {
63              in = getInputStream();
64              int read = 0;
65              byte[] data = new byte[1024];
66              while ((read = in.read(data)) > 0) {
67                  size += read;
68              }
69          } finally {
70              try {
71                  if (in != null) {
72                      in.close();
73                  }
74              } catch (IOException ioe) {
75                  // Exception ignored because logging is
76                  // unavailable
77              }
78          }
79          return size;
80      }
81  
82  }