本文整理汇总了Java中com.google.protobuf.Parser.parseFrom方法的典型用法代码示例。如果您正苦于以下问题:Java Parser.parseFrom方法的具体用法?Java Parser.parseFrom怎么用?Java Parser.parseFrom使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.protobuf.Parser
的用法示例。
在下文中一共展示了Parser.parseFrom方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: extract
import com.google.protobuf.Parser; //导入方法依赖的package包/类
/**
* Attempts to extract a protocol buffer from the specified extra.
* @throws MalformedDataException if the intent is null, the extra is missing or not a byte
* array, or the protocol buffer could not be parsed.
*/
@NonNull
public static <T extends MessageLite> T extract(
@NonNull String extraName,
@NonNull Parser<T> protoParser,
@NonNull String failureDescription,
@Nullable Intent intent)
throws MalformedDataException {
if (intent == null) {
throw new MalformedDataException(failureDescription);
}
byte[] protoBytes = intent.getByteArrayExtra(extraName);
if (protoBytes == null) {
throw new MalformedDataException(failureDescription);
}
try {
return protoParser.parseFrom(protoBytes);
} catch (IOException ex) {
throw new MalformedDataException(failureDescription, ex);
}
}
示例2: toScanMetrics
import com.google.protobuf.Parser; //导入方法依赖的package包/类
public static ScanMetrics toScanMetrics(final byte[] bytes) {
Parser<MapReduceProtos.ScanMetrics> parser = MapReduceProtos.ScanMetrics.PARSER;
MapReduceProtos.ScanMetrics pScanMetrics = null;
try {
pScanMetrics = parser.parseFrom(bytes);
} catch (InvalidProtocolBufferException e) {
//Ignored there are just no key values to add.
}
ScanMetrics scanMetrics = new ScanMetrics();
if (pScanMetrics != null) {
for (HBaseProtos.NameInt64Pair pair : pScanMetrics.getMetricsList()) {
if (pair.hasName() && pair.hasValue()) {
scanMetrics.setCounter(pair.getName(), pair.getValue());
}
}
}
return scanMetrics;
}
示例3: readProtobuf
import com.google.protobuf.Parser; //导入方法依赖的package包/类
/**
* De-serializes a protobuf from the given buffer.
* <p>
* The protobuf is assumed to be prefixed by a varint indicating its size.
* @param buf The buffer to de-serialize the protobuf from.
* @param parser The protobuf parser to use for this type of protobuf.
* @return An instance of the de-serialized type.
* @throws InvalidResponseException if the buffer contained an invalid
* protobuf that couldn't be de-serialized.
*/
static <T> T readProtobuf(final ChannelBuffer buf, final Parser<T> parser) {
final int length = HBaseRpc.readProtoBufVarint(buf);
HBaseRpc.checkArrayLength(buf, length);
final byte[] payload;
final int offset;
if (buf.hasArray()) { // Zero copy.
payload = buf.array();
offset = buf.arrayOffset() + buf.readerIndex();
buf.readerIndex(buf.readerIndex() + length);
} else { // We have to copy the entire payload out of the buffer :(
payload = new byte[length];
buf.readBytes(payload);
offset = 0;
}
try {
return parser.parseFrom(payload, offset, length);
} catch (InvalidProtocolBufferException e) {
final String msg = "Invalid RPC response: length=" + length
+ ", payload=" + Bytes.pretty(payload);
LOG.error("Invalid RPC from buffer: " + buf);
throw new InvalidResponseException(msg, e);
}
}
示例4: parseMessage
import com.google.protobuf.Parser; //导入方法依赖的package包/类
private Message<? extends com.google.protobuf.Message> parseMessage() {
Envelope envelope = null;
try {
envelope = Envelope.parseFrom(record.value());
} catch (InvalidProtocolBufferException parseError) {
markAsConsumed(record.offset());
parsingFailed(envelope, parseError);
return null;
}
try {
MessageType type = new MessageType(envelope.getMessageType());
Parser<com.google.protobuf.Message> parser = typeDictionary.parserFor(type);
if (parser == null) {
throw new UnknownMessageTypeException(type);
}
com.google.protobuf.Message innerMessage = parser.parseFrom(envelope.getInnerMessage());
return Messages.fromKafka(innerMessage, envelope, record);
} catch (InvalidProtocolBufferException | UnknownMessageTypeException unrecoverableParsingError) {
markAsConsumed(record.offset());
parsingFailed(envelope, unrecoverableParsingError);
return null;
}
}
示例5: get
import com.google.protobuf.Parser; //导入方法依赖的package包/类
public static <T> T get(ByteBuf pBody, Parser<T> parser) throws RpcException{
try {
ByteBufInputStream is = new ByteBufInputStream(pBody);
return parser.parseFrom(is);
} catch (InvalidProtocolBufferException e) {
throw new RpcException(String.format("Failure while decoding message with parser of type. %s", parser.getClass().getCanonicalName()), e);
}
}
示例6: read
import com.google.protobuf.Parser; //导入方法依赖的package包/类
public static <Proto> Proto read(byte[] buf, InputStream stream, Parser<Proto> parser)
throws IOException {
int leidos = stream.read(buf, 0, 2);
if (leidos == 2) {
final int tam = ((buf[0] & 0xff) << 8) | (buf[1] & 0xff);
leidos = stream.read(buf, 0, tam);
if (leidos > 0) {
return parser.parseFrom(buf, 0, leidos);
}
}
return null;
}
示例7: get
import com.google.protobuf.Parser; //导入方法依赖的package包/类
public static <T> T get(ByteBuf pBody, Parser<T> parser) throws RpcException {
try {
ByteBufInputStream is = new ByteBufInputStream(pBody);
return parser.parseFrom(is);
} catch (InvalidProtocolBufferException e) {
throw new RpcException(String.format("Failure while decoding message with parser of type. %s", parser.getClass().getCanonicalName()), e);
}
}
示例8: parseMessage
import com.google.protobuf.Parser; //导入方法依赖的package包/类
private static <T extends Message> T parseMessage(DocumentSnapshot doc, Parser<T> parser) {
final Blob blob = doc.getBlob(BYTES_KEY);
final byte[] bytes = blob.toBytes();
try {
final T message = parser.parseFrom(bytes);
return message;
} catch (InvalidProtocolBufferException e) {
throw illegalStateWithCauseOf(e);
}
}
示例9: parseAny
import com.google.protobuf.Parser; //导入方法依赖的package包/类
private <T> T parseAny(Any value, Parser<T> parser) {
try {
return parser.parseFrom(value.getValue());
} catch (InvalidProtocolBufferException e) {
throw new RuntimeException(e);
}
}
示例10: get
import com.google.protobuf.Parser; //导入方法依赖的package包/类
public static <T> T get(ByteBuf pBody, Parser<T> parser) throws RpcException {
try {
ByteBufInputStream is = new ByteBufInputStream(pBody);
return parser.parseFrom(is);
} catch (InvalidProtocolBufferException e) {
throw new RpcException(
String.format("Failure while decoding message with parser of type. %s",
parser.getClass().getCanonicalName()), e);
}
}
示例11: decode
import com.google.protobuf.Parser; //导入方法依赖的package包/类
/**
* Decode custom protobuf variable length array.
*
* @param <T> the item type
* @param data the raw input, not null
* @param parser the parser to decode each message, not null
* @return the decoded list of items, not null
* @throws IOException
* @throws NullPointerException if any arguments are null
*/
public static <T> List<T> decode(InputStream data, Parser<T> parser) throws IOException {
CodedInputStream stream = CodedInputStream.newInstance(data);
List<T> list = new ArrayList<>();
while (!stream.isAtEnd()) {
int size = stream.readRawVarint32();
byte[] element = stream.readRawBytes(size);
T decoded = parser.parseFrom(element);
list.add(decoded);
}
return list;
}
示例12: readMessage
import com.google.protobuf.Parser; //导入方法依赖的package包/类
public <T extends /*@NonNull*/ AbstractMessage> /*@Nullable*/ T readMessage(long cappedId,
Parser<T> parser) throws IOException {
boolean overwritten;
boolean inTheFuture;
synchronized (lock) {
overwritten = out.isOverwritten(cappedId);
inTheFuture = cappedId >= out.getCurrIndex();
}
if (overwritten) {
return null;
}
if (inTheFuture) {
// this can happen when the glowroot folder is copied for analysis without shutting down
// the JVM and glowroot.capped.db is copied first, then new data is written to
// glowroot.capped.db and the new capped ids are written to glowroot.h2.db and then
// glowroot.h2.db is copied with capped ids that do not exist in the copied
// glowroot.capped.db
return null;
}
// it's important to wrap CappedBlockInputStream in a BufferedInputStream to prevent
// lots of small reads from the underlying RandomAccessFile
final int bufferSize = 32768;
InputStream input = newLZFInputStream(
new BufferedInputStream(new CappedBlockInputStream(cappedId), bufferSize));
try {
return parser.parseFrom(input);
} catch (Exception e) {
if (!out.isOverwritten(cappedId)) {
logger.error(e.getMessage(), e);
}
return null;
} finally {
input.close();
}
}
示例13: getMessage
import com.google.protobuf.Parser; //导入方法依赖的package包/类
public MessageLite getMessage(ProtobaseFuture future) throws InvalidProtocolBufferException {
Parser<? extends MessageLite> parser = getParser(future.getFutureName());
return parser.parseFrom(future.getBinary());
}