parent
0422db3be9
commit
95dbaa67a8
5 changed files with 0 additions and 1890 deletions
@ -1,91 +0,0 @@ |
||||
// Copyright 2008 Google Inc. All Rights Reserved.
|
||||
|
||||
package com.google.common.io.protocol; |
||||
|
||||
import java.io.*; |
||||
|
||||
/** |
||||
* An input stream backed by another input stream, where reading from the |
||||
* underlying input stream is limited to a fixed number of bytes. Also does |
||||
* some buffering. |
||||
* |
||||
*/ |
||||
public class BoundInputStream extends InputStream { |
||||
|
||||
/** Buffer size */ |
||||
static final int BUF_SIZE = 4096; |
||||
|
||||
/** Number of bytes that may still be read from the underlying stream */ |
||||
private int remaining; |
||||
|
||||
/** Small buffer to avoid making OS calls for each byte read. */ |
||||
private byte[] buf; |
||||
|
||||
/** Current position in the buffer */ |
||||
private int bufPos; |
||||
|
||||
/** Filled size of the buffer */ |
||||
private int bufSize; |
||||
|
||||
/** Underlying stream to read from */ |
||||
private InputStream base; |
||||
|
||||
public BoundInputStream(InputStream base, int len) { |
||||
this.base = base; |
||||
this.remaining = len; |
||||
|
||||
buf = new byte[Math.min(len, BUF_SIZE)]; |
||||
} |
||||
|
||||
/** |
||||
* Make sure there is at least one byte in the buffer. If not possible, |
||||
* return false. |
||||
*/ |
||||
private boolean checkBuf() throws IOException { |
||||
if (remaining <= 0) { |
||||
return false; |
||||
} |
||||
|
||||
if (bufPos >= bufSize) { |
||||
bufSize = base.read(buf, 0, Math.min(remaining, buf.length)); |
||||
if (bufSize <= 0) { |
||||
remaining = 0; |
||||
return false; |
||||
} |
||||
bufPos = 0; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
public int available() { |
||||
return bufSize - bufPos; |
||||
} |
||||
|
||||
public int read() throws IOException { |
||||
if (!checkBuf()) { |
||||
return -1; |
||||
} |
||||
remaining--; |
||||
return buf[bufPos++] & 255; |
||||
} |
||||
|
||||
public int read(byte[] data, int start, int count) throws IOException { |
||||
if (!checkBuf()) { |
||||
return -1; |
||||
} |
||||
count = Math.min(count, bufSize - bufPos); |
||||
System.arraycopy(buf, bufPos, data, start, count); |
||||
bufPos += count; |
||||
remaining -= count; |
||||
return count; |
||||
} |
||||
|
||||
/** |
||||
* How many bytes are remaining, based on the length provided to the |
||||
* constructor. The underlying stream may terminate earlier. Provided mainly |
||||
* for testing purposes. |
||||
*/ |
||||
public int getRemaining() { |
||||
return remaining; |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -1,170 +0,0 @@ |
||||
// Copyright 2007 Google Inc.
|
||||
// All Rights Reserved.
|
||||
|
||||
package com.google.common.io.protocol; |
||||
|
||||
import java.util.*; |
||||
|
||||
/** |
||||
* This class can be used to create a memory model of a .proto file. Currently, |
||||
* it is assumed that tags ids are not large. This could be improved by storing |
||||
* a start offset, relaxing the assumption to a dense number space. |
||||
*/ |
||||
public class ProtoBufType { |
||||
// Note: Values 0..15 are reserved for wire types!
|
||||
public static final int TYPE_UNDEFINED = 16; |
||||
public static final int TYPE_DOUBLE = 17; |
||||
public static final int TYPE_FLOAT = 18; |
||||
public static final int TYPE_INT64 = 19; |
||||
public static final int TYPE_UINT64 = 20; |
||||
public static final int TYPE_INT32 = 21; |
||||
public static final int TYPE_FIXED64 = 22; |
||||
public static final int TYPE_FIXED32 = 23; |
||||
public static final int TYPE_BOOL = 24; |
||||
public static final int TYPE_DATA = 25; |
||||
public static final int TYPE_GROUP = 26; |
||||
public static final int TYPE_MESSAGE = 27; |
||||
public static final int TYPE_TEXT = 28; |
||||
public static final int TYPE_UINT32 = 29; |
||||
public static final int TYPE_ENUM = 30; |
||||
public static final int TYPE_SFIXED32 = 31; |
||||
public static final int TYPE_SFIXED64 = 32; |
||||
|
||||
// new protobuf 2 types
|
||||
public static final int TYPE_SINT32 = 33; |
||||
public static final int TYPE_SINT64 = 34; |
||||
public static final int TYPE_BYTES = 35; |
||||
public static final int TYPE_STRING = 36; |
||||
|
||||
public static final int MASK_TYPE = 0x0ff; |
||||
public static final int MASK_MODIFIER = 0x0ff00; |
||||
|
||||
public static final int REQUIRED = 0x100; |
||||
public static final int OPTIONAL = 0x200; |
||||
public static final int REPEATED = 0x400; |
||||
|
||||
private final StringBuffer types = new StringBuffer(); |
||||
private final Vector data = new Vector(); |
||||
private final String typeName; |
||||
|
||||
/** |
||||
* Empty constructor. |
||||
*/ |
||||
public ProtoBufType() { |
||||
typeName = null; |
||||
} |
||||
|
||||
/** |
||||
* Constructor including a type name for debugging purposes. |
||||
*/ |
||||
public ProtoBufType(String typeName) { |
||||
this.typeName = typeName; |
||||
} |
||||
|
||||
/** |
||||
* Adds a tag description. The data parameter contains the group definition |
||||
* for group elements and the default value for regular elements. |
||||
* |
||||
* @param optionsAndType any legal combination (bitwise or) of REQUIRED |
||||
* or OPTIONAL and REPEATED and one of the TYPE_ |
||||
* constants |
||||
* @param tag the tag id |
||||
* @param data the type for group elements (or the default value for |
||||
* regular elements in future versions) |
||||
* @return this is returned to permit cascading |
||||
*/ |
||||
public ProtoBufType addElement(int optionsAndType, int tag, Object data) { |
||||
while (types.length() <= tag) { |
||||
types.append((char) TYPE_UNDEFINED); |
||||
this.data.addElement(null); |
||||
} |
||||
types.setCharAt(tag, (char) optionsAndType); |
||||
this.data.setElementAt(data, tag); |
||||
|
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Returns the type for the given tag id (without modifiers such as OPTIONAL, |
||||
* REPEATED). For undefined tags, TYPE_UNDEFINED is returned. |
||||
*/ |
||||
public int getType(int tag) { |
||||
return (tag < 0 || tag >= types.length()) |
||||
? TYPE_UNDEFINED |
||||
: (types.charAt(tag) & MASK_TYPE); |
||||
} |
||||
|
||||
/** |
||||
* Returns a bit combination of the modifiers for the given tag id |
||||
* (OPTIONAL, REPEATED, REQUIRED). For undefined tags, OPTIONAL|REPEATED |
||||
* is returned. |
||||
*/ |
||||
public int getModifiers(int tag) { |
||||
return (tag < 0 || tag >= types.length()) |
||||
? (OPTIONAL | REPEATED) |
||||
: (types.charAt(tag) & MASK_MODIFIER); |
||||
} |
||||
|
||||
/** |
||||
* Returns the data associated to a given tag (either the default value for |
||||
* regular elements or a ProtoBufType for groups and messages). For undefined |
||||
* tags, null is returned. |
||||
*/ |
||||
public Object getData(int tag) { |
||||
return (tag < 0 || tag >= data.size()) ? null : data.elementAt(tag); |
||||
} |
||||
|
||||
/** |
||||
* Returns the type name set in the constructor for debugging purposes. |
||||
*/ |
||||
public String toString() { |
||||
return typeName; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritDoc} |
||||
* <p>Two ProtoBufTypes are equals if the fields types are the same. |
||||
*/ |
||||
public boolean equals(Object object) { |
||||
if (null == object) { |
||||
// trivial check
|
||||
return false; |
||||
} else if (this == object) { |
||||
// trivial check
|
||||
return true; |
||||
} else if (this.getClass() != object.getClass()) { |
||||
// different class
|
||||
return false; |
||||
} |
||||
ProtoBufType other = (ProtoBufType) object; |
||||
|
||||
return stringEquals(types, other.types); |
||||
} |
||||
|
||||
/** |
||||
* {@inheritDoc} |
||||
*/ |
||||
public int hashCode() { |
||||
if (types != null) { |
||||
return types.hashCode(); |
||||
} else { |
||||
return super.hashCode(); |
||||
} |
||||
} |
||||
|
||||
public static boolean stringEquals(CharSequence a, CharSequence b) { |
||||
if (a == b) return true; |
||||
int length; |
||||
if (a != null && b != null && (length = a.length()) == b.length()) { |
||||
if (a instanceof String && b instanceof String) { |
||||
return a.equals(b); |
||||
} else { |
||||
for (int i = 0; i < length; i++) { |
||||
if (a.charAt(i) != b.charAt(i)) return false; |
||||
} |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
} |
@ -1,285 +0,0 @@ |
||||
// Copyright 2008 Google Inc. All Rights Reserved.
|
||||
|
||||
package com.google.common.io.protocol; |
||||
|
||||
import java.io.*; |
||||
|
||||
/** |
||||
* Utility functions for dealing with ProtoBuf objects consolidated from |
||||
* previous spot implementations across the codebase. |
||||
* |
||||
*/ |
||||
public final class ProtoBufUtil { |
||||
private ProtoBufUtil() { |
||||
} |
||||
|
||||
/** Convenience method to return a string value from of a proto or "". */ |
||||
public static String getProtoValueOrEmpty(ProtoBuf proto, int tag) { |
||||
try { |
||||
return (proto != null && proto.has(tag)) ? proto.getString(tag) : ""; |
||||
} catch (ClassCastException e) { |
||||
return ""; |
||||
} |
||||
} |
||||
|
||||
/** Convenience method to return a string value from of a sub-proto or "". */ |
||||
public static String getSubProtoValueOrEmpty( |
||||
ProtoBuf proto, int sub, int tag) { |
||||
try { |
||||
return getProtoValueOrEmpty(getSubProtoOrNull(proto, sub), tag); |
||||
} catch (ClassCastException e) { |
||||
return ""; |
||||
} |
||||
} |
||||
|
||||
/** Convenience method to get a subproto if the proto has it. */ |
||||
public static ProtoBuf getSubProtoOrNull(ProtoBuf proto, int sub) { |
||||
return (proto != null && proto.has(sub)) ? proto.getProtoBuf(sub) : null; |
||||
} |
||||
|
||||
/** |
||||
* Get an int with "tag" from the proto buffer. If the given field can't be |
||||
* retrieved, return the provided default value. |
||||
* |
||||
* @param proto The proto buffer. |
||||
* @param tag The tag value that identifies which protocol buffer field to |
||||
* retrieve. |
||||
* @param defaultValue The value to return if the field can't be retrieved. |
||||
* @return The result which should be an integer. |
||||
*/ |
||||
public static int getProtoValueOrDefault(ProtoBuf proto, int tag, |
||||
int defaultValue) { |
||||
try { |
||||
return (proto != null && proto.has(tag)) |
||||
? proto.getInt(tag) : defaultValue; |
||||
} catch (IllegalArgumentException e) { |
||||
return defaultValue; |
||||
} catch (ClassCastException e) { |
||||
return defaultValue; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Get an Int with "tag" from the proto buffer. |
||||
* If the given field can't be retrieved, return 0. |
||||
* |
||||
* @param proto The proto buffer. |
||||
* @param tag The tag value that identifies which protocol buffer field to |
||||
* retrieve. |
||||
* @return The result which should be an integer. |
||||
*/ |
||||
public static int getProtoValueOrZero(ProtoBuf proto, int tag) { |
||||
return getProtoValueOrDefault(proto, tag, 0); |
||||
} |
||||
|
||||
/** |
||||
* Get an Long with "tag" from the proto buffer. |
||||
* If the given field can't be retrieved, return 0. |
||||
* |
||||
* @param proto The proto buffer. |
||||
* @param tag The tag value that identifies which protocol buffer field to |
||||
* retrieve. |
||||
* @return The result which should be an integer. |
||||
*/ |
||||
public static long getProtoLongValueOrZero(ProtoBuf proto, int tag) { |
||||
try { |
||||
return (proto != null && proto.has(tag)) ? proto.getLong(tag) : 0L; |
||||
} catch (IllegalArgumentException e) { |
||||
return 0L; |
||||
} catch (ClassCastException e) { |
||||
return 0L; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Get an Int with "tag" from the proto buffer. |
||||
* If the given field can't be retrieved, return -1. |
||||
* |
||||
* @param proto The proto buffer. |
||||
* @param tag The tag value that identifies which protocol buffer field to |
||||
* retrieve. |
||||
* @return The result which should be a long. |
||||
*/ |
||||
public static long getProtoValueOrNegativeOne(ProtoBuf proto, int tag) { |
||||
try { |
||||
return (proto != null && proto.has(tag)) ? proto.getLong(tag) : -1; |
||||
} catch (IllegalArgumentException e) { |
||||
return -1; |
||||
} catch (ClassCastException e) { |
||||
return -1; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Returns an input stream for reading protocol buffer |
||||
* responses. This method reads a 32-bit signed integer from the |
||||
* stream, which determines the data size and compression. If the |
||||
* integer is negative, indicating a GZipped input stream which we |
||||
* do not support, an exception is thrown. Otherwise, just a |
||||
* BoundInputStream is returned. The input stream returned is always |
||||
* limited to the data available. |
||||
* |
||||
* @param dataInput the data input to read from |
||||
* @return an input stream, limited to the data size read from the stream |
||||
* @throws IOException if the incoming stream is gzipped. |
||||
*/ |
||||
public static InputStream getInputStreamForProtoBufResponse( |
||||
DataInput dataInput) throws IOException { |
||||
|
||||
int size = dataInput.readInt(); |
||||
InputStream is = new BoundInputStream((InputStream) dataInput, |
||||
Math.abs(size)); |
||||
|
||||
if (size < 0) { |
||||
throw new IOException("Cannot read gzipped streams"); |
||||
} |
||||
return is; |
||||
} |
||||
|
||||
/** |
||||
* Reads a single protocol buffer from the given input stream. This method is |
||||
* provided where the client needs incremental access to the contents of a |
||||
* protocol buffer which contains a sequence of protocol buffers. |
||||
* <p /> |
||||
* Please use {@link #getInputStreamForProtoBufResponse} to obtain an input |
||||
* stream suitable for this method. |
||||
* |
||||
* @param umbrellaType the type of the "outer" protocol buffer containing |
||||
* the message to read |
||||
* @param is the stream to read the protocol buffer from |
||||
* @param result the result protocol buffer (must be empty, will be filled |
||||
* with the data read and the type will be set) |
||||
* @return the tag id of the message, -1 at the end of the stream |
||||
*/ |
||||
public static int readNextProtoBuf(ProtoBufType umbrellaType, |
||||
InputStream is, ProtoBuf result) throws IOException { |
||||
long tagAndType = ProtoBuf.readVarInt(is, true /* permits EOF */); |
||||
if (tagAndType == -1) { |
||||
return -1; |
||||
} |
||||
|
||||
if ((tagAndType & 7) != ProtoBuf.WIRETYPE_LENGTH_DELIMITED) { |
||||
throw new IOException("Message expected"); |
||||
} |
||||
int tag = (int) (tagAndType >>> 3); |
||||
|
||||
result.setType((ProtoBufType) umbrellaType.getData(tag)); |
||||
int length = (int) ProtoBuf.readVarInt(is, false); |
||||
result.parse(is, length); |
||||
return tag; |
||||
} |
||||
|
||||
/** |
||||
* Reads a size int and a protocol buffer from a DataInput. If the size |
||||
* is negative, this is interpreted as an indicator that the protocol buffer |
||||
* is packed with GZIP. In this case, -size bytes are read, and the data is |
||||
* unpacked with GZIP before constructing the protocol buffer. |
||||
* |
||||
* @param protoBufType the protocol buffer type to read |
||||
* @param dataInput the data input to read from |
||||
* @return a protocol buffer of the given type |
||||
* @throws IOException |
||||
*/ |
||||
public static ProtoBuf readProtoBufResponse(ProtoBufType protoBufType, |
||||
DataInput dataInput) throws IOException { |
||||
ProtoBuf response = new ProtoBuf(protoBufType); |
||||
InputStream is = getInputStreamForProtoBufResponse(dataInput); |
||||
response.parse(is); |
||||
if (is.read() != -1) { |
||||
throw new IOException(); |
||||
} |
||||
return response; |
||||
} |
||||
|
||||
/** |
||||
* A wrapper for <code> getProtoValueOrNegativeOne </code> that drills into |
||||
* a sub message returning the long value if it exists, returning -1 if it |
||||
* does not. |
||||
* |
||||
* @param proto The proto buffer. |
||||
* @param tag The tag value that identifies which protocol buffer field to |
||||
* retrieve. |
||||
* @param sub The sub tag value that identifies which protocol buffer |
||||
* sub-field to retrieve.n |
||||
* @return The result which should be a long. |
||||
*/ |
||||
public static long getSubProtoValueOrNegativeOne( |
||||
ProtoBuf proto, int sub, int tag) { |
||||
try { |
||||
return getProtoValueOrNegativeOne(getSubProtoOrNull(proto, sub), tag); |
||||
} catch (IllegalArgumentException e) { |
||||
return -1; |
||||
} catch (ClassCastException e) { |
||||
return -1; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* A wrapper for {@link #getProtoValueOrDefault(ProtoBuf, int, int)} that |
||||
* drills into a sub message returning the int value if it exists, returning |
||||
* the given default if it does not. |
||||
* |
||||
* @param proto The proto buffer. |
||||
* @param tag The tag value that identifies which protocol buffer field to |
||||
* retrieve. |
||||
* @param sub The sub tag value that identifies which protocol buffer |
||||
* sub-field to retrieve. |
||||
* @param defaultValue The value to return if the field is not present. |
||||
* @return The result which should be a long. |
||||
*/ |
||||
public static int getSubProtoValueOrDefault(ProtoBuf proto, int sub, int tag, |
||||
int defaultValue) { |
||||
try { |
||||
return getProtoValueOrDefault(getSubProtoOrNull(proto, sub), tag, |
||||
defaultValue); |
||||
} catch (IllegalArgumentException e) { |
||||
return defaultValue; |
||||
} catch (ClassCastException e) { |
||||
return defaultValue; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Creates a sub ProtoBuf of the given Protobuf and sets it. |
||||
* |
||||
* @param proto The proto buffer. |
||||
* @param tag The tag value that identifies which protocol buffer field to |
||||
* create. |
||||
* @return the sub ProtoBuf generated. |
||||
*/ |
||||
public static ProtoBuf createProtoBuf(ProtoBuf proto, int tag) { |
||||
ProtoBuf child = proto.createGroup(tag); |
||||
proto.setProtoBuf(tag, child); |
||||
return child; |
||||
} |
||||
|
||||
/** |
||||
* Creates a sub ProtoBuf of the given Protobuf and adds it. |
||||
* |
||||
* @param proto The proto buffer. |
||||
* @param tag The tag value that identifies which protocol buffer field to |
||||
* add. |
||||
* @return the sub ProtoBuf generated. |
||||
*/ |
||||
public static ProtoBuf addProtoBuf(ProtoBuf proto, int tag) { |
||||
ProtoBuf child = proto.createGroup(tag); |
||||
proto.addProtoBuf(tag, child); |
||||
return child; |
||||
} |
||||
|
||||
/** |
||||
* Writes the ProtoBuf to the given DataOutput. This is useful for unit |
||||
* tests. |
||||
* |
||||
* @param output The data output to write to. |
||||
* @param protoBuf The proto buffer. |
||||
*/ |
||||
public static void writeProtoBufToOutput(DataOutput output, ProtoBuf protoBuf) |
||||
throws IOException { |
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
||||
protoBuf.outputTo(baos); |
||||
byte[] bytes = baos.toByteArray(); |
||||
output.writeInt(bytes.length); |
||||
output.write(bytes); |
||||
} |
||||
} |
@ -1,5 +0,0 @@ |
||||
<html> |
||||
<body> |
||||
{@hide} |
||||
</body> |
||||
</html> |
Loading…
Reference in new issue