Search in sources :

Example 11 with CodedInputStream

use of com.google.protobuf.CodedInputStream in project grpc-java by grpc.

the class ProtoLiteUtils method marshaller.

/** Create a {@code Marshaller} for protos of the same type as {@code defaultInstance}. */
public static <T extends MessageLite> Marshaller<T> marshaller(final T defaultInstance) {
    @SuppressWarnings("unchecked") final Parser<T> parser = (Parser<T>) defaultInstance.getParserForType();
    // TODO(ejona): consider changing return type to PrototypeMarshaller (assuming ABI safe)
    return new PrototypeMarshaller<T>() {

        @SuppressWarnings("unchecked")
        @Override
        public Class<T> getMessageClass() {
            // Precisely T since protobuf doesn't let messages extend other messages.
            return (Class<T>) defaultInstance.getClass();
        }

        @Override
        public T getMessagePrototype() {
            return defaultInstance;
        }

        @Override
        public InputStream stream(T value) {
            return new ProtoInputStream(value, parser);
        }

        @Override
        public T parse(InputStream stream) {
            if (stream instanceof ProtoInputStream) {
                ProtoInputStream protoStream = (ProtoInputStream) stream;
                // to enable this optimization.
                if (protoStream.parser() == parser) {
                    try {
                        @SuppressWarnings("unchecked") T message = (T) ((ProtoInputStream) stream).message();
                        return message;
                    } catch (IllegalStateException ex) {
                    // Stream must have been read from, which is a strange state. Since the point of this
                    // optimization is to be transparent, instead of throwing an error we'll continue,
                    // even though it seems likely there's a bug.
                    }
                }
            }
            CodedInputStream cis = null;
            try {
                if (stream instanceof KnownLength) {
                    int size = stream.available();
                    if (size > 0 && size <= GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE) {
                        // buf should not be used after this method has returned.
                        byte[] buf = bufs.get().get();
                        if (buf == null || buf.length < size) {
                            buf = new byte[size];
                            bufs.set(new WeakReference<byte[]>(buf));
                        }
                        int chunkSize;
                        int position = 0;
                        while ((chunkSize = stream.read(buf, position, size - position)) != -1) {
                            position += chunkSize;
                        }
                        if (size != position) {
                            throw new RuntimeException("size inaccurate: " + size + " != " + position);
                        }
                        cis = CodedInputStream.newInstance(buf, 0, size);
                    } else if (size == 0) {
                        return defaultInstance;
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            if (cis == null) {
                cis = CodedInputStream.newInstance(stream);
            }
            // Pre-create the CodedInputStream so that we can remove the size limit restriction
            // when parsing.
            cis.setSizeLimit(Integer.MAX_VALUE);
            try {
                return parseFrom(cis);
            } catch (InvalidProtocolBufferException ipbe) {
                throw Status.INTERNAL.withDescription("Invalid protobuf byte sequence").withCause(ipbe).asRuntimeException();
            }
        }

        private T parseFrom(CodedInputStream stream) throws InvalidProtocolBufferException {
            T message = parser.parseFrom(stream, globalRegistry);
            try {
                stream.checkLastTagWas(0);
                return message;
            } catch (InvalidProtocolBufferException e) {
                e.setUnfinishedMessage(message);
                throw e;
            }
        }
    };
}
Also used : CodedInputStream(com.google.protobuf.CodedInputStream) InputStream(java.io.InputStream) CodedInputStream(com.google.protobuf.CodedInputStream) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException) PrototypeMarshaller(io.grpc.MethodDescriptor.PrototypeMarshaller) KnownLength(io.grpc.KnownLength) Parser(com.google.protobuf.Parser)

Example 12 with CodedInputStream

use of com.google.protobuf.CodedInputStream in project heron by twitter.

the class ExtraActionUtils method getExtraActionInfo.

public static ExtraActionInfo getExtraActionInfo(String extraActionFile) {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    ExtraActionsBase.registerAllExtensions(registry);
    try (InputStream stream = Files.newInputStream(Paths.get(extraActionFile))) {
        CodedInputStream coded = CodedInputStream.newInstance(stream);
        return ExtraActionInfo.parseFrom(coded, registry);
    } catch (IOException e) {
        throw new RuntimeException("ERROR: failed to deserialize extra action file " + extraActionFile + ": " + e.getMessage(), e);
    }
}
Also used : CodedInputStream(com.google.protobuf.CodedInputStream) InputStream(java.io.InputStream) CodedInputStream(com.google.protobuf.CodedInputStream) IOException(java.io.IOException) ExtensionRegistry(com.google.protobuf.ExtensionRegistry)

Example 13 with CodedInputStream

use of com.google.protobuf.CodedInputStream in project hadoop by apache.

the class BlockListAsLongs method readFrom.

public static BlockListAsLongs readFrom(InputStream is, int maxDataLength) throws IOException {
    CodedInputStream cis = CodedInputStream.newInstance(is);
    if (maxDataLength != IPC_MAXIMUM_DATA_LENGTH_DEFAULT) {
        cis.setSizeLimit(maxDataLength);
    }
    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, maxDataLength);
    }
    return null;
}
Also used : CodedInputStream(com.google.protobuf.CodedInputStream) ByteString(com.google.protobuf.ByteString)

Example 14 with CodedInputStream

use of com.google.protobuf.CodedInputStream in project compiler by boalang.

the class BoaAstIntrinsics method getissues.

