Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions java/src/main/java/com/google/protobuf/ByteString.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
Expand All @@ -59,6 +60,8 @@
*/
public abstract class ByteString implements Iterable<Byte> {

static final Charset UTF_8 = Charset.forName("UTF-8");

/**
* When two strings to be concatenated have a combined length shorter than
* this, we just copy their bytes on {@link #concat(ByteString)}.
Expand Down Expand Up @@ -611,6 +614,15 @@ abstract void writeToInternal(OutputStream out, int sourceOffset,
public abstract String toString(String charsetName)
throws UnsupportedEncodingException;

/**
* Constructs a new {@code String} by decoding the bytes using the
* specified charset.
*
* @param charset encode using this charset
* @return new string
*/
public abstract String toString(Charset charset);

// =================================================================
// UTF-8 decoding

Expand All @@ -620,11 +632,7 @@ public abstract String toString(String charsetName)
* @return new string using UTF-8 encoding
*/
public String toStringUtf8() {
try {
return toString("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UTF-8 not supported?", e);
}
return toString(UTF_8);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions java/src/main/java/com/google/protobuf/LiteralByteString.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -155,6 +156,11 @@ public String toString(String charsetName)
return new String(bytes, getOffsetIntoBytes(), size(), charsetName);
}

@Override
public String toString(Charset charset) {
return new String(bytes, getOffsetIntoBytes(), size(), charset);
}

// =================================================================
// UTF-8 decoding

Expand Down
6 changes: 6 additions & 0 deletions java/src/main/java/com/google/protobuf/RopeByteString.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.io.UnsupportedEncodingException;
import java.io.ByteArrayInputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
Expand Down Expand Up @@ -421,6 +422,11 @@ public String toString(String charsetName)
return new String(toByteArray(), charsetName);
}

@Override
public String toString(Charset charset) {
return new String(toByteArray(), charset);
}

// =================================================================
// UTF-8 decoding

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ protected void setUp() throws Exception {
@Override
public void testToString() throws UnsupportedEncodingException {
String testString = "I love unicode \u1234\u5678 characters";
LiteralByteString unicode = new LiteralByteString(testString.getBytes(UTF_8));
LiteralByteString unicode = new LiteralByteString(testString.getBytes(ByteString.UTF_8));
ByteString chopped = unicode.substring(2, unicode.size() - 6);
assertEquals(classUnderTest + ".substring() must have the expected type",
classUnderTest, getActualClassName(chopped));

String roundTripString = chopped.toString(UTF_8);
String roundTripString = chopped.toString(ByteString.UTF_8);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test both ByteString.toString(String) and ByteString.toString(Charset) here (you added a test case for the latter but removed the test case for the former).

assertEquals(classUnderTest + " unicode bytes must match",
testString.substring(2, testString.length() - 6), roundTripString);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
* @author [email protected] (Carl Haverl)
*/
public class LiteralByteStringTest extends TestCase {
protected static final String UTF_8 = "UTF-8";

protected String classUnderTest;
protected byte[] referenceBytes;
Expand Down Expand Up @@ -291,8 +290,8 @@ public void testNewOutput() throws IOException {

public void testToString() throws UnsupportedEncodingException {
String testString = "I love unicode \u1234\u5678 characters";
LiteralByteString unicode = new LiteralByteString(testString.getBytes(UTF_8));
String roundTripString = unicode.toString(UTF_8);
LiteralByteString unicode = new LiteralByteString(testString.getBytes(ByteString.UTF_8));
String roundTripString = unicode.toString(ByteString.UTF_8);
assertEquals(classUnderTest + " unicode must match", testString, roundTripString);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void testToString() throws UnsupportedEncodingException {

assertEquals(classUnderTest + " from string must have the expected type",
classUnderTest, getActualClassName(unicode));
String roundTripString = unicode.toString(UTF_8);
String roundTripString = unicode.toString(ByteString.UTF_8);
assertEquals(classUnderTest + " unicode bytes must match",
testString, roundTripString);
ByteString flatString = ByteString.copyFromUtf8(testString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void testToString() throws UnsupportedEncodingException {

assertEquals(classUnderTest + " from string must have the expected type",
classUnderTest, getActualClassName(unicode));
String roundTripString = unicode.toString(UTF_8);
String roundTripString = unicode.toString(ByteString.UTF_8);
assertEquals(classUnderTest + " unicode bytes must match",
testString, roundTripString);
ByteString flatString = ByteString.copyFromUtf8(testString);
Expand Down