|
|
|
@ -30,9 +30,8 @@ |
|
|
|
|
|
|
|
|
|
package com.google.protobuf; |
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
|
import java.nio.ByteBuffer; |
|
|
|
|
import java.nio.charset.Charset; |
|
|
|
|
import java.util.AbstractList; |
|
|
|
|
import java.util.AbstractMap; |
|
|
|
|
import java.util.AbstractSet; |
|
|
|
@ -51,6 +50,10 @@ import java.util.Set; |
|
|
|
|
* @author kenton@google.com (Kenton Varda) |
|
|
|
|
*/ |
|
|
|
|
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 |
|
|
|
|
* fields. |
|
|
|
@ -80,14 +83,7 @@ public class Internal { |
|
|
|
|
* generated code calls this automatically. |
|
|
|
|
*/ |
|
|
|
|
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. |
|
|
|
|
*/ |
|
|
|
|
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 |
|
|
|
|
* fields. |
|
|
|
|
* <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) { |
|
|
|
|
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 |
|
|
|
|
* fields. |
|
|
|
|
* <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) { |
|
|
|
|
return ByteBuffer.wrap(byteArrayDefaultValue(bytes)); |
|
|
|
@ -185,7 +167,7 @@ public class Internal { |
|
|
|
|
public static boolean isValidUtf8(ByteString byteString) { |
|
|
|
|
return byteString.isValidUtf8(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 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. |
|
|
|
|
*/ |
|
|
|
|
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. |
|
|
|
|
*/ |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Helper method for implementing {@link Message#equals(Object)} for bytes field. |
|
|
|
|
*/ |
|
|
|
@ -298,7 +272,7 @@ public class Internal { |
|
|
|
|
} |
|
|
|
|
return hash; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Helper method for implementing {@link Message#hashCode()} for bytes field. |
|
|
|
|
*/ |
|
|
|
@ -309,7 +283,7 @@ public class Internal { |
|
|
|
|
// based hashCode() method.
|
|
|
|
|
return LiteralByteString.hashCode(bytes); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Helper method for implementing {@link Message#equals(Object)} for bytes |
|
|
|
|
* field. |
|
|
|
@ -322,7 +296,7 @@ public class Internal { |
|
|
|
|
// compare all the content.
|
|
|
|
|
return a.duplicate().clear().equals(b.duplicate().clear()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Helper method for implementing {@link Message#equals(Object)} for bytes |
|
|
|
|
* field. |
|
|
|
@ -351,9 +325,9 @@ public class Internal { |
|
|
|
|
} |
|
|
|
|
return hash; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final int DEFAULT_BUFFER_SIZE = 4096; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Helper method for implementing {@link Message#hashCode()} for bytes |
|
|
|
|
* field. |
|
|
|
@ -382,18 +356,18 @@ public class Internal { |
|
|
|
|
return h == 0 ? 1 : h; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* An empty byte array constant used in generated code. |
|
|
|
|
*/ |
|
|
|
|
public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* An empty byte array constant used in generated code. |
|
|
|
|
*/ |
|
|
|
|
public static final ByteBuffer EMPTY_BYTE_BUFFER = |
|
|
|
|
ByteBuffer.wrap(EMPTY_BYTE_ARRAY); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** An empty coded input stream constant used in generated code. */ |
|
|
|
|
public static final CodedInputStream EMPTY_CODED_INPUT_STREAM = |
|
|
|
|
CodedInputStream.newInstance(EMPTY_BYTE_ARRAY); |
|
|
|
@ -458,13 +432,13 @@ public class Internal { |
|
|
|
|
|
|
|
|
|
private final Map<K, RealValue> realMap; |
|
|
|
|
private final Converter<RealValue, V> valueConverter; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public MapAdapter(Map<K, RealValue> realMap, |
|
|
|
|
Converter<RealValue, V> valueConverter) { |
|
|
|
|
this.realMap = realMap; |
|
|
|
|
this.valueConverter = valueConverter; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
@Override |
|
|
|
|
public V get(Object key) { |
|
|
|
@ -474,7 +448,7 @@ public class Internal { |
|
|
|
|
} |
|
|
|
|
return valueConverter.doForward(result); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public V put(K key, V 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() { |
|
|
|
|
return new SetAdapter(realMap.entrySet()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class SetAdapter extends AbstractSet<Map.Entry<K, V>> { |
|
|
|
|
private final Set<Map.Entry<K, RealValue>> realSet; |
|
|
|
|
public SetAdapter(Set<Map.Entry<K, RealValue>> realSet) { |
|
|
|
|
this.realSet = realSet; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Iterator<java.util.Map.Entry<K, V>> iterator() { |
|
|
|
|
return new IteratorAdapter(realSet.iterator()); |
|
|
|
@ -503,17 +477,17 @@ public class Internal { |
|
|
|
|
@Override |
|
|
|
|
public int size() { |
|
|
|
|
return realSet.size(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class IteratorAdapter implements Iterator<Map.Entry<K, V>> { |
|
|
|
|
private final Iterator<Map.Entry<K, RealValue>> realIterator; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public IteratorAdapter( |
|
|
|
|
Iterator<Map.Entry<K, RealValue>> realIterator) { |
|
|
|
|
this.realIterator = realIterator; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public boolean hasNext() { |
|
|
|
|
return realIterator.hasNext(); |
|
|
|
@ -529,14 +503,14 @@ public class Internal { |
|
|
|
|
realIterator.remove(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class EntryAdapter implements Map.Entry<K, V> { |
|
|
|
|
private final Map.Entry<K, RealValue> realEntry; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public EntryAdapter(Map.Entry<K, RealValue> realEntry) { |
|
|
|
|
this.realEntry = realEntry; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public K getKey() { |
|
|
|
|
return realEntry.getKey(); |
|
|
|
|