001    // Copyright 2011 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    // http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.internal.services.assets;
016    
017    import org.apache.tapestry5.services.assets.CompressionStatus;
018    import org.apache.tapestry5.services.assets.StreamableResource;
019    
020    import java.io.IOException;
021    import java.io.InputStream;
022    import java.io.OutputStream;
023    
024    public class StreamableResourceImpl implements StreamableResource
025    {
026        private final String description;
027    
028        private final String contentType;
029    
030        private final CompressionStatus compression;
031    
032        private final long lastModified;
033    
034        private final BytestreamCache bytestreamCache;
035    
036        public StreamableResourceImpl(String description, String contentType, CompressionStatus compression, long lastModified,
037                                      BytestreamCache bytestreamCache)
038        {
039            this.description = description;
040            this.contentType = contentType;
041            this.compression = compression;
042            this.lastModified = lastModified;
043            this.bytestreamCache = bytestreamCache;
044        }
045    
046        public String getDescription()
047        {
048            return description;
049        }
050    
051        public CompressionStatus getCompression()
052        {
053            return compression;
054        }
055    
056        public String getContentType()
057        {
058            return contentType;
059        }
060    
061        public int getSize()
062        {
063            return bytestreamCache.size();
064        }
065    
066        public long getLastModified()
067        {
068            return lastModified;
069        }
070    
071        public void streamTo(OutputStream os) throws IOException
072        {
073            bytestreamCache.writeTo(os);
074        }
075    
076        public InputStream openStream() throws IOException
077        {
078            return bytestreamCache.openStream();
079        }
080    
081        @Override
082        public String toString()
083        {
084            return String.format("StreamableResource<%s %s %s lastModified: %tc size: %d>", contentType, description, compression.name(),
085                    lastModified, getSize());
086        }
087    }