/**
	 * Given an IssueRepository, return the issues.
	 *
	 * @param f the IssueRepository to get issues for
	 * @return the issues list, or an empty list on any sort of error
	 */
@FunctionSpec(name = "getissues", returnType = "IssuesRoot", formalParameters = { "IssueRepository" })
public static IssuesRoot getissues(final IssueRepository f) {
    if (issuesMap == null)
        openIssuesMap();
    try {
        final BytesWritable value = new BytesWritable();
        if (issuesMap.get(new Text(f.getKey()), value) != null) {
            final CodedInputStream _stream = CodedInputStream.newInstance(value.getBytes(), 0, value.getLength());
            final IssuesRoot root = IssuesRoot.parseFrom(_stream);
            return root;
        }
    } catch (final InvalidProtocolBufferException e) {
        e.printStackTrace();
    } catch (final IOException e) {
        e.printStackTrace();
    } catch (final RuntimeException e) {
        e.printStackTrace();
    } catch (final Error e) {
        e.printStackTrace();
    }
    System.err.println("error with issues: " + f.getKey());
    return emptyIssues;
}
Also used : CodedInputStream(com.google.protobuf.CodedInputStream) IssuesRoot(boa.types.Issues.IssuesRoot) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) BytesWritable(org.apache.hadoop.io.BytesWritable) Text(org.apache.hadoop.io.Text) IOException(java.io.IOException)

Example 15 with CodedInputStream

use of com.google.protobuf.CodedInputStream in project compiler by boalang.

the class BoaAstIntrinsics method getast.

/**
	 * Given a ChangedFile, return the AST for that file at that revision.
	 *
	 * @param f the ChangedFile to get a snapshot of the AST for
	 * @return the AST, or an empty AST on any sort of error
	 */
@SuppressWarnings("unchecked")
@FunctionSpec(name = "getast", returnType = "ASTRoot", formalParameters = { "ChangedFile" })
public static ASTRoot getast(final ChangedFile f) {
    // since we know only certain kinds have ASTs, filter before looking up
    final ChangedFile.FileKind kind = f.getKind();
    if (kind != ChangedFile.FileKind.SOURCE_JAVA_ERROR && kind != ChangedFile.FileKind.SOURCE_JAVA_JLS2 && kind != ChangedFile.FileKind.SOURCE_JAVA_JLS3 && kind != ChangedFile.FileKind.SOURCE_JAVA_JLS4 && kind != ChangedFile.FileKind.SOURCE_JAVA_JLS8)
        return emptyAst;
    context.getCounter(AST_COUNTER.GETS_ATTEMPTED).increment(1);
    final String rowName = f.getKey() + "!!" + f.getName();
    if (map == null)
        openMap();
    try {
        final BytesWritable value = new BytesWritable();
        if (map.get(new Text(rowName), value) == null) {
            context.getCounter(AST_COUNTER.GETS_FAIL_MISSING).increment(1);
        } else {
            final CodedInputStream _stream = CodedInputStream.newInstance(value.getBytes(), 0, value.getLength());
            // defaults to 64, really big ASTs require more
            _stream.setRecursionLimit(Integer.MAX_VALUE);
            final ASTRoot root = ASTRoot.parseFrom(_stream);
            context.getCounter(AST_COUNTER.GETS_SUCCEED).increment(1);
            return root;
        }
    } catch (final InvalidProtocolBufferException e) {
        e.printStackTrace();
        context.getCounter(AST_COUNTER.GETS_FAIL_BADPROTOBUF).increment(1);
    } catch (final IOException e) {
        e.printStackTrace();
        context.getCounter(AST_COUNTER.GETS_FAIL_MISSING).increment(1);
    } catch (final RuntimeException e) {
        e.printStackTrace();
        context.getCounter(AST_COUNTER.GETS_FAIL_MISSING).increment(1);
    } catch (final Error e) {
        e.printStackTrace();
        context.getCounter(AST_COUNTER.GETS_FAIL_BADPROTOBUF).increment(1);
    }
    System.err.println("error with ast: " + rowName);
    context.getCounter(AST_COUNTER.GETS_FAILED).increment(1);
    return emptyAst;
}
Also used : CodedInputStream(com.google.protobuf.CodedInputStream) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) BytesWritable(org.apache.hadoop.io.BytesWritable) Text(org.apache.hadoop.io.Text) IOException(java.io.IOException) ChangedFile(boa.types.Diff.ChangedFile)

Aggregations

CodedInputStream (com.google.protobuf.CodedInputStream)26 IOException (java.io.IOException)15 InputStream (java.io.InputStream)8 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)4 ByteString (com.google.protobuf.ByteString)3 ExtensionRegistry (com.google.protobuf.ExtensionRegistry)3 BytesWritable (org.apache.hadoop.io.BytesWritable)3 Text (org.apache.hadoop.io.Text)3 ChangedFile (boa.types.Diff.ChangedFile)2 Protocol (communication.Protocol)2 FileInputStream (java.io.FileInputStream)2 WireMessage (org.apache.calcite.avatica.proto.Common.WireMessage)2 CompiledFile (android.aapt.pb.internal.ResourcesInternal.CompiledFile)1 IssuesRoot (boa.types.Issues.IssuesRoot)1 DynamicMessage (com.google.protobuf.DynamicMessage)1 Parser (com.google.protobuf.Parser)1 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)1 KnownLength (io.grpc.KnownLength)1 PrototypeMarshaller (io.grpc.MethodDescriptor.PrototypeMarshaller)1 BufferedInputStream (java.io.BufferedInputStream)1