1 17 18 19 20 package org.apache.fop.render.afp.tools; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 25 41 public class StructuredFieldReader { 42 43 46 private InputStream _inputStream = null; 47 48 52 public StructuredFieldReader(InputStream inputStream) { 53 54 _inputStream = inputStream; 55 56 } 57 58 64 public byte[] getNext(byte[] identifier) throws IOException { 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 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 |