1 17 18 19 20 package org.apache.fop.pdf; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.OutputStream ; 24 import java.io.IOException ; 25 26 29 public class InMemoryStreamCache implements StreamCache { 30 31 private int hintSize = -1; 32 33 36 private ByteArrayOutputStream output; 37 38 41 public InMemoryStreamCache() { 42 } 43 44 48 public InMemoryStreamCache(int hintSize) { 49 this.hintSize = hintSize; 50 } 51 52 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 72 public void write(byte[] data) throws IOException { 73 getOutputStream().write(data); 74 } 75 76 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 96 public int getSize() throws IOException { 97 if (output == null) { 98 return 0; 99 } else { 100 return output.size(); 101 } 102 } 103 104 108 public void clear() throws IOException { 109 if (output != null) { 110 output.close(); 111 output = null; 112 } 113 } 114 } 115 | Popular Tags |