Skip to content

Commit bedbb6d

Browse files
committed
Refactor tostring
1 parent 0ccbe23 commit bedbb6d

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/main/java/at/favre/lib/bytes/Bytes.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,6 @@ public double toDouble() {
13491349
return resize(8).internalBuffer().getDouble();
13501350
}
13511351

1352-
13531352
/**
13541353
* Compares this bytes instance to another.
13551354
* <p>
@@ -1415,16 +1414,7 @@ public int hashCode() {
14151414
*/
14161415
@Override
14171416
public String toString() {
1418-
String preview;
1419-
if (isEmpty()) {
1420-
preview = "";
1421-
} else if (length() > 8) {
1422-
preview = "(0x" + copy(0, 4).encodeHex() + "..." + copy(length() - 4, 4).encodeHex() + ")";
1423-
} else {
1424-
preview = "(0x" + encodeHex() + ")";
1425-
}
1426-
1427-
return length() + " bytes " + preview;
1417+
return Util.toString(this);
14281418
}
14291419

14301420
@Override

src/main/java/at/favre/lib/bytes/Util.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,25 @@ static byte[] readFromFile(File file) {
325325
}
326326
}
327327

328+
/**
329+
* Shows the length and a preview of max 8 bytes of the given byte
330+
*
331+
* @param bytes to convert to string
332+
* @return string representation
333+
*/
334+
static String toString(Bytes bytes) {
335+
String preview;
336+
if (bytes.isEmpty()) {
337+
preview = "";
338+
} else if (bytes.length() > 8) {
339+
preview = "(0x" + bytes.copy(0, 4).encodeHex() + "..." + bytes.copy(bytes.length() - 4, 4).encodeHex() + ")";
340+
} else {
341+
preview = "(0x" + bytes.encodeHex() + ")";
342+
}
343+
344+
return bytes.length() + " bytes " + preview;
345+
}
346+
328347
/*
329348
=================================================================================================
330349
Copyright 2011 Twitter, Inc.

0 commit comments

Comments
 (0)