|
@@ -30,9 +30,8 @@
|
|
|
|
|
|
package com.google.protobuf;
|
|
package com.google.protobuf;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
|
-import java.io.UnsupportedEncodingException;
|
|
|
|
import java.nio.ByteBuffer;
|
|
import java.nio.ByteBuffer;
|
|
|
|
+import java.nio.charset.Charset;
|
|
import java.util.AbstractList;
|
|
import java.util.AbstractList;
|
|
import java.util.AbstractMap;
|
|
import java.util.AbstractMap;
|
|
import java.util.AbstractSet;
|
|
import java.util.AbstractSet;
|
|
@@ -51,6 +50,10 @@ import java.util.Set;
|
|
* @author kenton@google.com (Kenton Varda)
|
|
* @author kenton@google.com (Kenton Varda)
|
|
*/
|
|
*/
|
|
public class Internal {
|
|
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");
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Helper called by generated code to construct default values for string
|
|
* Helper called by generated code to construct default values for string
|
|
* fields.
|
|
* fields.
|
|
@@ -80,14 +83,7 @@ public class Internal {
|
|
* generated code calls this automatically.
|
|
* generated code calls this automatically.
|
|
*/
|
|
*/
|
|
public static String stringDefaultValue(String bytes) {
|
|
public static String stringDefaultValue(String bytes) {
|
|
- try {
|
|
|
|
- return new String(bytes.getBytes("ISO-8859-1"), "UTF-8");
|
|
|
|
- } catch (UnsupportedEncodingException e) {
|
|
|
|
- // This should never happen since all JVMs are required to implement
|
|
|
|
- // both of the above character sets.
|
|
|
|
- throw new IllegalStateException(
|
|
|
|
- "Java VM does not support a standard character set.", e);
|
|
|
|
- }
|
|
|
|
|
|
+ return new String(bytes.getBytes(ISO_8859_1), UTF_8);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -99,37 +95,23 @@ public class Internal {
|
|
* embed raw bytes as a string literal with ISO-8859-1 encoding.
|
|
* embed raw bytes as a string literal with ISO-8859-1 encoding.
|
|
*/
|
|
*/
|
|
public static ByteString bytesDefaultValue(String bytes) {
|
|
public static ByteString bytesDefaultValue(String bytes) {
|
|
- try {
|
|
|
|
- return ByteString.copyFrom(bytes.getBytes("ISO-8859-1"));
|
|
|
|
- } catch (UnsupportedEncodingException e) {
|
|
|
|
- // This should never happen since all JVMs are required to implement
|
|
|
|
- // ISO-8859-1.
|
|
|
|
- throw new IllegalStateException(
|
|
|
|
- "Java VM does not support a standard character set.", e);
|
|
|
|
- }
|
|
|
|
|
|
+ return ByteString.copyFrom(bytes.getBytes(ISO_8859_1));
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
* Helper called by generated code to construct default values for bytes
|
|
* Helper called by generated code to construct default values for bytes
|
|
* fields.
|
|
* fields.
|
|
* <p>
|
|
* <p>
|
|
- * This is like {@link #bytesDefaultValue}, but returns a byte array.
|
|
|
|
|
|
+ * This is like {@link #bytesDefaultValue}, but returns a byte array.
|
|
*/
|
|
*/
|
|
public static byte[] byteArrayDefaultValue(String bytes) {
|
|
public static byte[] byteArrayDefaultValue(String bytes) {
|
|
- try {
|
|
|
|
- return bytes.getBytes("ISO-8859-1");
|
|
|
|
- } catch (UnsupportedEncodingException e) {
|
|
|
|
- // This should never happen since all JVMs are required to implement
|
|
|
|
- // ISO-8859-1.
|
|
|
|
- throw new IllegalStateException(
|
|
|
|
- "Java VM does not support a standard character set.", e);
|
|
|
|
- }
|
|
|
|
|
|
+ return bytes.getBytes(ISO_8859_1);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* Helper called by generated code to construct default values for bytes
|
|
* Helper called by generated code to construct default values for bytes
|
|
* fields.
|
|
* fields.
|
|
* <p>
|
|
* <p>
|
|
- * This is like {@link #bytesDefaultValue}, but returns a ByteBuffer.
|
|
|
|
|
|
+ * This is like {@link #bytesDefaultValue}, but returns a ByteBuffer.
|
|
*/
|
|
*/
|
|
public static ByteBuffer byteBufferDefaultValue(String bytes) {
|
|
public static ByteBuffer byteBufferDefaultValue(String bytes) {
|
|
return ByteBuffer.wrap(byteArrayDefaultValue(bytes));
|
|
return ByteBuffer.wrap(byteArrayDefaultValue(bytes));
|
|
@@ -185,7 +167,7 @@ public class Internal {
|
|
public static boolean isValidUtf8(ByteString byteString) {
|
|
public static boolean isValidUtf8(ByteString byteString) {
|
|
return byteString.isValidUtf8();
|
|
return byteString.isValidUtf8();
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Like {@link #isValidUtf8(ByteString)} but for byte arrays.
|
|
* Like {@link #isValidUtf8(ByteString)} but for byte arrays.
|
|
*/
|
|
*/
|
|
@@ -197,22 +179,14 @@ public class Internal {
|
|
* Helper method to get the UTF-8 bytes of a string.
|
|
* Helper method to get the UTF-8 bytes of a string.
|
|
*/
|
|
*/
|
|
public static byte[] toByteArray(String value) {
|
|
public static byte[] toByteArray(String value) {
|
|
- try {
|
|
|
|
- return value.getBytes("UTF-8");
|
|
|
|
- } catch (UnsupportedEncodingException e) {
|
|
|
|
- throw new RuntimeException("UTF-8 not supported?", e);
|
|
|
|
- }
|
|
|
|
|
|
+ return value.getBytes(UTF_8);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Helper method to convert a byte array to a string using UTF-8 encoding.
|
|
* Helper method to convert a byte array to a string using UTF-8 encoding.
|
|
*/
|
|
*/
|
|
public static String toStringUtf8(byte[] bytes) {
|
|
public static String toStringUtf8(byte[] bytes) {
|
|
- try {
|
|
|
|
- return new String(bytes, "UTF-8");
|
|
|
|
- } catch (UnsupportedEncodingException e) {
|
|
|
|
- throw new RuntimeException("UTF-8 not supported?", e);
|
|
|
|
- }
|
|
|
|
|
|
+ return new String(bytes, UTF_8);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -274,7 +248,7 @@ public class Internal {
|
|
}
|
|
}
|
|
return hash;
|
|
return hash;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Helper method for implementing {@link Message#equals(Object)} for bytes field.
|
|
* Helper method for implementing {@link Message#equals(Object)} for bytes field.
|
|
*/
|
|
*/
|
|
@@ -298,7 +272,7 @@ public class Internal {
|
|
}
|
|
}
|
|
return hash;
|
|
return hash;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Helper method for implementing {@link Message#hashCode()} for bytes field.
|
|
* Helper method for implementing {@link Message#hashCode()} for bytes field.
|
|
*/
|
|
*/
|
|
@@ -309,7 +283,7 @@ public class Internal {
|
|
// based hashCode() method.
|
|
// based hashCode() method.
|
|
return LiteralByteString.hashCode(bytes);
|
|
return LiteralByteString.hashCode(bytes);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Helper method for implementing {@link Message#equals(Object)} for bytes
|
|
* Helper method for implementing {@link Message#equals(Object)} for bytes
|
|
* field.
|
|
* field.
|
|
@@ -322,7 +296,7 @@ public class Internal {
|
|
// compare all the content.
|
|
// compare all the content.
|
|
return a.duplicate().clear().equals(b.duplicate().clear());
|
|
return a.duplicate().clear().equals(b.duplicate().clear());
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Helper method for implementing {@link Message#equals(Object)} for bytes
|
|
* Helper method for implementing {@link Message#equals(Object)} for bytes
|
|
* field.
|
|
* field.
|
|
@@ -351,9 +325,9 @@ public class Internal {
|
|
}
|
|
}
|
|
return hash;
|
|
return hash;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
private static final int DEFAULT_BUFFER_SIZE = 4096;
|
|
private static final int DEFAULT_BUFFER_SIZE = 4096;
|
|
-
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Helper method for implementing {@link Message#hashCode()} for bytes
|
|
* Helper method for implementing {@link Message#hashCode()} for bytes
|
|
* field.
|
|
* field.
|
|
@@ -382,18 +356,18 @@ public class Internal {
|
|
return h == 0 ? 1 : h;
|
|
return h == 0 ? 1 : h;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* An empty byte array constant used in generated code.
|
|
* An empty byte array constant used in generated code.
|
|
*/
|
|
*/
|
|
public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
|
|
public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
|
|
-
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* An empty byte array constant used in generated code.
|
|
* An empty byte array constant used in generated code.
|
|
*/
|
|
*/
|
|
public static final ByteBuffer EMPTY_BYTE_BUFFER =
|
|
public static final ByteBuffer EMPTY_BYTE_BUFFER =
|
|
ByteBuffer.wrap(EMPTY_BYTE_ARRAY);
|
|
ByteBuffer.wrap(EMPTY_BYTE_ARRAY);
|
|
-
|
|
|
|
|
|
+
|
|
/** An empty coded input stream constant used in generated code. */
|
|
/** An empty coded input stream constant used in generated code. */
|
|
public static final CodedInputStream EMPTY_CODED_INPUT_STREAM =
|
|
public static final CodedInputStream EMPTY_CODED_INPUT_STREAM =
|
|
CodedInputStream.newInstance(EMPTY_BYTE_ARRAY);
|
|
CodedInputStream.newInstance(EMPTY_BYTE_ARRAY);
|
|
@@ -458,13 +432,13 @@ public class Internal {
|
|
|
|
|
|
private final Map<K, RealValue> realMap;
|
|
private final Map<K, RealValue> realMap;
|
|
private final Converter<RealValue, V> valueConverter;
|
|
private final Converter<RealValue, V> valueConverter;
|
|
-
|
|
|
|
|
|
+
|
|
public MapAdapter(Map<K, RealValue> realMap,
|
|
public MapAdapter(Map<K, RealValue> realMap,
|
|
Converter<RealValue, V> valueConverter) {
|
|
Converter<RealValue, V> valueConverter) {
|
|
this.realMap = realMap;
|
|
this.realMap = realMap;
|
|
this.valueConverter = valueConverter;
|
|
this.valueConverter = valueConverter;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
@SuppressWarnings("unchecked")
|
|
@SuppressWarnings("unchecked")
|
|
@Override
|
|
@Override
|
|
public V get(Object key) {
|
|
public V get(Object key) {
|
|
@@ -474,7 +448,7 @@ public class Internal {
|
|
}
|
|
}
|
|
return valueConverter.doForward(result);
|
|
return valueConverter.doForward(result);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public V put(K key, V value) {
|
|
public V put(K key, V value) {
|
|
RealValue oldValue = realMap.put(key, valueConverter.doBackward(value));
|
|
RealValue oldValue = realMap.put(key, valueConverter.doBackward(value));
|
|
@@ -488,13 +462,13 @@ public class Internal {
|
|
public Set<java.util.Map.Entry<K, V>> entrySet() {
|
|
public Set<java.util.Map.Entry<K, V>> entrySet() {
|
|
return new SetAdapter(realMap.entrySet());
|
|
return new SetAdapter(realMap.entrySet());
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
private class SetAdapter extends AbstractSet<Map.Entry<K, V>> {
|
|
private class SetAdapter extends AbstractSet<Map.Entry<K, V>> {
|
|
private final Set<Map.Entry<K, RealValue>> realSet;
|
|
private final Set<Map.Entry<K, RealValue>> realSet;
|
|
public SetAdapter(Set<Map.Entry<K, RealValue>> realSet) {
|
|
public SetAdapter(Set<Map.Entry<K, RealValue>> realSet) {
|
|
this.realSet = realSet;
|
|
this.realSet = realSet;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public Iterator<java.util.Map.Entry<K, V>> iterator() {
|
|
public Iterator<java.util.Map.Entry<K, V>> iterator() {
|
|
return new IteratorAdapter(realSet.iterator());
|
|
return new IteratorAdapter(realSet.iterator());
|
|
@@ -503,17 +477,17 @@ public class Internal {
|
|
@Override
|
|
@Override
|
|
public int size() {
|
|
public int size() {
|
|
return realSet.size();
|
|
return realSet.size();
|
|
- }
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
private class IteratorAdapter implements Iterator<Map.Entry<K, V>> {
|
|
private class IteratorAdapter implements Iterator<Map.Entry<K, V>> {
|
|
private final Iterator<Map.Entry<K, RealValue>> realIterator;
|
|
private final Iterator<Map.Entry<K, RealValue>> realIterator;
|
|
-
|
|
|
|
|
|
+
|
|
public IteratorAdapter(
|
|
public IteratorAdapter(
|
|
Iterator<Map.Entry<K, RealValue>> realIterator) {
|
|
Iterator<Map.Entry<K, RealValue>> realIterator) {
|
|
this.realIterator = realIterator;
|
|
this.realIterator = realIterator;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public boolean hasNext() {
|
|
public boolean hasNext() {
|
|
return realIterator.hasNext();
|
|
return realIterator.hasNext();
|
|
@@ -529,14 +503,14 @@ public class Internal {
|
|
realIterator.remove();
|
|
realIterator.remove();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
private class EntryAdapter implements Map.Entry<K, V> {
|
|
private class EntryAdapter implements Map.Entry<K, V> {
|
|
private final Map.Entry<K, RealValue> realEntry;
|
|
private final Map.Entry<K, RealValue> realEntry;
|
|
-
|
|
|
|
|
|
+
|
|
public EntryAdapter(Map.Entry<K, RealValue> realEntry) {
|
|
public EntryAdapter(Map.Entry<K, RealValue> realEntry) {
|
|
this.realEntry = realEntry;
|
|
this.realEntry = realEntry;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public K getKey() {
|
|
public K getKey() {
|
|
return realEntry.getKey();
|
|
return realEntry.getKey();
|