You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PDFNumsArray.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /* $Id$ */
  18. package org.apache.fop.pdf;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.io.Writer;
  22. import java.util.Iterator;
  23. import java.util.Map;
  24. import java.util.SortedMap;
  25. import org.apache.commons.io.output.CountingOutputStream;
  26. /**
  27. * Class representing an "Nums" array object (for Number Trees).
  28. */
  29. public class PDFNumsArray extends PDFObject {
  30. /** Sorted Map holding the values of this array. */
  31. protected SortedMap map = new java.util.TreeMap();
  32. /**
  33. * Create a new, empty array object.
  34. * @param parent the object's parent if any
  35. */
  36. public PDFNumsArray(PDFObject parent) {
  37. super(parent);
  38. }
  39. /**
  40. * Returns the length of the array
  41. * @return the length of the array
  42. */
  43. public int length() {
  44. return this.map.size();
  45. }
  46. /**
  47. * Sets an entry.
  48. * @param key the key of the value to set
  49. * @param obj the new value
  50. */
  51. public void put(int key, Object obj) {
  52. this.map.put(new Integer(key), obj);
  53. }
  54. /**
  55. * Gets an entry.
  56. * @param key the key of requested value
  57. * @return the requested value
  58. */
  59. public Object get(int key) {
  60. return this.map.get(new Integer(key));
  61. }
  62. /** {@inheritDoc} */
  63. protected int output(OutputStream stream) throws IOException {
  64. CountingOutputStream cout = new CountingOutputStream(stream);
  65. Writer writer = PDFDocument.getWriterFor(cout);
  66. if (hasObjectNumber()) {
  67. writer.write(getObjectID());
  68. }
  69. writer.write('[');
  70. boolean first = true;
  71. Iterator iter = this.map.entrySet().iterator();
  72. while (iter.hasNext()) {
  73. Map.Entry entry = (Map.Entry)iter.next();
  74. if (!first) {
  75. writer.write(" ");
  76. }
  77. first = false;
  78. formatObject(entry.getKey(), cout, writer);
  79. writer.write(" ");
  80. formatObject(entry.getValue(), cout, writer);
  81. }
  82. writer.write(']');
  83. if (hasObjectNumber()) {
  84. writer.write("\nendobj\n");
  85. }
  86. writer.flush();
  87. return cout.getCount();
  88. }
  89. }