Skip to content

Commit ed4df18

Browse files
committed
refactoring
1 parent ca3ebf8 commit ed4df18

File tree

7 files changed

+63
-69
lines changed

7 files changed

+63
-69
lines changed

src/main/java/com/igormaznitsa/jbbp/JBBPParser.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,11 @@ private List<JBBPAbstractField> parseStruct(final JBBPBitInputStream inStream, f
182182
break;
183183
case JBBPCompiler.CODE_SKIP: {
184184
final int skipByteNumber = JBBPUtils.unpackInt(compiled, positionAtCompiledBlock);
185-
if (resultNotIgnored) {
186-
if (skipByteNumber > 0) {
185+
if (resultNotIgnored && skipByteNumber > 0) {
187186
final long skippedBytes = inStream.skip(skipByteNumber);
188187
if (skippedBytes != skipByteNumber) {
189188
throw new EOFException("Can't skip " + skipByteNumber + " byte(s), skipped only " + skippedBytes + " byte(s)");
190189
}
191-
}
192190
}
193191
}
194192
break;

src/main/java/com/igormaznitsa/jbbp/compiler/JBBPCompiler.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
/**
3030
* The Class implements the compiler of a bin source script represented as a
3131
* text value into byte codes.
32+
*
3233
* @since 1.0
3334
*/
3435
public final class JBBPCompiler {
@@ -55,11 +56,14 @@ private static final class StructStackItem {
5556
* Named field counter value for the structure start.
5657
*/
5758
private final int namedFieldCounter;
58-
59+
5960
/**
6061
* The Constructor.
61-
* @param namedFieldCounter the named field counter value for the structure start
62-
* @param startStructureOffset the offset of the start structure byte-code instruction
62+
*
63+
* @param namedFieldCounter the named field counter value for the structure
64+
* start
65+
* @param startStructureOffset the offset of the start structure byte-code
66+
* instruction
6367
* @param code the start byte code
6468
* @param token the token
6569
*/
@@ -125,12 +129,14 @@ private StructStackItem(final int namedFieldCounter, final int startStructureOff
125129
public static final int CODE_SKIP = 0x0C;
126130

127131
/**
128-
* The Byte code of the VAR command. It describes a request to an external processor to load values from a stream.
132+
* The Byte code of the VAR command. It describes a request to an external
133+
* processor to load values from a stream.
129134
*/
130135
public static final int CODE_VAR = 0x0D;
131136

132137
/**
133-
* The Byte code of the RESET COUNTER command. It resets the inside counter of the input stream.
138+
* The Byte code of the RESET COUNTER command. It resets the inside counter of
139+
* the input stream.
134140
*/
135141
public static final int CODE_RESET_COUNTER = 0x0E;
136142

@@ -181,7 +187,7 @@ public static JBBPCompiledBlock compile(final String script) throws IOException
181187
int fieldUnrestrictedArrayOffset = -1;
182188

183189
boolean hasVarFields = false;
184-
190+
185191
for (final JBBPToken token : parser) {
186192
if (token.isComment()) {
187193
continue;
@@ -197,11 +203,9 @@ public static JBBPCompiledBlock compile(final String script) throws IOException
197203
boolean extraFieldPresented = false;
198204
int extraField = -1;
199205

200-
if ((code & 0xF) != CODE_STRUCT_END && fieldUnrestrictedArrayOffset >= 0) {
201-
// check that the field is not in the current structure which is a whole stream one
202-
if (structureStack.isEmpty() || structureStack.get(structureStack.size() - 1).startStructureOffset != fieldUnrestrictedArrayOffset) {
203-
throw new JBBPCompilationException("Attempt to read field or structure after a full stream field", token);
204-
}
206+
// check that the field is not in the current structure which is a whole stream one
207+
if ((code & 0xF) != CODE_STRUCT_END && fieldUnrestrictedArrayOffset >= 0 && (structureStack.isEmpty() || structureStack.get(structureStack.size() - 1).startStructureOffset != fieldUnrestrictedArrayOffset)) {
208+
throw new JBBPCompilationException("Attempt to read field or structure after a full stream field", token);
205209
}
206210

207211
switch (code & 0xF) {
@@ -309,12 +313,13 @@ public static JBBPCompiledBlock compile(final String script) throws IOException
309313
if (token.getFieldName() != null) {
310314
throw new JBBPCompilationException("A Reset counter field can't be named [" + token.getFieldName() + ']', token);
311315
}
312-
if (token.getFieldTypeParameters().getExtraData()!=null){
316+
if (token.getFieldTypeParameters().getExtraData() != null) {
313317
throw new JBBPCompilationException("A Reset counter field doesn't use extra value [" + token.getFieldName() + ']', token);
314318
}
315-
}break;
319+
}
320+
break;
316321
case CODE_STRUCT_START: {
317-
structureStack.add(new StructStackItem(namedFields.size() + ((code & JBBPCompiler.FLAG_NAMED)==0 ? 0 : 1), offset - 1, code, token));
322+
structureStack.add(new StructStackItem(namedFields.size() + ((code & JBBPCompiler.FLAG_NAMED) == 0 ? 0 : 1), offset - 1, code, token));
318323
}
319324
break;
320325
case CODE_STRUCT_END: {
@@ -361,7 +366,7 @@ public static JBBPCompiledBlock compile(final String script) throws IOException
361366
if ((code & FLAG_NAMED) != 0) {
362367
final String normalizedName = JBBPUtils.normalizeFieldNameOrPath(token.getFieldName());
363368
assertName(normalizedName, token);
364-
registerNamedField(normalizedName, structureStack.isEmpty() ? 0 : structureStack.get(structureStack.size()-1).namedFieldCounter, startFieldOffset, namedFields, token);
369+
registerNamedField(normalizedName, structureStack.isEmpty() ? 0 : structureStack.get(structureStack.size() - 1).namedFieldCounter, startFieldOffset, namedFields, token);
365370
}
366371
else {
367372
if (currentClosedStructure != null && (currentClosedStructure.code & FLAG_NAMED) != 0) {
@@ -399,16 +404,17 @@ public static JBBPCompiledBlock compile(final String script) throws IOException
399404

400405
/**
401406
* The Method checks a value for negative.
407+
*
402408
* @param value a value to be checked
403409
* @param token the tokens related to the value
404410
* @throws JBBPCompilationException if the value is a negative one
405411
*/
406-
private static void assertNonNegativeValue(final int value, final JBBPToken token){
407-
if (value<0) {
412+
private static void assertNonNegativeValue(final int value, final JBBPToken token) {
413+
if (value < 0) {
408414
throw new JBBPCompilationException("Detected unsupported negative value for a field must have only zero or a positive one", token);
409415
}
410416
}
411-
417+
412418
/**
413419
* The Method check that a field name supports business rules.
414420
*
@@ -433,9 +439,9 @@ private static void assertName(final String name, final JBBPToken token) {
433439
* the path
434440
*/
435441
private static void registerNamedField(final String normalizedName, final int structureBorder, final int offset, final List<JBBPNamedFieldInfo> namedFields, final JBBPToken token) {
436-
for(int i=namedFields.size()-1;i>=structureBorder;i--){
442+
for (int i = namedFields.size() - 1; i >= structureBorder; i--) {
437443
final JBBPNamedFieldInfo info = namedFields.get(i);
438-
if (info.getFieldPath().equals(normalizedName)){
444+
if (info.getFieldPath().equals(normalizedName)) {
439445
throw new JBBPCompilationException("Duplicated named field detected [" + normalizedName + ']', token);
440446
}
441447
}

src/main/java/com/igormaznitsa/jbbp/io/JBBPBitInputStream.java

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ public boolean[] readBoolArray(final int items) throws IOException {
137137
return result;
138138
}
139139

140-
private byte [] _readArray(final int items, final JBBPBitNumber bitNumber) throws IOException {
140+
private byte[] _readArray(final int items, final JBBPBitNumber bitNumber) throws IOException {
141141
final boolean readByteArray = bitNumber == null;
142-
142+
143143
int pos = 0;
144144
if (items < 0) {
145145
byte[] buffer = new byte[INITIAL_ARRAY_BUFFER_SIZE];
@@ -166,24 +166,25 @@ public boolean[] readBoolArray(final int items) throws IOException {
166166
else {
167167
// number
168168
final byte[] buffer = new byte[items];
169-
if (readByteArray){
169+
if (readByteArray) {
170170
final int read = this.read(buffer, 0, items);
171171
if (read != items) {
172172
throw new EOFException("Have read only " + read + " byte(s) instead of " + items + " byte(s)");
173173
}
174-
}else{
175-
for (int i = 0; i < items; i++) {
176-
final int next = readBits(bitNumber);
177-
if (next < 0) {
178-
throw new EOFException("Have read only " + i + " bit portions instead of " + items);
179-
}
180-
buffer[i] = (byte) next;
181174
}
175+
else {
176+
for (int i = 0; i < items; i++) {
177+
final int next = readBits(bitNumber);
178+
if (next < 0) {
179+
throw new EOFException("Have read only " + i + " bit portions instead of " + items);
180+
}
181+
buffer[i] = (byte) next;
182+
}
182183
}
183184
return buffer;
184185
}
185186
}
186-
187+
187188
/**
188189
* Read array of bit sequence.
189190
*
@@ -482,11 +483,13 @@ public int readBits(final JBBPBitNumber numOfBitsToRead) throws IOException {
482483
int theBitBuffer = this.bitBuffer;
483484
int theBitBufferCounter = this.bitsInBuffer;
484485

485-
final boolean doIncCounter = theBitBufferCounter!=0;
486-
486+
final boolean doIncCounter = theBitBufferCounter != 0;
487+
487488
while (i > 0) {
488489
if (theBitBufferCounter == 0) {
489-
if (doIncCounter) this.byteCounter++;
490+
if (doIncCounter) {
491+
this.byteCounter++;
492+
}
490493
final int nextByte = this.readByteFromStream();
491494
if (nextByte < 0) {
492495
if (i == numOfBitsAsNumber) {
@@ -510,7 +513,7 @@ public int readBits(final JBBPBitNumber numOfBitsToRead) throws IOException {
510513

511514
this.bitBuffer = theBitBuffer;
512515
this.bitsInBuffer = theBitBufferCounter;
513-
516+
514517
return JBBPUtils.reverseBitsInByte(JBBPBitNumber.decode(numOfBitsAsNumber - i), (byte) result) & 0xFF;
515518
}
516519
}
@@ -543,11 +546,6 @@ public int readByte() throws IOException {
543546
return read;
544547
}
545548

546-
@Override
547-
public boolean markSupported() {
548-
return super.markSupported();
549-
}
550-
551549
@Override
552550
public synchronized void reset() throws IOException {
553551
in.reset();
@@ -564,11 +562,6 @@ public synchronized void mark(final int readlimit) {
564562
this.markedBitsInBuffer = this.bitsInBuffer;
565563
}
566564

567-
@Override
568-
public int available() throws IOException {
569-
return super.available();
570-
}
571-
572565
/**
573566
* Read padding bytes from the stream and ignore them to align the stream
574567
* counter.
@@ -625,10 +618,8 @@ public long skip(final long numOfBytes) throws IOException {
625618
*/
626619
private int readByteFromStream() throws IOException {
627620
int result = this.in.read();
628-
if (result >= 0) {
629-
if (this.msb0) {
630-
result = JBBPUtils.reverseBitsInByte((byte) result) & 0xFF;
631-
}
621+
if (result >= 0 && this.msb0) {
622+
result = JBBPUtils.reverseBitsInByte((byte) result) & 0xFF;
632623
}
633624
return result;
634625
}

src/main/java/com/igormaznitsa/jbbp/model/AbstractFieldByteArray.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
/**
2222
* Inside abstract class to keep common operations for byte array based entities.
2323
* @param <T> type of array item.
24+
*
25+
* @since 1.1.1
2426
*/
2527
abstract class AbstractFieldByteArray<T extends JBBPAbstractField> extends JBBPAbstractArrayField<T> {
2628
private static final long serialVersionUID = -884448637983315507L;

src/main/java/com/igormaznitsa/jbbp/model/JBBPFieldArrayByte.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package com.igormaznitsa.jbbp.model;
1717

1818
import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
19-
import com.igormaznitsa.jbbp.utils.JBBPUtils;
2019

2120
/**
2221
* Describes a byte array.

src/main/java/com/igormaznitsa/jbbp/model/JBBPFieldStruct.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
*/
1616
package com.igormaznitsa.jbbp.model;
1717

18-
import com.igormaznitsa.jbbp.JBBPParser;
1918
import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
2019
import com.igormaznitsa.jbbp.exceptions.JBBPFinderException;
2120
import com.igormaznitsa.jbbp.exceptions.JBBPTooManyFieldsFoundException;
22-
import com.igormaznitsa.jbbp.io.JBBPOut;
2321
import com.igormaznitsa.jbbp.mapper.JBBPMapper;
2422
import com.igormaznitsa.jbbp.mapper.JBBPMapperCustomFieldProcessor;
2523
import com.igormaznitsa.jbbp.model.finder.JBBPFieldFinder;

0 commit comments

Comments
 (0)