17
17
18
18
import java .io .ByteArrayOutputStream ;
19
19
import java .nio .ByteBuffer ;
20
+ import java .util .LinkedList ;
21
+ import java .util .Queue ;
20
22
21
23
/**
22
24
* A PclCommandReader implementation using the java.nio.ByteBuffer
@@ -26,6 +28,7 @@ public class ByteBufferPclCommandReader implements PclCommandReader {
26
28
private PclUtil pclUtil = new PclUtil ();
27
29
protected ByteBuffer buffer ;
28
30
private long filePosition = 0 ;
31
+ private Queue <PclCommand > queuedCommands = new LinkedList <PclCommand >();
29
32
30
33
public ByteBufferPclCommandReader (byte [] entirePclFileContents ) {
31
34
this (ByteBuffer .wrap (entirePclFileContents ));
@@ -45,56 +48,107 @@ public void skip(long numberOfBytesToSkip) throws PclCommandReaderException {
45
48
}
46
49
47
50
public PclCommand nextCommand () throws PclCommandReaderException {
51
+ if (commandsAreQueued ()) {
52
+ return queuedCommands .remove ();
53
+ }
54
+
48
55
if (isFileEmpty () || isEOF ()) {
49
56
return null ;
50
57
}
51
58
52
59
long commandPosition = filePosition ;
53
60
byte currentByte = readNextByte ();
54
61
if (pclUtil .isEscape (currentByte )) {
55
- return pclCommand (commandPosition );
62
+ pclCommand (commandPosition );
63
+ return queuedCommands .remove ();
56
64
} else {
57
65
return textCommand (commandPosition , currentByte );
58
66
}
59
67
}
60
68
61
- private PclCommand pclCommand (long commandPosition ) {
62
- PclCommand command = null ;
69
+ private void pclCommand (long initialCommandPosition ) {
70
+ long commandPosition = initialCommandPosition ;
71
+ ByteArrayOutputStream commandPrefixBytes = new ByteArrayOutputStream ();
72
+ commandPrefixBytes .write (PclUtil .ESCAPE );
63
73
ByteArrayOutputStream commandData = new ByteArrayOutputStream ();
64
74
commandData .write (PclUtil .ESCAPE );
75
+
65
76
boolean isFirstRead = true ;
77
+ boolean captureByte = true ;
78
+ boolean escapeSequenceComplete = false ;
79
+ boolean groupByteFound = false ;
80
+ boolean parameterByteFound = false ;
66
81
boolean commandCompleted = false ;
82
+ boolean compressedCommand = false ;
67
83
68
- while (!commandCompleted && isNotEOF ()) {
84
+ while (!escapeSequenceComplete && isNotEOF ()) {
69
85
byte currentByte = readNextByte ();
70
- commandData .write (currentByte );
71
86
72
- if (isFirstRead && pclUtil .is2ByteCommandOperator (currentByte )) {
87
+ if (!groupByteFound ) {
88
+ commandPrefixBytes .write (currentByte );
89
+ }
90
+
91
+
92
+ if (isTwoByteCommand (isFirstRead , currentByte )) {
93
+ escapeSequenceComplete = true ;
94
+ } else if (!parameterByteFound ) {
95
+ parameterByteFound = pclUtil .isParameterizedCharacter (currentByte );
96
+ } else if (!groupByteFound ) {
97
+ groupByteFound = pclUtil .isGroupCharacter (currentByte );
98
+ } else if (groupByteFound && pclUtil .isParameterCharacter (currentByte ) && isNextByteNotAnEscapeByte ()) {
73
99
commandCompleted = true ;
100
+ compressedCommand = true ;
101
+ currentByte = pclUtil .changeParameterToTerminator (currentByte );
74
102
} else if (pclUtil .isTermination (currentByte )) {
75
- if (pclUtil .isCommandExpectingData (commandData .toByteArray ())) {
103
+ escapeSequenceComplete = true ;
104
+ } else if (pclUtil .isEscape (currentByte )) {
105
+ undoRead ();
106
+ escapeSequenceComplete = true ;
107
+ captureByte = false ;
108
+ }
109
+
110
+ if (captureByte ) {
111
+ commandData .write (currentByte );
112
+ }
113
+
114
+ if (escapeSequenceComplete || commandCompleted || isLastByteInFile ()) {
115
+ if (!isFirstRead && pclUtil .isCommandExpectingData (commandData .toByteArray ())) {
76
116
captureBinaryData (commandData );
77
117
}
78
- commandCompleted = true ;
118
+
119
+ queuedCommands .add (pclCommandFactory .build (commandPosition , commandData .toByteArray ()));
120
+ commandPosition = filePosition ;
121
+
122
+ if (compressedCommand ) {
123
+ commandData .reset ();
124
+ commandData .write (commandPrefixBytes .toByteArray (), 0 , 3 );
125
+ commandCompleted = false ;
126
+ }
79
127
}
80
128
81
129
isFirstRead = false ;
82
130
}
131
+ }
83
132
133
+ private boolean isLastByteInFile () {
84
134
if (isEOF ()) {
85
- commandCompleted = true ;
135
+ return true ;
86
136
}
87
137
88
- if (commandCompleted ) {
89
- command = pclCommandFactory .build (commandPosition , commandData .toByteArray ());
90
- }
138
+ return buffer .position () == buffer .capacity ();
139
+ }
91
140
92
- return command ;
141
+ private boolean isNextByteNotAnEscapeByte () {
142
+ return !isNextByteAnEscapeByte ();
143
+ }
144
+
145
+ private boolean isTwoByteCommand (boolean firstRead , byte currentByte ) {
146
+ return firstRead && pclUtil .is2ByteCommandOperator (currentByte );
93
147
}
94
148
95
149
private void captureBinaryData (ByteArrayOutputStream commandData ) {
96
- String valueAsString = new String (pclUtil .getValue (commandData .toByteArray ())).replaceAll ("\\ .[0-9]*" , "" );
97
- Integer numberOfBytesToRead = Integer .valueOf (valueAsString );
150
+ String valueAsString = new String (pclUtil .getValue (commandData .toByteArray ())).replaceAll ("\\ +| \\ .[0-9]*| \\ s+ " , "" );
151
+ Integer numberOfBytesToRead = valueAsString . length () == 0 ? 0 : Integer .valueOf (valueAsString );
98
152
int count = 0 ;
99
153
while (isNotEOF () && count < numberOfBytesToRead ) {
100
154
commandData .write (readNextByte ());
@@ -149,7 +203,12 @@ private boolean isFileEmpty() {
149
203
return buffer .capacity () == 0 ;
150
204
}
151
205
206
+ private boolean commandsAreQueued () {
207
+ return queuedCommands .size () > 0 ;
208
+ }
209
+
152
210
public void close () {
153
211
154
212
}
213
+
155
214
}
0 commit comments