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 package org.apache.fontbox.cff; 18 19 import java.io.ByteArrayOutputStream; 20 import java.io.IOException; 21 22 /** 23 * 24 * @author Villu Ruusmann 25 * @version $Revision: 1.0 $ 26 */ 27 public class DataOutput 28 { 29 30 private ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream(); 31 32 private String outputEncoding = null; 33 34 /** 35 * Constructor. 36 */ 37 public DataOutput() 38 { 39 this("ISO-8859-1"); 40 } 41 42 /** 43 * Constructor with a given encoding. 44 * @param encoding the encoding to be used for writing 45 */ 46 public DataOutput(String encoding) 47 { 48 this.outputEncoding = encoding; 49 } 50 51 /** 52 * Returns the written data buffer as byte array. 53 * @return the data buffer as byte array 54 */ 55 public byte[] getBytes() 56 { 57 return outputBuffer.toByteArray(); 58 } 59 60 /** 61 * Write an int value to the buffer. 62 * @param value the given value 63 */ 64 public void write(int value) 65 { 66 outputBuffer.write(value); 67 } 68 69 /** 70 * Write a byte array to the buffer. 71 * @param buffer the given byte array 72 */ 73 public void write(byte[] buffer) 74 { 75 outputBuffer.write(buffer, 0, buffer.length); 76 } 77 78 /** 79 * Write a part of a byte array to the buffer. 80 * @param buffer the given byte buffer 81 * @param offset the offset where to start 82 * @param length the amount of bytes to be written from the array 83 */ 84 public void write(byte[] buffer, int offset, int length) 85 { 86 outputBuffer.write(buffer, offset, length); 87 } 88 89 /** 90 * Write the given string to the buffer using the given encoding. 91 * @param string the given string 92 * @throws IOException If an error occurs during writing the data to the buffer 93 */ 94 public void print(String string) throws IOException 95 { 96 write(string.getBytes(outputEncoding)); 97 } 98 99 /** 100 * Write the given string to the buffer using the given encoding. 101 * A newline is added after the given string 102 * @param string the given string 103 * @throws IOException If an error occurs during writing the data to the buffer 104 */ 105 public void println(String string) throws IOException 106 { 107 write(string.getBytes(outputEncoding)); 108 write('\n'); 109 } 110 111 /** 112 * Add a newline to the given string. 113 */ 114 public void println() 115 { 116 write('\n'); 117 } 118 }