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