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.

StructuredFieldReader.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.render.afp.tools;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. /**
  21. * A helper class to read structured fields from a MO:DCA document. Each
  22. * component of a mixed object document is explicitly defined and delimited
  23. * in the data. This is accomplished through the use of MO:DCA data structures,
  24. * called structured fields. Structured fields are used to envelop document
  25. * components and to provide commands and information to applications using
  26. * the data. Structured fields may contain one or more parameters. Each
  27. * parameter provides one value from a set of values defined by the architecture.
  28. * <p/>
  29. * MO:DCA structured fields consist of two parts: an introducer that identifies
  30. * the length and type of the structured field, and data that provides the
  31. * structured field's effect. The data is contained in a set of parameters,
  32. * which can consist of other data structures and data elements. The maximum
  33. * length of a structured field is 32767 bytes.
  34. * <p/>
  35. */
  36. public class StructuredFieldReader {
  37. /**
  38. * The input stream to read
  39. */
  40. private InputStream _inputStream = null;
  41. /**
  42. * The constructor for the StructuredFieldReader
  43. * @param inputStream the input stream to process
  44. */
  45. public StructuredFieldReader(InputStream inputStream) {
  46. _inputStream = inputStream;
  47. }
  48. /**
  49. * Get the next structured field as identified by the identifer
  50. * parameter (this must be a valid MO:DCA structured field.
  51. * @param identifier the three byte identifier
  52. */
  53. public byte[] getNext(byte[] identifier) throws IOException {
  54. int bufferPointer = 0;
  55. byte[] bufferData = new byte[identifier.length + 2];
  56. for (int x = 0; x < identifier.length; x++) {
  57. bufferData[x] = (byte) 0;
  58. }
  59. int c;
  60. while ((c = _inputStream.read()) > -1) {
  61. bufferData[bufferPointer] = (byte) c;
  62. // Check the last characters in the buffer
  63. int index = 0;
  64. boolean found = true;
  65. for (int i = identifier.length - 1; i > -1; i--) {
  66. int p = bufferPointer - index;
  67. if (p < 0) {
  68. p = bufferData.length + p;
  69. }
  70. index++;
  71. if (identifier[i] != bufferData[p]) {
  72. found = false;
  73. break;
  74. }
  75. }
  76. if (found) {
  77. byte[] length = new byte[2];
  78. int a = bufferPointer - identifier.length;
  79. if (a < 0) {
  80. a = bufferData.length + a;
  81. }
  82. int b = bufferPointer - identifier.length - 1;
  83. if (b < 0) {
  84. b = bufferData.length + b;
  85. }
  86. length[0] = bufferData[b];
  87. length[1] = bufferData[a];
  88. int reclength = ((length[0] & 0xFF) << 8) + (length[1] & 0xFF) - identifier.length -2;
  89. byte[] retval = new byte[reclength];
  90. _inputStream.read(retval, 0, reclength);
  91. return retval;
  92. }
  93. bufferPointer++;
  94. if (bufferPointer >= bufferData.length) {
  95. bufferPointer = 0;
  96. }
  97. }
  98. return new byte[] {
  99. };
  100. }
  101. }