Skip to content

Commit f10ccf8

Browse files
committed
Refactor code
1 parent 0b79eaa commit f10ccf8

5 files changed

Lines changed: 19 additions & 17 deletions

File tree

src/main/java/com/dencode/logic/dencoder/DencodeUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected static <T, U, R> List<R> dencodeEach(List<T> vals, List<U> vals2, BiFu
7474
}
7575

7676
protected static <T> String dencodeLines(List<T> vals, Function<T, String> func) {
77-
return dencodeLines(vals, null, (val1, val2) -> func.apply(val1));
77+
return dencodeLines(vals, null, (val1, _) -> func.apply(val1));
7878
}
7979

8080
protected static <T, U> String dencodeLines(List<T> vals, List<U> vals2, BiFunction<T, U, String> func) {

src/main/java/com/dencode/logic/dencoder/StringBase32Dencoder.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
package com.dencode.logic.dencoder;
1818

19-
import java.io.ByteArrayOutputStream;
19+
2020
import java.nio.charset.Charset;
2121

2222
import com.dencode.logic.dencoder.annotation.Dencoder;
@@ -118,7 +118,8 @@ private static String decStrBase32(String val, Charset charset) {
118118
}
119119
}
120120

121-
ByteArrayOutputStream binBuf = new ByteArrayOutputStream(len);
121+
byte[] binBuf = new byte[len * 5 / 8]; // Max decoded size
122+
int binBufIdx = 0;
122123
int bitsBuf = 0;
123124
int bitCount = 0;
124125
for (int i = 0; i < len; i++) {
@@ -140,11 +141,11 @@ private static String decStrBase32(String val, Charset charset) {
140141

141142
if (8 <= bitCount) {
142143
byte b = (byte)((bitsBuf >>> (bitCount - 8)) & 0b11111111);
143-
binBuf.write(b);
144+
binBuf[binBufIdx++] = b;
144145
bitCount -= 8;
145146
}
146147
}
147148

148-
return binBuf.toString(charset);
149+
return new String(binBuf, 0, binBufIdx, charset);
149150
}
150151
}

src/main/java/com/dencode/logic/dencoder/StringBase64Dencoder.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
package com.dencode.logic.dencoder;
1818

19-
import java.io.ByteArrayOutputStream;
19+
2020
import java.nio.charset.Charset;
2121
import java.nio.charset.IllegalCharsetNameException;
2222
import java.nio.charset.UnsupportedCharsetException;
@@ -134,7 +134,8 @@ private static String decode(String val, Charset charset) {
134134
}
135135
}
136136

137-
ByteArrayOutputStream binBuf = new ByteArrayOutputStream(len);
137+
byte[] binBuf = new byte[len * 3 / 4]; // Max decoded size
138+
int binBufIdx = 0;
138139
int bitsBuf = 0;
139140
int bitCount = 0;
140141
for (int i = 0; i < len; i++) {
@@ -156,11 +157,11 @@ private static String decode(String val, Charset charset) {
156157

157158
if (8 <= bitCount) {
158159
byte b = (byte)((bitsBuf >>> (bitCount - 8)) & 0b11111111);
159-
binBuf.write(b);
160+
binBuf[binBufIdx++] = b;
160161
bitCount -= 8;
161162
}
162163
}
163164

164-
return binBuf.toString(charset);
165+
return new String(binBuf, 0, binBufIdx, charset);
165166
}
166167
}

src/main/java/com/dencode/logic/dencoder/StringHTMLEscapeDencoder.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.util.HashMap;
2020
import java.util.Map;
21-
import java.util.Objects;
2221
import java.util.stream.Collectors;
2322

2423
import com.dencode.logic.dencoder.annotation.Dencoder;
@@ -43,7 +42,7 @@ private static class Chars {
4342
public char char2() { return this.char2; }
4443
public Chars set(char char1) { return set(char1, '\0'); }
4544
public Chars set(char char1, char char2) { this.char1 =char1; this.char2 =char2; this.hash = -1; return this; }
46-
@Override public int hashCode() { if (this.hash == -1) { this.hash = Objects.hash(char1, char2); } return this.hash; }
45+
@Override public int hashCode() { if (this.hash == -1) { this.hash = (char1 << 16) ^ char2; } return this.hash; }
4746
@Override public boolean equals(Object obj) { return (char1 == ((Chars)obj).char1) && (char2 == ((Chars)obj).char2); }
4847
}
4948

@@ -2304,7 +2303,7 @@ public static void main(String[] args) throws Exception {
23042303

23052304
private static final Map<Chars, String> CHARS_NAME_MAP = NAME_CHARS_MAP.entrySet().stream()
23062305
.filter((entry) -> entry.getKey().charAt(entry.getKey().length() - 1) == ';')
2307-
.collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey, (first, second) -> first));
2306+
.collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey, (first, _) -> first));
23082307

23092308

23102309
private StringHTMLEscapeDencoder() {

src/main/java/com/dencode/logic/dencoder/StringQuotedPrintableDencoder.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
package com.dencode.logic.dencoder;
1818

19-
import java.io.ByteArrayOutputStream;
19+
2020
import java.nio.charset.Charset;
2121
import java.nio.charset.IllegalCharsetNameException;
2222
import java.nio.charset.UnsupportedCharsetException;
@@ -169,7 +169,8 @@ private static String decode(String val, Charset charset) {
169169
}
170170

171171
int len = val.length();
172-
ByteArrayOutputStream binBuf = new ByteArrayOutputStream(len);
172+
byte[] binBuf = new byte[len];
173+
int binBufIdx = 0;
173174

174175
for (int i = 0; i < len; i++) {
175176
char ch = val.charAt(i);
@@ -204,16 +205,16 @@ private static String decode(String val, Charset charset) {
204205

205206
int high = DencodeUtils.hexDigitToNum(ch1);
206207
int low = DencodeUtils.hexDigitToNum(ch2);
207-
binBuf.write((byte)((high << 4) | low));
208+
binBuf[binBufIdx++] = (byte)((high << 4) | low);
208209
} catch (IndexOutOfBoundsException | IllegalArgumentException e) {
209210
return null;
210211
}
211212
}
212213
} else {
213-
binBuf.write((byte)ch);
214+
binBuf[binBufIdx++] = (byte)ch;
214215
}
215216
}
216217

217-
return binBuf.toString(charset);
218+
return new String(binBuf, 0, binBufIdx, charset);
218219
}
219220
}

0 commit comments

Comments
 (0)