001/****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one   *
003 * or more contributor license agreements.  See the NOTICE file *
004 * distributed with this work for additional information        *
005 * regarding copyright ownership.  The ASF licenses this file   *
006 * to you under the Apache License, Version 2.0 (the            *
007 * "License"); you may not use this file except in compliance   *
008 * with the License.  You may obtain a copy of the License at   *
009 *                                                              *
010 *   http://www.apache.org/licenses/LICENSE-2.0                 *
011 *                                                              *
012 * Unless required by applicable law or agreed to in writing,   *
013 * software distributed under the License is distributed on an  *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015 * KIND, either express or implied.  See the License for the    *
016 * specific language governing permissions and limitations      *
017 * under the License.                                           *
018 ****************************************************************/
019
020
021package org.apache.james.repository.file;
022
023import org.apache.james.repository.api.StreamRepository;
024
025import java.io.BufferedOutputStream;
026import java.io.IOException;
027import java.io.InputStream;
028import java.io.OutputStream;
029
030/**
031 * Implementation of a StreamRepository to a File.<br>
032 * TODO: -retieve(String key) should return a FilterInputStream to allow
033 * mark and reset methods. (working not like BufferedInputStream!!!)
034 */
035public class FilePersistentStreamRepository extends AbstractFileRepository implements StreamRepository {
036
037    @Override
038    protected String getExtensionDecorator() {
039        return ".FileStreamStore";
040    }
041
042
043    @Override
044    public synchronized InputStream get(String key) {
045        try {
046            return getInputStream(key);
047        } catch (IOException ioe) {
048            final String message = "Exception caught while retrieving a stream ";
049            getLogger().warn(message, ioe);
050            throw new RuntimeException(message + ": " + ioe);
051        }
052    }
053
054
055    @Override
056    public synchronized OutputStream put(String key) {
057        try {
058            final OutputStream outputStream = getOutputStream(key);
059            return new BufferedOutputStream(outputStream);
060        } catch (IOException ioe) {
061            final String message = "Exception caught while storing a stream ";
062            getLogger().warn(message, ioe);
063            throw new RuntimeException(message + ": " + ioe);
064        }
065    }
066
067    /**
068     * Return the size of the file which belongs to the given key
069     *
070     * @param key the key to get the size for
071     * @return size the Size which belongs to the givens keys file
072     */
073    public long getSize(String key) {
074        try {
075            return getFile(key).length();
076        } catch (IOException e) {
077            return 0;
078        }
079    }
080}