KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > afp > tools > StructuredFieldReader


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: StructuredFieldReader.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.afp.tools;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24
25 /**
26  * A helper class to read structured fields from a MO:DCA document. Each
27  * component of a mixed object document is explicitly defined and delimited
28  * in the data. This is accomplished through the use of MO:DCA data structures,
29  * called structured fields. Structured fields are used to envelop document
30  * components and to provide commands and information to applications using
31  * the data. Structured fields may contain one or more parameters. Each
32  * parameter provides one value from a set of values defined by the architecture.
33  * <p/>
34  * MO:DCA structured fields consist of two parts: an introducer that identifies
35  * the length and type of the structured field, and data that provides the
36  * structured field's effect. The data is contained in a set of parameters,
37  * which can consist of other data structures and data elements. The maximum
38  * length of a structured field is 32767 bytes.
39  * <p/>
40  */

41 public class StructuredFieldReader {
42
43     /**
44      * The input stream to read
45      */

46     private InputStream JavaDoc _inputStream = null;
47
48     /**
49      * The constructor for the StructuredFieldReader
50      * @param inputStream the input stream to process
51      */

52     public StructuredFieldReader(InputStream JavaDoc inputStream) {
53
54         _inputStream = inputStream;
55
56     }
57
58     /**
59      * Get the next structured field as identified by the identifer
60      * parameter (this must be a valid MO:DCA structured field.
61      * @param identifier the three byte identifier
62      * @throws IOException
63      */

64     public byte[] getNext(byte[] identifier) throws IOException JavaDoc {
65
66         int bufferPointer = 0;
67         byte[] bufferData = new byte[identifier.length + 2];
68         for (int x = 0; x < identifier.length; x++) {
69             bufferData[x] = (byte) 0;
70         }
71
72         int c;
73         while ((c = _inputStream.read()) > -1) {
74
75             bufferData[bufferPointer] = (byte) c;
76
77             // Check the last characters in the buffer
78
int index = 0;
79             boolean found = true;
80
81             for (int i = identifier.length - 1; i > -1; i--) {
82
83                 int p = bufferPointer - index;
84                 if (p < 0) {
85                     p = bufferData.length + p;
86                 }
87
88                 index++;
89
90                 if (identifier[i] != bufferData[p]) {
91                     found = false;
92                     break;
93                 }
94
95             }
96
97             if (found) {
98
99                 byte[] length = new byte[2];
100
101                 int a = bufferPointer - identifier.length;
102                 if (a < 0) {
103                     a = bufferData.length + a;
104                 }
105
106                 int b = bufferPointer - identifier.length - 1;
107                 if (b < 0) {
108                     b = bufferData.length + b;
109                 }
110
111                 length[0] = bufferData[b];
112                 length[1] = bufferData[a];
113
114                 int reclength = ((length[0] & 0xFF) << 8)
115                                 + (length[1] & 0xFF) - identifier.length - 2;
116
117                 byte[] retval = new byte[reclength];
118
119                 _inputStream.read(retval, 0, reclength);
120
121                 return retval;
122
123             }
124
125             bufferPointer++;
126             if (bufferPointer >= bufferData.length) {
127                 bufferPointer = 0;
128             }
129
130         }
131
132         return new byte[] {
133         };
134
135     }
136
137 }
138
Popular Tags