Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
badef1fc
Commit
badef1fc
authored
Apr 02, 2015
by
Tamir Duberstein
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move `UTF_8` to `Internal`
parent
2820e86a
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
33 additions
and
34 deletions
+33
-34
ByteString.java
java/src/main/java/com/google/protobuf/ByteString.java
+2
-4
CodedInputStream.java
java/src/main/java/com/google/protobuf/CodedInputStream.java
+3
-3
CodedOutputStream.java
.../src/main/java/com/google/protobuf/CodedOutputStream.java
+2
-2
Internal.java
java/src/main/java/com/google/protobuf/Internal.java
+5
-4
Utf8.java
java/src/main/java/com/google/protobuf/Utf8.java
+1
-1
BoundedByteStringTest.java
.../test/java/com/google/protobuf/BoundedByteStringTest.java
+3
-3
ByteStringTest.java
java/src/test/java/com/google/protobuf/ByteStringTest.java
+2
-2
CodedOutputStreamTest.java
.../test/java/com/google/protobuf/CodedOutputStreamTest.java
+2
-2
IsValidUtf8TestUtil.java
...rc/test/java/com/google/protobuf/IsValidUtf8TestUtil.java
+4
-4
LiteralByteStringTest.java
.../test/java/com/google/protobuf/LiteralByteStringTest.java
+4
-4
RopeByteStringSubstringTest.java
...java/com/google/protobuf/RopeByteStringSubstringTest.java
+1
-1
RopeByteStringTest.java
...src/test/java/com/google/protobuf/RopeByteStringTest.java
+2
-2
TestUtil.java
java/src/test/java/com/google/protobuf/TestUtil.java
+1
-1
UnknownFieldSetLiteTest.java
...est/java/com/google/protobuf/UnknownFieldSetLiteTest.java
+1
-1
No files found.
java/src/main/java/com/google/protobuf/ByteString.java
View file @
badef1fc
...
...
@@ -78,8 +78,6 @@ public abstract class ByteString implements Iterable<Byte>, Serializable {
static
final
int
MIN_READ_FROM_CHUNK_SIZE
=
0x100
;
// 256b
static
final
int
MAX_READ_FROM_CHUNK_SIZE
=
0x2000
;
// 8k
protected
static
final
Charset
UTF_8
=
Charset
.
forName
(
"UTF-8"
);
/**
* Empty {@code ByteString}.
*/
...
...
@@ -282,7 +280,7 @@ public abstract class ByteString implements Iterable<Byte>, Serializable {
* @return new {@code ByteString}
*/
public
static
ByteString
copyFromUtf8
(
String
text
)
{
return
new
LiteralByteString
(
text
.
getBytes
(
UTF_8
));
return
new
LiteralByteString
(
text
.
getBytes
(
Internal
.
UTF_8
));
}
// =================================================================
...
...
@@ -661,7 +659,7 @@ public abstract class ByteString implements Iterable<Byte>, Serializable {
* @return new string using UTF-8 encoding
*/
public
String
toStringUtf8
()
{
return
toString
(
UTF_8
);
return
toString
(
Internal
.
UTF_8
);
}
/**
...
...
java/src/main/java/com/google/protobuf/CodedInputStream.java
View file @
badef1fc
...
...
@@ -373,14 +373,14 @@ public final class CodedInputStream {
if
(
size
<=
(
bufferSize
-
bufferPos
)
&&
size
>
0
)
{
// Fast path: We already have the bytes in a contiguous buffer, so
// just copy directly from it.
final
String
result
=
new
String
(
buffer
,
bufferPos
,
size
,
ByteString
.
UTF_8
);
final
String
result
=
new
String
(
buffer
,
bufferPos
,
size
,
Internal
.
UTF_8
);
bufferPos
+=
size
;
return
result
;
}
else
if
(
size
==
0
)
{
return
""
;
}
else
{
// Slow path: Build a byte array first then copy it.
return
new
String
(
readRawBytesSlowPath
(
size
),
ByteString
.
UTF_8
);
return
new
String
(
readRawBytesSlowPath
(
size
),
Internal
.
UTF_8
);
}
}
...
...
@@ -409,7 +409,7 @@ public final class CodedInputStream {
if
(!
Utf8
.
isValidUtf8
(
bytes
,
pos
,
pos
+
size
))
{
throw
InvalidProtocolBufferException
.
invalidUtf8
();
}
return
new
String
(
bytes
,
pos
,
size
,
ByteString
.
UTF_8
);
return
new
String
(
bytes
,
pos
,
size
,
Internal
.
UTF_8
);
}
/** Read a {@code group} field value from the stream. */
...
...
java/src/main/java/com/google/protobuf/CodedOutputStream.java
View file @
badef1fc
...
...
@@ -420,7 +420,7 @@ public final class CodedOutputStream {
// Unfortunately there does not appear to be any way to tell Java to encode
// UTF-8 directly into our buffer, so we have to let it create its own byte
// array and then copy.
final
byte
[]
bytes
=
value
.
getBytes
(
ByteString
.
UTF_8
);
final
byte
[]
bytes
=
value
.
getBytes
(
Internal
.
UTF_8
);
writeRawVarint32
(
bytes
.
length
);
writeRawBytes
(
bytes
);
}
...
...
@@ -827,7 +827,7 @@ public final class CodedOutputStream {
* {@code string} field.
*/
public
static
int
computeStringSizeNoTag
(
final
String
value
)
{
final
byte
[]
bytes
=
value
.
getBytes
(
ByteString
.
UTF_8
);
final
byte
[]
bytes
=
value
.
getBytes
(
Internal
.
UTF_8
);
return
computeRawVarint32Size
(
bytes
.
length
)
+
bytes
.
length
;
}
...
...
java/src/main/java/com/google/protobuf/Internal.java
View file @
badef1fc
...
...
@@ -53,6 +53,7 @@ import java.util.Set;
*/
public
class
Internal
{
protected
static
final
Charset
UTF_8
=
Charset
.
forName
(
"UTF-8"
);
protected
static
final
Charset
ISO_8859_1
=
Charset
.
forName
(
"ISO-8859-1"
);
/**
...
...
@@ -84,7 +85,7 @@ public class Internal {
* generated code calls this automatically.
*/
public
static
String
stringDefaultValue
(
String
bytes
)
{
return
new
String
(
bytes
.
getBytes
(
ISO_8859_1
),
ByteString
.
UTF_8
);
return
new
String
(
bytes
.
getBytes
(
ISO_8859_1
),
UTF_8
);
}
/**
...
...
@@ -144,7 +145,7 @@ public class Internal {
* without loss. More precisely, returns {@code true} whenever:
* <pre> {@code
* Arrays.equals(byteString.toByteArray(),
* new String(byteString.toByteArray(),
ByteString.UTF_8).getBytes(ByteString.UTF_8
))
* new String(byteString.toByteArray(),
"UTF-8").getBytes("UTF-8"
))
* }</pre>
*
* <p>This method rejects "overlong" byte sequences, as well as
...
...
@@ -180,14 +181,14 @@ public class Internal {
* Helper method to get the UTF-8 bytes of a string.
*/
public
static
byte
[]
toByteArray
(
String
value
)
{
return
value
.
getBytes
(
ByteString
.
UTF_8
);
return
value
.
getBytes
(
UTF_8
);
}
/**
* Helper method to convert a byte array to a string using UTF-8 encoding.
*/
public
static
String
toStringUtf8
(
byte
[]
bytes
)
{
return
new
String
(
bytes
,
ByteString
.
UTF_8
);
return
new
String
(
bytes
,
UTF_8
);
}
/**
...
...
java/src/main/java/com/google/protobuf/Utf8.java
View file @
badef1fc
...
...
@@ -46,7 +46,7 @@ package com.google.protobuf;
* <p>The byte sequences considered valid by this class are exactly
* those that can be roundtrip converted to Strings and back to bytes
* using the UTF-8 charset, without loss: <pre> {@code
* Arrays.equals(bytes, new String(bytes,
ByteString.UTF_8).getBytes(ByteString
.UTF_8))
* Arrays.equals(bytes, new String(bytes,
Internal.UTF_8).getBytes(Internal
.UTF_8))
* }</pre>
*
* <p>See the Unicode Standard,</br>
...
...
java/src/test/java/com/google/protobuf/BoundedByteStringTest.java
View file @
badef1fc
...
...
@@ -62,7 +62,7 @@ public class BoundedByteStringTest extends LiteralByteStringTest {
@Override
public
void
testToString
()
throws
UnsupportedEncodingException
{
String
testString
=
"I love unicode \u1234\u5678 characters"
;
LiteralByteString
unicode
=
new
LiteralByteString
(
testString
.
getBytes
(
ByteString
.
UTF_8
));
LiteralByteString
unicode
=
new
LiteralByteString
(
testString
.
getBytes
(
Internal
.
UTF_8
));
ByteString
chopped
=
unicode
.
substring
(
2
,
unicode
.
size
()
-
6
);
assertEquals
(
classUnderTest
+
".substring() must have the expected type"
,
classUnderTest
,
getActualClassName
(
chopped
));
...
...
@@ -75,12 +75,12 @@ public class BoundedByteStringTest extends LiteralByteStringTest {
@Override
public
void
testCharsetToString
()
throws
UnsupportedEncodingException
{
String
testString
=
"I love unicode \u1234\u5678 characters"
;
LiteralByteString
unicode
=
new
LiteralByteString
(
testString
.
getBytes
(
ByteString
.
UTF_8
));
LiteralByteString
unicode
=
new
LiteralByteString
(
testString
.
getBytes
(
Internal
.
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
(
ByteString
.
UTF_8
);
String
roundTripString
=
chopped
.
toString
(
Internal
.
UTF_8
);
assertEquals
(
classUnderTest
+
" unicode bytes must match"
,
testString
.
substring
(
2
,
testString
.
length
()
-
6
),
roundTripString
);
}
...
...
java/src/test/java/com/google/protobuf/ByteStringTest.java
View file @
badef1fc
...
...
@@ -140,7 +140,7 @@ public class ByteStringTest extends TestCase {
public
void
testCopyFrom_Utf8
()
throws
UnsupportedEncodingException
{
String
testString
=
"I love unicode \u1234\u5678 characters"
;
ByteString
byteString
=
ByteString
.
copyFromUtf8
(
testString
);
byte
[]
testBytes
=
testString
.
getBytes
(
ByteString
.
UTF_8
);
byte
[]
testBytes
=
testString
.
getBytes
(
Internal
.
UTF_8
);
assertTrue
(
"copyFromUtf8 string must respect the charset"
,
isArrayRange
(
byteString
.
toByteArray
(),
testBytes
,
0
,
testBytes
.
length
));
}
...
...
@@ -401,7 +401,7 @@ public class ByteStringTest extends TestCase {
public
void
testToStringUtf8
()
throws
UnsupportedEncodingException
{
String
testString
=
"I love unicode \u1234\u5678 characters"
;
byte
[]
testBytes
=
testString
.
getBytes
(
ByteString
.
UTF_8
);
byte
[]
testBytes
=
testString
.
getBytes
(
Internal
.
UTF_8
);
ByteString
byteString
=
ByteString
.
copyFrom
(
testBytes
);
assertEquals
(
"copyToStringUtf8 must respect the charset"
,
testString
,
byteString
.
toStringUtf8
());
...
...
java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java
View file @
badef1fc
...
...
@@ -321,7 +321,7 @@ public class CodedOutputStreamTest extends TestCase {
final
int
BUFFER_SIZE
=
4
*
1024
;
ByteArrayOutputStream
outputStream
=
new
ByteArrayOutputStream
(
BUFFER_SIZE
);
CodedOutputStream
codedStream
=
CodedOutputStream
.
newInstance
(
outputStream
);
byte
[]
value
=
"abcde"
.
getBytes
(
ByteString
.
UTF_8
);
byte
[]
value
=
"abcde"
.
getBytes
(
Internal
.
UTF_8
);
for
(
int
i
=
0
;
i
<
1024
;
++
i
)
{
codedStream
.
writeRawBytes
(
value
,
0
,
value
.
length
);
}
...
...
@@ -367,7 +367,7 @@ public class CodedOutputStreamTest extends TestCase {
}
public
void
testWriteByteBuffer
()
throws
Exception
{
byte
[]
value
=
"abcde"
.
getBytes
(
ByteString
.
UTF_8
);
byte
[]
value
=
"abcde"
.
getBytes
(
Internal
.
UTF_8
);
ByteArrayOutputStream
outputStream
=
new
ByteArrayOutputStream
();
CodedOutputStream
codedStream
=
CodedOutputStream
.
newInstance
(
outputStream
);
ByteBuffer
byteBuffer
=
ByteBuffer
.
wrap
(
value
,
0
,
1
);
...
...
java/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java
View file @
badef1fc
...
...
@@ -220,8 +220,8 @@ class IsValidUtf8TestUtil {
}
ByteString
bs
=
ByteString
.
copyFrom
(
bytes
);
boolean
isRoundTrippable
=
bs
.
isValidUtf8
();
String
s
=
new
String
(
bytes
,
ByteString
.
UTF_8
);
byte
[]
bytesReencoded
=
s
.
getBytes
(
ByteString
.
UTF_8
);
String
s
=
new
String
(
bytes
,
Internal
.
UTF_8
);
byte
[]
bytesReencoded
=
s
.
getBytes
(
Internal
.
UTF_8
);
boolean
bytesEqual
=
Arrays
.
equals
(
bytes
,
bytesReencoded
);
if
(
bytesEqual
!=
isRoundTrippable
)
{
...
...
@@ -313,10 +313,10 @@ class IsValidUtf8TestUtil {
void
testBytesUsingByteBuffers
(
int
numBytes
,
long
expectedCount
,
long
start
,
long
lim
)
throws
UnsupportedEncodingException
{
CharsetDecoder
decoder
=
ByteString
.
UTF_8
.
newDecoder
()
CharsetDecoder
decoder
=
Internal
.
UTF_8
.
newDecoder
()
.
onMalformedInput
(
CodingErrorAction
.
REPLACE
)
.
onUnmappableCharacter
(
CodingErrorAction
.
REPLACE
);
CharsetEncoder
encoder
=
ByteString
.
UTF_8
.
newEncoder
()
CharsetEncoder
encoder
=
Internal
.
UTF_8
.
newEncoder
()
.
onMalformedInput
(
CodingErrorAction
.
REPLACE
)
.
onUnmappableCharacter
(
CodingErrorAction
.
REPLACE
);
byte
[]
bytes
=
new
byte
[
numBytes
];
...
...
java/src/test/java/com/google/protobuf/LiteralByteStringTest.java
View file @
badef1fc
...
...
@@ -293,21 +293,21 @@ public class LiteralByteStringTest extends TestCase {
public
void
testToString
()
throws
UnsupportedEncodingException
{
String
testString
=
"I love unicode \u1234\u5678 characters"
;
LiteralByteString
unicode
=
new
LiteralByteString
(
testString
.
getBytes
(
ByteString
.
UTF_8
));
LiteralByteString
unicode
=
new
LiteralByteString
(
testString
.
getBytes
(
Internal
.
UTF_8
));
String
roundTripString
=
unicode
.
toString
(
UTF_8
);
assertEquals
(
classUnderTest
+
" unicode must match"
,
testString
,
roundTripString
);
}
public
void
testCharsetToString
()
throws
UnsupportedEncodingException
{
String
testString
=
"I love unicode \u1234\u5678 characters"
;
LiteralByteString
unicode
=
new
LiteralByteString
(
testString
.
getBytes
(
ByteString
.
UTF_8
));
String
roundTripString
=
unicode
.
toString
(
ByteString
.
UTF_8
);
LiteralByteString
unicode
=
new
LiteralByteString
(
testString
.
getBytes
(
Internal
.
UTF_8
));
String
roundTripString
=
unicode
.
toString
(
Internal
.
UTF_8
);
assertEquals
(
classUnderTest
+
" unicode must match"
,
testString
,
roundTripString
);
}
public
void
testToString_returnsCanonicalEmptyString
()
throws
UnsupportedEncodingException
{
assertSame
(
classUnderTest
+
" must be the same string references"
,
ByteString
.
EMPTY
.
toString
(
ByteString
.
UTF_8
),
new
LiteralByteString
(
new
byte
[]{}).
toString
(
ByteString
.
UTF_8
));
ByteString
.
EMPTY
.
toString
(
Internal
.
UTF_8
),
new
LiteralByteString
(
new
byte
[]{}).
toString
(
Internal
.
UTF_8
));
}
public
void
testToString_raisesException
()
throws
UnsupportedEncodingException
{
...
...
java/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java
View file @
badef1fc
...
...
@@ -116,7 +116,7 @@ public class RopeByteStringSubstringTest extends LiteralByteStringTest {
assertEquals
(
classUnderTest
+
" from string must have the expected type"
,
classUnderTest
,
getActualClassName
(
unicode
));
String
roundTripString
=
unicode
.
toString
(
ByteString
.
UTF_8
);
String
roundTripString
=
unicode
.
toString
(
Internal
.
UTF_8
);
assertEquals
(
classUnderTest
+
" unicode bytes must match"
,
testString
,
roundTripString
);
ByteString
flatString
=
ByteString
.
copyFromUtf8
(
testString
);
...
...
java/src/test/java/com/google/protobuf/RopeByteStringTest.java
View file @
badef1fc
...
...
@@ -135,7 +135,7 @@ public class RopeByteStringTest extends LiteralByteStringTest {
assertEquals
(
classUnderTest
+
" from string must have the expected type"
,
classUnderTest
,
getActualClassName
(
unicode
));
String
roundTripString
=
unicode
.
toString
(
ByteString
.
UTF_8
);
String
roundTripString
=
unicode
.
toString
(
Internal
.
UTF_8
);
assertEquals
(
classUnderTest
+
" unicode bytes must match"
,
testString
,
roundTripString
);
ByteString
flatString
=
ByteString
.
copyFromUtf8
(
testString
);
...
...
@@ -149,7 +149,7 @@ public class RopeByteStringTest extends LiteralByteStringTest {
RopeByteString
ropeByteString
=
RopeByteString
.
newInstanceForTest
(
ByteString
.
EMPTY
,
ByteString
.
EMPTY
);
assertSame
(
classUnderTest
+
" must be the same string references"
,
ByteString
.
EMPTY
.
toString
(
ByteString
.
UTF_8
),
ropeByteString
.
toString
(
ByteString
.
UTF_8
));
ByteString
.
EMPTY
.
toString
(
Internal
.
UTF_8
),
ropeByteString
.
toString
(
Internal
.
UTF_8
));
}
public
void
testToString_raisesException
()
throws
UnsupportedEncodingException
{
...
...
java/src/test/java/com/google/protobuf/TestUtil.java
View file @
badef1fc
...
...
@@ -276,7 +276,7 @@ public final class TestUtil {
/** Helper to convert a String to ByteString. */
static
ByteString
toBytes
(
String
str
)
{
return
ByteString
.
copyFrom
(
str
.
getBytes
(
ByteString
.
UTF_8
));
return
ByteString
.
copyFrom
(
str
.
getBytes
(
Internal
.
UTF_8
));
}
/**
...
...
java/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java
View file @
badef1fc
...
...
@@ -229,7 +229,7 @@ public class UnknownFieldSetLiteTest extends TestCase {
public
void
testMalformedBytes
()
throws
Exception
{
try
{
Foo
.
parseFrom
(
"this is a malformed protocol buffer"
.
getBytes
(
ByteString
.
UTF_8
));
Foo
.
parseFrom
(
"this is a malformed protocol buffer"
.
getBytes
(
Internal
.
UTF_8
));
fail
();
}
catch
(
InvalidProtocolBufferException
e
)
{
// Expected.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment