Skip to content

Commit 0472707

Browse files
committed
fix for binary-safety (don't use strings, use byte arrays)
1 parent 98cbfd1 commit 0472707

2 files changed

Lines changed: 154 additions & 134 deletions

File tree

src/nl/melp/redis/Redis.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,13 @@ static class Encoder {
3636
}
3737

3838
/**
39-
* Write a string in the "RESP Bulk String" format.
39+
* Write a byte array in the "RESP Bulk String" format.
4040
*
41-
* @param str The string to write.
41+
* @param value The byte array to write.
4242
* @throws IOException Propagated from the output stream.
4343
* @link https://redis.io/topics/protocol#resp-bulk-strings
4444
*/
45-
void write(String str) throws IOException {
46-
byte[] value = str.getBytes();
45+
void write(byte[] value) throws IOException {
4746
out.write('$');
4847
out.write(Long.toString(value.length).getBytes());
4948
out.write(CRLF);
@@ -78,8 +77,10 @@ void write(List<Object> list) throws IOException, IllegalArgumentException {
7877
out.write(CRLF);
7978

8079
for (Object o : list) {
81-
if (o instanceof String) {
82-
write((String) o);
80+
if (o instanceof byte[]) {
81+
write((byte[])o);
82+
} else if (o instanceof String) {
83+
write(((String) o).getBytes());
8384
} else if (o instanceof Long) {
8485
write((Long) o);
8586
} else if (o instanceof Integer) {
@@ -151,7 +152,7 @@ Object parse() throws IOException, ProtocolException {
151152
ret = this.parseSimpleString();
152153
break;
153154
case '-':
154-
throw new ServerError(this.parseSimpleString());
155+
throw new ServerError(new String(this.parseSimpleString()));
155156
case ':':
156157
ret = this.parseNumber();
157158
break;
@@ -185,7 +186,7 @@ Object parse() throws IOException, ProtocolException {
185186
* @return The parsed response
186187
* @throws IOException Propagated from underlying stream.
187188
*/
188-
private String parseBulkString() throws IOException, ProtocolException {
189+
private byte[] parseBulkString() throws IOException, ProtocolException {
189190
final long expectedLength = parseNumber();
190191
if (expectedLength == -1) {
191192
return null;
@@ -206,7 +207,7 @@ private String parseBulkString() throws IOException, ProtocolException {
206207
throw new ProtocolException("Expected LF");
207208
}
208209

209-
return new String(buffer);
210+
return buffer;
210211
}
211212

212213
/**
@@ -215,8 +216,8 @@ private String parseBulkString() throws IOException, ProtocolException {
215216
* @return Resultant string
216217
* @throws IOException Propagated from underlying stream.
217218
*/
218-
private String parseSimpleString() throws IOException {
219-
return new String(scanCr(1024));
219+
private byte[] parseSimpleString() throws IOException {
220+
return scanCr(1024);
220221
}
221222

222223
private long parseNumber() throws IOException {
@@ -298,7 +299,7 @@ public Redis(InputStream inputStream, OutputStream outputStream) {
298299
*
299300
* @throws IOException All protocol and io errors are IO exceptions.
300301
*/
301-
public <T> T call(String... args) throws IOException {
302+
public <T> T call(Object... args) throws IOException {
302303
writer.write(Arrays.asList((Object[]) args));
303304
writer.flush();
304305
return (T) reader.parse();

0 commit comments

Comments
 (0)