KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > pdf > InMemoryStreamCache


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 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.pdf;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
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 JavaDoc output;
37
38     /**
39      * Creates a new InMemoryStreamCache.
40      */

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      */

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      */

58     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
59         if (output == null) {
60             if (this.hintSize <= 0) {
61                 output = new ByteArrayOutputStream JavaDoc(512);
62             } else {
63                 output = new ByteArrayOutputStream JavaDoc(this.hintSize);
64             }
65         }
66         return output;
67     }
68
69     /**
70      * @see org.apache.fop.pdf.StreamCache#write(byte[])
71      */

72     public void write(byte[] data) throws IOException JavaDoc {
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      */

82     public int outputContents(OutputStream JavaDoc out) throws IOException JavaDoc {
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      */

96     public int getSize() throws IOException JavaDoc {
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      */

108     public void clear() throws IOException JavaDoc {
109         if (output != null) {
110             output.close();
111             output = null;
112         }
113     }
114 }
115
Popular Tags