001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: InMemoryStreamCache.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package org.apache.fop.pdf;
021:
022: import java.io.ByteArrayOutputStream;
023: import java.io.OutputStream;
024: import java.io.IOException;
025:
026: /**
027: * StreamCache implementation that uses temporary files rather than heap.
028: */
029: public class InMemoryStreamCache implements StreamCache {
030:
031: private int hintSize = -1;
032:
033: /**
034: * The current output stream.
035: */
036: private ByteArrayOutputStream output;
037:
038: /**
039: * Creates a new InMemoryStreamCache.
040: */
041: public InMemoryStreamCache() {
042: }
043:
044: /**
045: * Creates a new InMemoryStreamCache.
046: * @param hintSize a hint about the approximate expected size of the buffer
047: */
048: public InMemoryStreamCache(int hintSize) {
049: this .hintSize = hintSize;
050: }
051:
052: /**
053: * Get the current OutputStream. Do not store it - it may change
054: * from call to call.
055: * @throws IOException if there is an error getting the output stream
056: * @return the output stream containing the data
057: */
058: public OutputStream getOutputStream() throws IOException {
059: if (output == null) {
060: if (this .hintSize <= 0) {
061: output = new ByteArrayOutputStream(512);
062: } else {
063: output = new ByteArrayOutputStream(this .hintSize);
064: }
065: }
066: return output;
067: }
068:
069: /**
070: * @see org.apache.fop.pdf.StreamCache#write(byte[])
071: */
072: public void write(byte[] data) throws IOException {
073: getOutputStream().write(data);
074: }
075:
076: /**
077: * Outputs the cached bytes to the given stream.
078: * @param out the output stream to write to
079: * @return the number of bytes written
080: * @throws IOException if there is an IO error writing to the output stream
081: */
082: public int outputContents(OutputStream out) throws IOException {
083: if (output == null) {
084: return 0;
085: }
086:
087: output.writeTo(out);
088: return output.size();
089: }
090:
091: /**
092: * Returns the current size of the stream.
093: * @throws IOException if there is an error getting the size
094: * @return the length of the stream
095: */
096: public int getSize() throws IOException {
097: if (output == null) {
098: return 0;
099: } 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 {
109: if (output != null) {
110: output.close();
111: output = null;
112: }
113: }
114: }
|