Skip to content

Commit ca3ebf8

Browse files
committed
refactoring to remove repeated code
1 parent 70a759a commit ca3ebf8

File tree

8 files changed

+115
-105
lines changed

8 files changed

+115
-105
lines changed

pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,27 @@
5959
</license>
6060
</licenses>
6161

62+
<reporting>
63+
<plugins>
64+
<plugin>
65+
<groupId>org.apache.maven.plugins</groupId>
66+
<artifactId>maven-jxr-plugin</artifactId>
67+
<version>2.5</version>
68+
</plugin>
69+
<plugin>
70+
<groupId>org.apache.maven.plugins</groupId>
71+
<artifactId>maven-pmd-plugin</artifactId>
72+
<version>3.4</version>
73+
<configuration>
74+
<detail>true</detail>
75+
<aggregate>true</aggregate>
76+
<format>html</format>
77+
<verbose>true</verbose>
78+
<printFailingErrors>true</printFailingErrors>
79+
</configuration>
80+
</plugin>
81+
</plugins>
82+
</reporting>
6283

6384
<dependencies>
6485
<dependency>

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

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

140-
/**
141-
* Read array of bit sequence.
142-
*
143-
* @param bitNumber bit number for each bit sequence item, must be 1..8
144-
* @param items number of items to be read, if less than zero then read whole
145-
* stream till the end
146-
* @return array of read bit items as a byte array
147-
* @throws IOException it will be thrown for any transport problem during the
148-
* operation
149-
*/
150-
public byte[] readBitsArray(final int items, final JBBPBitNumber bitNumber) throws IOException {
140+
private byte [] _readArray(final int items, final JBBPBitNumber bitNumber) throws IOException {
141+
final boolean readByteArray = bitNumber == null;
142+
151143
int pos = 0;
152144
if (items < 0) {
153145
byte[] buffer = new byte[INITIAL_ARRAY_BUFFER_SIZE];
154146
// till end
155147
while (true) {
156-
final int next = readBits(bitNumber);
148+
final int next = readByteArray ? read() : readBits(bitNumber);
157149
if (next < 0) {
158150
break;
159151
}
@@ -174,16 +166,37 @@ public byte[] readBitsArray(final int items, final JBBPBitNumber bitNumber) thro
174166
else {
175167
// number
176168
final byte[] buffer = new byte[items];
169+
if (readByteArray){
170+
final int read = this.read(buffer, 0, items);
171+
if (read != items) {
172+
throw new EOFException("Have read only " + read + " byte(s) instead of " + items + " byte(s)");
173+
}
174+
}else{
177175
for (int i = 0; i < items; i++) {
178176
final int next = readBits(bitNumber);
179177
if (next < 0) {
180178
throw new EOFException("Have read only " + i + " bit portions instead of " + items);
181179
}
182180
buffer[i] = (byte) next;
183181
}
182+
}
184183
return buffer;
185184
}
186185
}
186+
187+
/**
188+
* Read array of bit sequence.
189+
*
190+
* @param bitNumber bit number for each bit sequence item, must be 1..8
191+
* @param items number of items to be read, if less than zero then read whole
192+
* stream till the end
193+
* @return array of read bit items as a byte array
194+
* @throws IOException it will be thrown for any transport problem during the
195+
* operation
196+
*/
197+
public byte[] readBitsArray(final int items, final JBBPBitNumber bitNumber) throws IOException {
198+
return _readArray(items, bitNumber);
199+
}
187200

188201
/**
189202
* Read number of bytes for the stream.
@@ -195,38 +208,7 @@ public byte[] readBitsArray(final int items, final JBBPBitNumber bitNumber) thro
195208
* operation
196209
*/
197210
public byte[] readByteArray(final int items) throws IOException {
198-
int pos = 0;
199-
if (items < 0) {
200-
byte[] buffer = new byte[INITIAL_ARRAY_BUFFER_SIZE];
201-
// till end
202-
while (true) {
203-
final int next = read();
204-
if (next < 0) {
205-
break;
206-
}
207-
if (buffer.length == pos) {
208-
final byte[] newbuffer = new byte[buffer.length << 1];
209-
System.arraycopy(buffer, 0, newbuffer, 0, buffer.length);
210-
buffer = newbuffer;
211-
}
212-
buffer[pos++] = (byte) next;
213-
}
214-
if (buffer.length == pos) {
215-
return buffer;
216-
}
217-
final byte[] result = new byte[pos];
218-
System.arraycopy(buffer, 0, result, 0, pos);
219-
return result;
220-
}
221-
else {
222-
// number
223-
final byte[] buffer = new byte[items];
224-
final int read = this.read(buffer, 0, items);
225-
if (read != items) {
226-
throw new EOFException("Have read only " + read + " byte(s) instead of " + items + " byte(s)");
227-
}
228-
return buffer;
229-
}
211+
return _readArray(items, null);
230212
}
231213

232214
/**
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2015 Igor Maznitsa.
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+
package com.igormaznitsa.jbbp.model;
17+
18+
import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
19+
import com.igormaznitsa.jbbp.utils.JBBPUtils;
20+
21+
/**
22+
* Inside abstract class to keep common operations for byte array based entities.
23+
* @param <T> type of array item.
24+
*/
25+
abstract class AbstractFieldByteArray<T extends JBBPAbstractField> extends JBBPAbstractArrayField<T> {
26+
private static final long serialVersionUID = -884448637983315507L;
27+
28+
protected final byte [] array;
29+
30+
public AbstractFieldByteArray(final JBBPNamedFieldInfo name, final byte [] array) {
31+
super(name);
32+
JBBPUtils.assertNotNull(array, "Array must not be null");
33+
this.array = array;
34+
}
35+
36+
@Override
37+
public boolean getAsBool(final int index) {
38+
return this.array[index] != 0;
39+
}
40+
41+
@Override
42+
public long getAsLong(final int index) {
43+
return this.getAsInt(index);
44+
}
45+
46+
}

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,16 @@
2222
* Describes a byte array.
2323
* @since 1.0
2424
*/
25-
public final class JBBPFieldArrayByte extends JBBPAbstractArrayField<JBBPFieldByte>{
25+
public final class JBBPFieldArrayByte extends AbstractFieldByteArray<JBBPFieldByte>{
2626
private static final long serialVersionUID = -8100947416351943918L;
27-
/**
28-
* Inside storage of values.
29-
*/
30-
private final byte [] array;
3127

3228
/**
3329
* The Constructor.
3430
* @param name the field name info, it can be null.
3531
* @param array the values array, it must not be null
3632
*/
3733
public JBBPFieldArrayByte(final JBBPNamedFieldInfo name, final byte [] array) {
38-
super(name);
39-
JBBPUtils.assertNotNull(array, "Array must not be null");
40-
this.array = array;
34+
super(name, array);
4135
}
4236

4337
/**
@@ -68,11 +62,6 @@ public long getAsLong(final int index) {
6862
return this.getAsInt(index);
6963
}
7064

71-
@Override
72-
public boolean getAsBool(final int index) {
73-
return this.array[index] != 0;
74-
}
75-
7665
@Override
7766
public Object getValueArrayAsObject(final boolean reverseBits) {
7867
final byte[] result;

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,21 @@
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 unsigned byte array.
2322
* @since 1.0
2423
*/
25-
public final class JBBPFieldArrayUByte extends JBBPAbstractArrayField<JBBPFieldUByte>{
24+
public final class JBBPFieldArrayUByte extends AbstractFieldByteArray<JBBPFieldUByte>{
2625
private static final long serialVersionUID = -2568935326782182401L;
2726

28-
/**
29-
* The Inside value storage.
30-
*/
31-
private final byte [] array;
32-
3327
/**
3428
* The Constructor.
3529
* @param name a field name info, it can be null.
3630
* @param array a value array, it must not be null
3731
*/
3832
public JBBPFieldArrayUByte(final JBBPNamedFieldInfo name, final byte[] array) {
39-
super(name);
40-
JBBPUtils.assertNotNull(array, "Array must not be null");
41-
this.array = array;
33+
super(name,array);
4234
}
4335

4436
/**
@@ -64,16 +56,6 @@ public int getAsInt(final int index) {
6456
return this.array[index] & 0xFF;
6557
}
6658

67-
@Override
68-
public long getAsLong(final int index) {
69-
return this.getAsInt(index);
70-
}
71-
72-
@Override
73-
public boolean getAsBool(final int index) {
74-
return this.array[index] != 0;
75-
}
76-
7759
@Override
7860
public Object getValueArrayAsObject(final boolean reverseBits) {
7961
final byte[] result;

src/main/java/com/igormaznitsa/jbbp/utils/JBBPTextWriter.java

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -97,27 +97,18 @@ private String makeStructDescription(final Object obj, final Field field, final
9797
}
9898

9999
private String makeArrayDescription(final Field field, final com.igormaznitsa.jbbp.mapper.Bin annotation) {
100-
final StringBuilder result = new StringBuilder();
101-
if (annotation.name().length() == 0) {
102-
result.append(field.getName());
103-
}
104-
else {
105-
result.append(annotation.name());
106-
}
107-
if (annotation.comment().length() != 0) {
108-
result.append(", ").append(annotation.comment());
109-
}
110-
return result.toString();
100+
return makeFieldDescription(field, annotation);
111101
}
112102

113103
@Override
114104
protected void onArrayEnd(final Object obj, final Field field, final com.igormaznitsa.jbbp.mapper.Bin annotation) {
115105
try {
116106
IndentDec();
117107
HR();
118-
if (field.getType() == String.class){
108+
if (field.getType() == String.class) {
119109
Comment("END STRING : " + makeArrayDescription(field, annotation));
120-
}else{
110+
}
111+
else {
121112
Comment("END ARRAY : " + makeArrayDescription(field, annotation));
122113
}
123114
HR();
@@ -134,9 +125,10 @@ protected void onArrayEnd(final Object obj, final Field field, final com.igormaz
134125
protected void onArrayStart(final Object obj, final Field field, final com.igormaznitsa.jbbp.mapper.Bin annotation, final int length) {
135126
try {
136127
HR();
137-
if (field.getType() == String.class){
128+
if (field.getType() == String.class) {
138129
Comment("STRING: " + makeFieldDescription(field, annotation));
139-
}else{
130+
}
131+
else {
140132
Comment("START ARRAY : " + makeArrayDescription(field, annotation) + " OF " + field.getType().getComponentType().getSimpleName() + " [" + length + ']');
141133
}
142134
HR();
@@ -409,7 +401,7 @@ public interface Extra {
409401
* The Default value postfix.
410402
*/
411403
private static final String DEFAULT_VALUE_POSTFIX = "";
412-
404+
413405
/**
414406
* The Default horizontal rule prefix.
415407
*/
@@ -549,7 +541,7 @@ public interface Extra {
549541
* The Current HR prefix.
550542
*/
551543
private String prefixHR;
552-
544+
553545
/**
554546
* The Current line position, 0 is first one.
555547
*/
@@ -807,18 +799,16 @@ private void ensureNewLineMode() throws IOException {
807799
* @throws IOException it will be thrown for transport errors
808800
*/
809801
private void printValueString(final String value) throws IOException {
810-
if (this.valuesLineCounter > 0) {
811-
if (this.valueSeparator.length() > 0) {
812-
this.write(this.valueSeparator);
813-
}
802+
if (this.valuesLineCounter > 0 && this.valueSeparator.length() > 0) {
803+
this.write(this.valueSeparator);
814804
}
815805

816806
if (this.prefixValue.length() > 0) {
817807
this.write(this.prefixValue);
818808
}
819-
809+
820810
this.write(value);
821-
811+
822812
if (this.postfixValue.length() > 0) {
823813
this.write(this.postfixValue);
824814
}
@@ -867,11 +857,11 @@ public JBBPTextWriter Str(final String... str) throws IOException {
867857
final String oldPostfix = this.postfixValue;
868858
this.prefixValue = "";
869859
this.postfixValue = "";
870-
860+
871861
for (final String s : str) {
872862
printValueString(s == null ? "<NULL>" : s);
873863
}
874-
864+
875865
this.prefixValue = oldPrefix;
876866
this.postfixValue = oldPostfix;
877867
return this;
@@ -1381,6 +1371,7 @@ public JBBPTextWriter BR() throws IOException {
13811371

13821372
/**
13831373
* Change parameters for horizontal rule.
1374+
*
13841375
* @param prefix the prefix to be printed before rule, it can be null
13851376
* @param length the length in symbols.
13861377
* @param ch symbol to draw

src/test/java/com/igormaznitsa/jbbp/it/SNAParsingTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
import com.igormaznitsa.jbbp.utils.JBBPTextWriter;
2424
import com.igormaznitsa.jbbp.utils.JBBPUtils;
2525
import java.io.InputStream;
26-
import static junit.framework.Assert.assertTrue;
27-
import static org.junit.Assert.assertEquals;
26+
import static org.junit.Assert.*;
2827
import org.junit.Test;
2928

3029
/**

src/test/java/com/igormaznitsa/jbbp/mapper/JBBPMapperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private static class MappedWithStaticField{
111111
public void testMap_IgnoreStaticField() throws Exception {
112112
final MappedWithStaticField mapped = JBBPParser.prepare("int a;").parse(new byte[]{1,2,3,4}).mapTo(MappedWithStaticField.class);
113113
assertEquals(0x01020304,mapped.a);
114-
assertEquals(111,mapped.ignored);
114+
assertEquals(111,MappedWithStaticField.ignored);
115115
}
116116

117117
@Test

0 commit comments

Comments
 (0)