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 *
10
* http://www.apache.org/licenses/LICENSE-2.0 *
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
****************************************************************/
20
package org.apache.james.mime4j.storage;
22
import java.io.ByteArrayInputStream;
23
import java.io.IOException;
24
import java.io.InputStream;
26
import org.apache.james.mime4j.util.ByteArrayBuffer;
29
* A {@link StorageProvider} that stores the data entirely in memory.
34
* StorageProvider provider = new MemoryStorageProvider();
35
* DefaultStorageProvider.setInstance(provider);
38
public class MemoryStorageProvider extends AbstractStorageProvider {
41
* Creates a new <code>MemoryStorageProvider</code>.
43
public MemoryStorageProvider() {
46
public StorageOutputStream createStorageOutputStream() {
47
return new MemoryStorageOutputStream();
50
private static final class MemoryStorageOutputStream extends
52
ByteArrayBuffer bab = new ByteArrayBuffer(1024);
55
protected void write0(byte[] buffer, int offset, int length)
57
bab.append(buffer, offset, length);
61
protected Storage toStorage0() throws IOException {
62
return new MemoryStorage(bab.buffer(), bab.length());
66
static final class MemoryStorage implements Storage {
68
private final int count;
70
public MemoryStorage(byte[] data, int count) {
75
public InputStream getInputStream() throws IOException {
77
throw new IllegalStateException("storage has been deleted");
79
return new ByteArrayInputStream(data, 0, count);
82
public void delete() {