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.

ObjectStream.java 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.ByteArrayOutputStream;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import org.apache.fop.pdf.xref.CompressedObjectReference;
  25. /**
  26. * An object stream, as described in section 3.4.6 of the PDF 1.5 Reference.
  27. */
  28. public class ObjectStream extends PDFStream {
  29. private static final PDFName OBJ_STM = new PDFName("ObjStm");
  30. private List<CompressedObject> objects = new ArrayList<CompressedObject>();
  31. private int firstObjectOffset;
  32. ObjectStream() {
  33. super(false);
  34. }
  35. ObjectStream(ObjectStream previous) {
  36. this();
  37. put("Extends", previous);
  38. }
  39. CompressedObjectReference addObject(CompressedObject obj) {
  40. if (obj == null) {
  41. throw new NullPointerException("obj must not be null");
  42. }
  43. CompressedObjectReference reference = new CompressedObjectReference(obj.getObjectNumber(),
  44. getObjectNumber(), objects.size());
  45. objects.add(obj);
  46. return reference;
  47. }
  48. @Override
  49. protected void outputRawStreamData(OutputStream out) throws IOException {
  50. int currentOffset = 0;
  51. StringBuilder offsetsPart = new StringBuilder();
  52. ByteArrayOutputStream streamContent = new ByteArrayOutputStream();
  53. for (CompressedObject object : objects) {
  54. offsetsPart.append(object.getObjectNumber())
  55. .append(' ')
  56. .append(currentOffset)
  57. .append('\n');
  58. currentOffset += object.output(streamContent);
  59. }
  60. byte[] offsets = PDFDocument.encode(offsetsPart.toString());
  61. firstObjectOffset = offsets.length;
  62. out.write(offsets);
  63. streamContent.writeTo(out);
  64. }
  65. @Override
  66. protected void populateStreamDict(Object lengthEntry) {
  67. put("Type", OBJ_STM);
  68. put("N", objects.size());
  69. put("First", firstObjectOffset);
  70. super.populateStreamDict(lengthEntry);
  71. }
  72. }