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.mailrepository.filepair;
21
22 import org.apache.avalon.cornerstone.services.store.StreamRepository;
23
24 import java.io.BufferedOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28
29 /***
30 * Implementation of a StreamRepository to a File.
31 * TODO: -retieve(String key) should return a FilterInputStream to allow
32 * mark and reset methods. (working not like BufferedInputStream!!!)
33 *
34 */
35 public class File_Persistent_Stream_Repository
36 extends AbstractFileRepository
37 implements StreamRepository
38 {
39 protected String getExtensionDecorator()
40 {
41 return ".FileStreamStore";
42 }
43
44 /***
45 * Get the object associated to the given unique key.
46 */
47 public synchronized InputStream get( final String key )
48 {
49 try
50 {
51 return getInputStream( key );
52 }
53 catch( final IOException ioe )
54 {
55 final String message = "Exception caught while retrieving a stream ";
56 getLogger().warn( message, ioe );
57 throw new RuntimeException( message + ": " + ioe );
58 }
59 }
60
61 /***
62 * Store the given object and associates it to the given key
63 */
64 public synchronized OutputStream put( final String key )
65 {
66 try
67 {
68 final OutputStream outputStream = getOutputStream( key );
69 return new BufferedOutputStream( outputStream );
70 }
71 catch( final IOException ioe )
72 {
73 final String message = "Exception caught while storing a stream ";
74 getLogger().warn( message, ioe );
75 throw new RuntimeException( message + ": " + ioe );
76 }
77 }
78
79 public long getSize(final String key) {
80 try {
81 return getFile(key).length();
82 }
83 catch(IOException e) {
84 return 0;
85 }
86 }
87 }