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: PDFNumsArray.java 1305467 2012-03-26 17:39:20Z vhennebert $ */ 19 20 package org.apache.fop.pdf; 21 22 import java.io.IOException; 23 import java.io.OutputStream; 24 import java.util.Map; 25 import java.util.SortedMap; 26 27 import org.apache.commons.io.output.CountingOutputStream; 28 29 /** 30 * Class representing an "Nums" array object (for Number Trees). 31 */ 32 public class PDFNumsArray extends PDFObject { 33 34 /** Sorted Map holding the values of this array. */ 35 protected SortedMap<Integer, Object> map = new java.util.TreeMap<Integer, Object>(); 36 37 /** 38 * Create a new, empty array object. 39 * @param parent the object's parent if any 40 */ PDFNumsArray(PDFObject parent)41 public PDFNumsArray(PDFObject parent) { 42 super(parent); 43 } 44 45 /** 46 * Returns the length of the array 47 * @return the length of the array 48 */ length()49 public int length() { 50 return this.map.size(); 51 } 52 53 /** 54 * Sets an entry. 55 * @param key the key of the value to set 56 * @param obj the new value 57 */ put(Integer key, Object obj)58 public void put(Integer key, Object obj) { 59 this.map.put(key, obj); 60 } 61 62 /** 63 * Sets an entry. 64 * @param key the key of the value to set 65 * @param obj the new value 66 */ put(int key, Object obj)67 public void put(int key, Object obj) { 68 put(Integer.valueOf(key), obj); 69 } 70 71 /** 72 * Gets an entry. 73 * @param key the key of requested value 74 * @return the requested value 75 */ get(Integer key)76 public Object get(Integer key) { 77 return this.map.get(key); 78 } 79 80 /** 81 * Gets an entry. 82 * @param key the key of requested value 83 * @return the requested value 84 */ get(int key)85 public Object get(int key) { 86 return get(Integer.valueOf(key)); 87 } 88 89 /** {@inheritDoc} */ 90 @Override output(OutputStream stream)91 public int output(OutputStream stream) throws IOException { 92 CountingOutputStream cout = new CountingOutputStream(stream); 93 StringBuilder textBuffer = new StringBuilder(64); 94 textBuffer.append('['); 95 boolean first = true; 96 for (Map.Entry<Integer, Object> entry : this.map.entrySet()) { 97 if (!first) { 98 textBuffer.append(" "); 99 } 100 first = false; 101 formatObject(entry.getKey(), cout, textBuffer); 102 textBuffer.append(" "); 103 formatObject(entry.getValue(), cout, textBuffer); 104 } 105 textBuffer.append(']'); 106 PDFDocument.flushTextBuffer(textBuffer, cout); 107 return cout.getCount(); 108 } 109 110 } 111