Skip to content

Commit 3681c0c

Browse files
committed
sonar
1 parent 495a7a2 commit 3681c0c

21 files changed

+486
-445
lines changed

net.lecousin.core/src/main/java/net/lecousin/framework/encoding/HexaDecimalEncoding.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ public DecoderConsumer(
176176
public IAsync<TError> consume(Bytes.Readable data, Consumer<Bytes.Readable> onDataRelease) {
177177
int len = ((firstChar != -1 ? 1 : 0) + data.remaining()) / 2;
178178
if (len == 0) {
179-
if (data.remaining() == 1)
180-
firstChar = data.get() & 0xFF;
179+
firstChar = data.get() & 0xFF;
181180
if (onDataRelease != null)
182181
onDataRelease.accept(data);
183182
return new Async<>(true);
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package net.lecousin.framework.io.data;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
/** Composite Chars (multiple Chars as a single one).
8+
* @param <T> type of Chars it contains
9+
*/
10+
public abstract class AbstractComposite<T extends DataBuffer> implements DataBuffer {
11+
12+
/** Constructor. */
13+
public AbstractComposite() {
14+
list = new ArrayList<>();
15+
init();
16+
}
17+
18+
/** Constructor. */
19+
@SafeVarargs
20+
public AbstractComposite(T... elements) {
21+
list = new ArrayList<>(elements.length);
22+
Collections.addAll(list, elements);
23+
init();
24+
}
25+
26+
/** Constructor. */
27+
public AbstractComposite(List<T> elements) {
28+
list = new ArrayList<>(elements);
29+
init();
30+
}
31+
32+
protected ArrayList<T> list;
33+
protected int position;
34+
protected int index;
35+
protected int length;
36+
37+
private void init() {
38+
position = 0;
39+
index = 0;
40+
length = 0;
41+
for (T b : list) {
42+
b.setPosition(0);
43+
length += b.remaining();
44+
}
45+
}
46+
47+
public List<T> getWrappedBuffers() {
48+
return list;
49+
}
50+
51+
/** Append a new buffer to this composite. */
52+
public void add(T buffer) {
53+
list.add(buffer);
54+
buffer.setPosition(0);
55+
length += buffer.remaining();
56+
}
57+
58+
@Override
59+
public int length() {
60+
return length;
61+
}
62+
63+
@Override
64+
public int position() {
65+
return position;
66+
}
67+
68+
@Override
69+
public void setPosition(int position) {
70+
if (position == this.position)
71+
return;
72+
if (position < this.position) {
73+
int toMove = this.position - position;
74+
if (index == list.size()) index--;
75+
do {
76+
T elements = list.get(index);
77+
int p = elements.position();
78+
if (toMove <= p) {
79+
elements.setPosition(p - toMove);
80+
this.position = position;
81+
return;
82+
}
83+
toMove -= p;
84+
index--;
85+
elements.setPosition(0);
86+
} while (true);
87+
}
88+
int toMove = position - this.position;
89+
do {
90+
T elements = list.get(index);
91+
int r = elements.remaining();
92+
if (toMove < r) {
93+
elements.moveForward(toMove);
94+
this.position = position;
95+
return;
96+
}
97+
elements.moveForward(r);
98+
toMove -= r;
99+
index++;
100+
} while (toMove > 0);
101+
this.position = position;
102+
}
103+
104+
@Override
105+
public int remaining() {
106+
return length - position;
107+
}
108+
109+
@Override
110+
public boolean hasRemaining() {
111+
return position < length;
112+
}
113+
114+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package net.lecousin.framework.io.data;
2+
3+
/** Chars Readable implementation wrapping a String. */
4+
public abstract class AbstractStringAsReadableDataBuffer implements DataBuffer {
5+
6+
/** Constructor. */
7+
public AbstractStringAsReadableDataBuffer(String str) {
8+
this(str, 0, str.length());
9+
}
10+
11+
/** Constructor. */
12+
public AbstractStringAsReadableDataBuffer(String str, int offset, int length) {
13+
this.str = str;
14+
this.offset = offset;
15+
this.length = length;
16+
}
17+
18+
protected String str;
19+
protected int offset;
20+
protected int length;
21+
protected int pos = 0;
22+
23+
@Override
24+
public int length() {
25+
return length;
26+
}
27+
28+
@Override
29+
public int position() {
30+
return pos;
31+
}
32+
33+
@Override
34+
public void setPosition(int position) {
35+
pos = position;
36+
}
37+
38+
@Override
39+
public int remaining() {
40+
return length - pos;
41+
}
42+
43+
}

net.lecousin.core/src/main/java/net/lecousin/framework/io/data/CompositeBytes.java

Lines changed: 5 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,30 @@
11
package net.lecousin.framework.io.data;
22

33
import java.nio.ByteBuffer;
4-
import java.util.ArrayList;
5-
import java.util.Collections;
64
import java.util.List;
75

86
/** Composite Chars (multiple Chars as a single one).
97
* @param <T> type of Chars it contains
108
*/
11-
public abstract class CompositeBytes<T extends Bytes> implements Bytes {
9+
public abstract class CompositeBytes<T extends Bytes> extends AbstractComposite<T> implements Bytes {
1210

1311
/** Constructor. */
1412
public CompositeBytes() {
15-
list = new ArrayList<>();
16-
init();
13+
super();
1714
}
1815

1916
/** Constructor. */
2017
@SafeVarargs
2118
public CompositeBytes(T... bytes) {
22-
list = new ArrayList<>(bytes.length);
23-
Collections.addAll(list, bytes);
24-
init();
19+
super(bytes);
2520
}
2621

2722
/** Constructor. */
2823
public CompositeBytes(List<T> bytes) {
29-
list = new ArrayList<>(bytes);
30-
init();
24+
super(bytes);
3125
}
3226

33-
protected ArrayList<T> list;
34-
protected int position;
35-
protected int index;
36-
protected int length;
37-
38-
private void init() {
39-
position = 0;
40-
index = 0;
41-
length = 0;
42-
for (Bytes b : list) {
43-
b.setPosition(0);
44-
length += b.remaining();
45-
}
46-
}
47-
48-
public List<T> getWrappedChars() {
49-
return list;
50-
}
51-
52-
/** Append a new Bytes to this composite. */
53-
public void add(T bytes) {
54-
list.add(bytes);
55-
bytes.setPosition(0);
56-
length += bytes.remaining();
57-
}
58-
59-
@Override
60-
public int length() {
61-
return length;
62-
}
63-
64-
@Override
65-
public int position() {
66-
return position;
67-
}
68-
69-
@Override
70-
public void setPosition(int position) {
71-
if (position == this.position)
72-
return;
73-
if (position < this.position) {
74-
int toMove = this.position - position;
75-
if (index == list.size()) index--;
76-
do {
77-
T bytes = list.get(index);
78-
int p = bytes.position();
79-
if (toMove <= p) {
80-
bytes.setPosition(p - toMove);
81-
this.position = position;
82-
return;
83-
}
84-
toMove -= p;
85-
index--;
86-
bytes.setPosition(0);
87-
} while (true);
88-
}
89-
int toMove = position - this.position;
90-
do {
91-
T bytes = list.get(index);
92-
int r = bytes.remaining();
93-
if (toMove < r) {
94-
bytes.moveForward(toMove);
95-
this.position = position;
96-
return;
97-
}
98-
bytes.moveForward(r);
99-
toMove -= r;
100-
index++;
101-
} while (toMove > 0);
102-
this.position = position;
103-
}
104-
105-
@Override
106-
public int remaining() {
107-
return length - position;
108-
}
109-
110-
@Override
111-
public boolean hasRemaining() {
112-
return position < length;
113-
}
114-
115-
/** Composite Chars.Readable. */
27+
/** Composite Bytes.Readable. */
11628
public static class Readable extends CompositeBytes<Bytes.Readable> implements Bytes.Readable {
11729

11830
/** Constructor. */

0 commit comments

Comments
 (0)