本文整理汇总了Java中com.google.protobuf.CodedInputStream类的典型用法代码示例。如果您正苦于以下问题:Java CodedInputStream类的具体用法?Java CodedInputStream怎么用?Java CodedInputStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CodedInputStream类属于com.google.protobuf包,在下文中一共展示了CodedInputStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testTryWrite
import com.google.protobuf.CodedInputStream; //导入依赖的package包/类
@Test
public void testTryWrite() throws IOException {
Counter.Value v = Counter.Value.newBuilder().setValue(42).build();
assertFalse(connection.tryWrite(v));
CodedInputStream in = CodedInputStream.newInstance(channel.lastWrites.get(0));
int length = in.readRawLittleEndian32();
assertEquals(length, channel.lastWrites.get(0).length - 4);
Counter.Value w = Counter.Value.parseFrom(in);
assertEquals(v, w);
assertTrue(in.isAtEnd());
channel.clear();
channel.numBytesToAccept = 3;
assertTrue(connection.tryWrite(v));
channel.numBytesToAccept = -1;
assertFalse(connection.writeAvailable());
assertEquals(2, channel.lastWrites.size());
}
示例2: getDino
import com.google.protobuf.CodedInputStream; //导入依赖的package包/类
private static FullHttpResponse getDino(XrpcRequest request, List<Dino> dinos) {
try {
DinoGetRequest getRequest =
DinoGetRequest.parseFrom(CodedInputStream.newInstance(request.getData().nioBuffer()));
Optional<Dino> dinoOptional =
dinos.stream().filter(xs -> xs.getName().equals(getRequest.getName())).findFirst();
if (dinoOptional.isPresent()) {
DinoGetReply getReply = DinoGetReply.newBuilder().setDino(dinoOptional.get()).build();
ByteBuf resp = request.getByteBuf();
resp.ensureWritable(CodedOutputStream.computeMessageSizeNoTag(getReply), true);
getReply.writeTo(new ByteBufOutputStream(resp));
return Recipes.newResponse(
HttpResponseStatus.OK,
request.getByteBuf().writeBytes(resp),
Recipes.ContentType.Application_Octet_Stream);
}
} catch (IOException e) {
return Recipes.newResponseBadRequest("Malformed GetDino Request: " + e.getMessage());
}
return Recipes.newResponseOk("Dino not Found");
}
示例3: setDino
import com.google.protobuf.CodedInputStream; //导入依赖的package包/类
private static HttpResponse setDino(XrpcRequest request, List<Dino> dinos) {
try {
Optional<DinoSetRequest> setRequest =
Optional.of(
DinoSetRequest.parseFrom(
CodedInputStream.newInstance(request.getData().nioBuffer())));
setRequest.ifPresent(req -> dinos.add(req.getDino()));
return Recipes.newResponse(
HttpResponseStatus.OK,
request
.getByteBuf()
.writeBytes(DinoSetReply.newBuilder().setResponseCode("OK").build().toByteArray()),
Recipes.ContentType.Application_Octet_Stream);
} catch (IOException e) {
return Recipes.newResponseBadRequest("Malformed SetDino Request: " + e.getMessage());
}
}
示例4: decode
import com.google.protobuf.CodedInputStream; //导入依赖的package包/类
protected void decode(ChannelHandlerContext paramChannelHandlerContext, ByteBuf paramByteBuf, List<Object> paramList)
throws Exception {
paramByteBuf.markReaderIndex();
byte[] arrayOfByte = new byte[5];
for(int i = 0; i < arrayOfByte.length; i++) {
if(!paramByteBuf.isReadable()) {
paramByteBuf.resetReaderIndex();
return;
}
arrayOfByte[i] = paramByteBuf.readByte();
if(arrayOfByte[i] >= 0) {
int j = CodedInputStream.newInstance(arrayOfByte, 0, i + 1).readRawVarint32();
if(j < 0) {
throw new CorruptedFrameException("negative length: " + j);
}
if(paramByteBuf.readableBytes() < j) {
paramByteBuf.resetReaderIndex();
return;
}
paramList.add(paramByteBuf.readBytes(j));
return;
}
}
throw new CorruptedFrameException("length wider than 32-bit");
}
示例5: mergeFrom
import com.google.protobuf.CodedInputStream; //导入依赖的package包/类
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
ProcessDataProto.ProcessData parsedMessage = null;
try {
parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
parsedMessage = (ProcessDataProto.ProcessData) e.getUnfinishedMessage();
throw e.unwrapIOException();
} finally {
if (parsedMessage != null) {
mergeFrom(parsedMessage);
}
}
return this;
}
示例6: parseRequest
import com.google.protobuf.CodedInputStream; //导入依赖的package包/类
@Override public Request parseRequest(byte[] bytes) throws IOException {
ByteString byteString = UnsafeByteOperations.unsafeWrap(bytes);
CodedInputStream inputStream = byteString.newCodedInput();
// Enable aliasing to avoid an extra copy to get at the serialized Request inside of the
// WireMessage.
inputStream.enableAliasing(true);
WireMessage wireMsg = WireMessage.parseFrom(inputStream);
String serializedMessageClassName = wireMsg.getName();
try {
RequestTranslator translator = getParserForRequest(serializedMessageClassName);
// The ByteString should be logical offsets into the original byte array
return translator.transform(wireMsg.getWrappedMessage());
} catch (RuntimeException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Failed to parse request message '{}'", TextFormat.shortDebugString(wireMsg));
}
throw e;
}
}
示例7: parseResponse
import com.google.protobuf.CodedInputStream; //导入依赖的package包/类
@Override public Response parseResponse(byte[] bytes) throws IOException {
ByteString byteString = UnsafeByteOperations.unsafeWrap(bytes);
CodedInputStream inputStream = byteString.newCodedInput();
// Enable aliasing to avoid an extra copy to get at the serialized Response inside of the
// WireMessage.
inputStream.enableAliasing(true);
WireMessage wireMsg = WireMessage.parseFrom(inputStream);
String serializedMessageClassName = wireMsg.getName();
try {
ResponseTranslator translator = getParserForResponse(serializedMessageClassName);
return translator.transform(wireMsg.getWrappedMessage());
} catch (RuntimeException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Failed to parse response message '{}'", TextFormat.shortDebugString(wireMsg));
}
throw e;
}
}
示例8: loginPacketDispatcher
import com.google.protobuf.CodedInputStream; //导入依赖的package包/类
/**
* @param commandId
* @param buffer
*
* 有没有更加优雅的方式
*/
public static void loginPacketDispatcher(int commandId,CodedInputStream buffer){
try {
switch (commandId) {
// case IMBaseDefine.LoginCmdID.CID_LOGIN_RES_USERLOGIN_VALUE :
// IMLogin.IMLoginRes imLoginRes = IMLogin.IMLoginRes.parseFrom(buffer);
// IMLoginManager.instance().onRepMsgServerLogin(imLoginRes);
// return;
case IMBaseDefine.LoginCmdID.CID_LOGIN_RES_LOGINOUT_VALUE:
IMLogin.IMLogoutRsp imLogoutRsp = IMLogin.IMLogoutRsp.parseFrom(buffer);
IMLoginManager.instance().onRepLoginOut(imLogoutRsp);
return;
case IMBaseDefine.LoginCmdID.CID_LOGIN_KICK_USER_VALUE:
IMLogin.IMKickUser imKickUser = IMLogin.IMKickUser.parseFrom(buffer);
IMLoginManager.instance().onKickout(imKickUser);
}
} catch (IOException e) {
logger.e("loginPacketDispatcher# error,cid:%d",commandId);
}
}
示例9: readFrom
import com.google.protobuf.CodedInputStream; //导入依赖的package包/类
public static BlockListAsLongs readFrom(InputStream is) throws IOException {
CodedInputStream cis = CodedInputStream.newInstance(is);
int numBlocks = -1;
ByteString blocksBuf = null;
while (!cis.isAtEnd()) {
int tag = cis.readTag();
int field = WireFormat.getTagFieldNumber(tag);
switch(field) {
case 0:
break;
case 1:
numBlocks = (int)cis.readInt32();
break;
case 2:
blocksBuf = cis.readBytes();
break;
default:
cis.skipField(tag);
break;
}
}
if (numBlocks != -1 && blocksBuf != null) {
return decodeBuffer(numBlocks, blocksBuf);
}
return null;
}
示例10: loadINodeSection
import com.google.protobuf.CodedInputStream; //导入依赖的package包/类
private static byte[][] loadINodeSection(InputStream in)
throws IOException {
FsImageProto.INodeSection s = FsImageProto.INodeSection
.parseDelimitedFrom(in);
LOG.info("Loading " + s.getNumInodes() + " inodes.");
final byte[][] inodes = new byte[(int) s.getNumInodes()][];
for (int i = 0; i < s.getNumInodes(); ++i) {
int size = CodedInputStream.readRawVarint32(in.read(), in);
byte[] bytes = new byte[size];
IOUtils.readFully(in, bytes, 0, size);
inodes[i] = bytes;
}
LOG.debug("Sorting inodes");
Arrays.sort(inodes, INODE_BYTES_COMPARATOR);
LOG.debug("Finished sorting inodes");
return inodes;
}
示例11: loadFromBuffer
import com.google.protobuf.CodedInputStream; //导入依赖的package包/类
private void loadFromBuffer(ByteBuffer buffer) {
final String MESSAGE_NAME = AISProtobuf.AkibanInformationSchema.getDescriptor().getFullName();
checkBuffer(buffer);
final int serializedSize = buffer.getInt();
final int initialPos = buffer.position();
final int bufferSize = buffer.limit() - initialPos;
if(bufferSize < serializedSize) {
throw new ProtobufReadException(MESSAGE_NAME, "Buffer corrupt, serialized size greater than remaining");
}
CodedInputStream codedInput = CodedInputStream.newInstance(buffer.array(), buffer.position(), Math.min(serializedSize, bufferSize));
try {
pbAISBuilder.mergeFrom(codedInput, storageFormatRegistry.getExtensionRegistry());
// Successfully consumed, update byte buffer
buffer.position(initialPos + serializedSize);
} catch(IOException e) {
// CodedInputStream really only throws InvalidProtocolBufferException, but declares IOE
throw new ProtobufReadException(MESSAGE_NAME, e.getMessage());
}
}
示例12: mergeFrom
import com.google.protobuf.CodedInputStream; //导入依赖的package包/类
public PushSMPMessageProtos.PushSMPMessageContent.Builder mergeFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry) throws IOException {
PushSMPMessageProtos.PushSMPMessageContent parsedMessage = null;
try {
parsedMessage = (PushSMPMessageProtos.PushSMPMessageContent)PushSMPMessageProtos.PushSMPMessageContent.PARSER.parsePartialFrom(input, extensionRegistry);
} catch (InvalidProtocolBufferException var8) {
parsedMessage = (PushSMPMessageProtos.PushSMPMessageContent)var8.getUnfinishedMessage();
throw var8;
} finally {
if(parsedMessage != null) {
this.mergeFrom(parsedMessage);
}
}
return this;
}
示例13: readFrom
import com.google.protobuf.CodedInputStream; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
<T> T readFrom(ByteBuffer bb) throws IOException {
// using the parser with a byte[]-backed coded input stream is the
// most efficient way to deserialize a protobuf. it has a direct
// path to the PB ctor that doesn't create multi-layered streams
// that internally buffer.
CodedInputStream cis = CodedInputStream.newInstance(
bb.array(), bb.position() + bb.arrayOffset(), bb.remaining());
try {
cis.pushLimit(cis.readRawVarint32());
message = message.getParserForType().parseFrom(cis);
cis.checkLastTagWas(0);
} finally {
// advance over the bytes read.
bb.position(bb.position() + cis.getTotalBytesRead());
}
return (T)message;
}
示例14: main
import com.google.protobuf.CodedInputStream; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
Configuration conf = new Configuration();
FileSystem fileSystem = FileSystem.get(conf);
//
// SequenceFile.Writer w = SequenceFile.createWriter(fileSystem, conf, new Path("seq"), Text.class, BytesWritable.class);
// for (int i = 0; i < 1; i++) {
// ASTRoot.Builder ast = ASTRoot.newBuilder();
// ast.addImports("a.b.C");
// w.append(new Text(i + ""), new BytesWritable(ast.build().toByteArray()));
// System.out.println("Parse before writing to sequence file: " + ASTRoot.parseFrom(ast.build().toByteArray()).getImportsList());
// }
// w.close();
Text key = new Text();
BytesWritable val = new BytesWritable();
SequenceFile.Reader r = new SequenceFile.Reader(fileSystem, new Path("/Users/nmtiwari/nmt/githubCacheJSon/tmprepcache/ast-nmtiwari-0-1453911429.seq"), conf);
while (r.next(key, val)) {
System.out.println("next project");
byte[] bytes = val.getBytes();
System.out.print("Parse after writing to sequence file: ");
//System.out.println(ASTRoot.parseFrom(bytes).getImportsList());
System.out.println(ASTRoot.parseFrom(CodedInputStream.newInstance(bytes, 0, val.getLength())));
}
r.close();
}
示例15: mergeFrom
import com.google.protobuf.CodedInputStream; //导入依赖的package包/类
public Builder mergeFrom(
CodedInputStream input,
ExtensionRegistryLite extensionRegistry)
throws IOException {
MumbleProto.Mumble.PermissionQuery parsedMessage = null;
try {
parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
} catch (InvalidProtocolBufferException e) {
parsedMessage = (MumbleProto.Mumble.PermissionQuery) e.getUnfinishedMessage();
throw e;
} finally {
if (parsedMessage != null) {
mergeFrom(parsedMessage);
}
}
return this;
}