1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /* $Id: InMemoryStreamCache.java 1296526 2012-03-03 00:18:45Z gadams $ */
19 
20 package org.apache.fop.pdf;
21 
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.OutputStream;
25 
26 /**
27  * StreamCache implementation that uses temporary files rather than heap.
28  */
29 public class InMemoryStreamCache implements StreamCache {
30 
31     private int hintSize = -1;
32 
33     /**
34      * The current output stream.
35      */
36     private ByteArrayOutputStream output;
37 
38     /**
39      * Creates a new InMemoryStreamCache.
40      */
InMemoryStreamCache()41     public InMemoryStreamCache() {
42     }
43 
44     /**
45      * Creates a new InMemoryStreamCache.
46      * @param hintSize a hint about the approximate expected size of the buffer
47      */
InMemoryStreamCache(int hintSize)48     public InMemoryStreamCache(int hintSize) {
49         this.hintSize = hintSize;
50     }
51 
52     /**
53      * Get the current OutputStream. Do not store it - it may change
54      * from call to call.
55      * @throws IOException if there is an error getting the output stream
56      * @return the output stream containing the data
57      */
getOutputStream()58     public OutputStream getOutputStream() throws IOException {
59         if (output == null) {
60             if (this.hintSize <= 0) {
61                 output = new ByteArrayOutputStream(512);
62             } else {
63                 output = new ByteArrayOutputStream(this.hintSize);
64             }
65         }
66         return output;
67     }
68 
69     /**
70      * {@inheritDoc}
71      */
write(byte[] data)72     public void write(byte[] data) throws IOException {
73         getOutputStream().write(data);
74     }
75 
76     /**
77      * Outputs the cached bytes to the given stream.
78      * @param out the output stream to write to
79      * @return the number of bytes written
80      * @throws IOException if there is an IO error writing to the output stream
81      */
outputContents(OutputStream out)82     public int outputContents(OutputStream out) throws IOException {
83         if (output == null) {
84             return 0;
85         }
86 
87         output.writeTo(out);
88         return output.size();
89     }
90 
91     /**
92      * Returns the current size of the stream.
93      * @throws IOException if there is an error getting the size
94      * @return the length of the stream
95      */
getSize()96     public int getSize() throws IOException {
97         if (output == null) {
98             return 0;
99         } else {
100             return output.size();
101         }
102     }
103 
104     /**
105      * Clears and resets the cache.
106      * @throws IOException if there is an error closing the stream
107      */
clear()108     public void clear() throws IOException {
109         if (output != null) {
110             output.close();
111             output = null;
112         }
113     }
114 }
115