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.mime4j.storage;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  
26  import org.apache.james.mime4j.util.ByteArrayBuffer;
27  
28  /**
29   * A {@link StorageProvider} that stores the data entirely in memory.
30   * <p>
31   * Example usage:
32   *
33   * <pre>
34   * StorageProvider provider = new MemoryStorageProvider();
35   * DefaultStorageProvider.setInstance(provider);
36   * </pre>
37   */
38  public class MemoryStorageProvider extends AbstractStorageProvider {
39  
40      /**
41       * Creates a new <code>MemoryStorageProvider</code>.
42       */
43      public MemoryStorageProvider() {
44      }
45  
46      public StorageOutputStream createStorageOutputStream() {
47          return new MemoryStorageOutputStream();
48      }
49  
50      private static final class MemoryStorageOutputStream extends
51              StorageOutputStream {
52          ByteArrayBuffer bab = new ByteArrayBuffer(1024);
53  
54          @Override
55          protected void write0(byte[] buffer, int offset, int length)
56                  throws IOException {
57              bab.append(buffer, offset, length);
58          }
59  
60          @Override
61          protected Storage toStorage0() throws IOException {
62              return new MemoryStorage(bab.buffer(), bab.length());
63          }
64      }
65  
66      static final class MemoryStorage implements Storage {
67          private byte[] data;
68          private final int count;
69  
70          public MemoryStorage(byte[] data, int count) {
71              this.data = data;
72              this.count = count;
73          }
74  
75          public InputStream getInputStream() throws IOException {
76              if (data == null)
77                  throw new IllegalStateException("storage has been deleted");
78  
79              return new ByteArrayInputStream(data, 0, count);
80          }
81  
82          public void delete() {
83              data = null;
84          }
85      }
86  
87  }