// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// http://code.google.com/p/protobuf/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package com.google.protobuf.micro;

import org.json.JSONException;
import org.json.JSONStringer;

import java.io.IOException;

/**
 * Abstract interface implemented by Protocol Message objects.
 *
 * @author wink@google.com Wink Saville
 */
public abstract class MessageMicro {
    /**
     * Get the number of bytes required to encode this message.
     * Returns the cached size or calls getSerializedSize which
     * sets the cached size. This is used internally when serializing
     * so the size is only computed once. If a member is modified
     * then this could be stale call getSerializedSize if in doubt.
     */
    abstract public int getCachedSize();

    /**
     * Computes the number of bytes required to encode this message.
     * The size is cached and the cached result can be retrieved
     * using getCachedSize().
     */
    abstract public int getSerializedSize();

    /**
     * Serializes the message and writes it to {@code output}.  This does not
     * flush or close the stream.
     */
    abstract public void writeTo(CodedOutputStreamMicro output) throws java.io.IOException;

    /**
     * Parse {@code input} as a message of this type and merge it with the
     * message being built.
     */
    abstract public MessageMicro mergeFrom(final CodedInputStreamMicro input) throws IOException;

    /**
     * Serialize to a byte array.
     *
     * @return byte array with the serialized data.
     */
    public byte[] toByteArray() {
        final byte[] result = new byte[getSerializedSize()];
        toByteArray(result, 0, result.length);
        return result;
    }

    /**
     * Serialize to a byte array starting at offset through length. The
     * method getSerializedSize must have been called prior to calling
     * this method so the proper length is know.  If an attempt to
     * write more than length bytes OutOfSpaceException will be thrown
     * and if length bytes are not written then IllegalStateException
     * is thrown.
     *
     * @return byte array with the serialized data.
     */
    public void toByteArray(byte[] data, int offset, int length) {
        try {
            final CodedOutputStreamMicro output = CodedOutputStreamMicro.newInstance(data, offset, length);
            writeTo(output);
            output.checkNoSpaceLeft();
        } catch (IOException e) {
            throw new RuntimeException("Serializing to a byte array threw an IOException "
                    + "(should never happen).");
        }
    }

    /**
     * Parse {@code data} as a message of this type and merge it with the
     * message being built.
     */
    public MessageMicro mergeFrom(final byte[] data) throws InvalidProtocolBufferMicroException {
        return mergeFrom(data, 0, data.length);
    }

    /**
     * Parse {@code data} as a message of this type and merge it with the
     * message being built.
     */
    public MessageMicro mergeFrom(final byte[] data, final int off, final int len)
            throws InvalidProtocolBufferMicroException {
        try {
            final CodedInputStreamMicro input = CodedInputStreamMicro.newInstance(data, off, len);
            mergeFrom(input);
            input.checkLastTagWas(0);
            return this;
        } catch (InvalidProtocolBufferMicroException e) {
            throw e;
        } catch (IOException e) {
            throw new RuntimeException("Reading from a byte array threw an IOException (should "
                    + "never happen).");
        }
    }

    /**
     * Called by subclasses to parse an unknown field.
     *
     * @return {@code true} unless the tag is an end-group tag.
     */
    protected boolean parseUnknownField(
            final CodedInputStreamMicro input,
            final int tag) throws IOException {
        return input.skipField(tag);
    }

    public String toJSON() throws JSONException {
        JSONStringer stringer = new JSONStringer();
        stringer.object();
        stringer.endObject();
        return stringer.toString();
    }
}

@@ -1687,30 +1687,25 @@
 ort 
-org.json.JSONException
+android.os.Bundle
 ;%0Aim
@@ -1722,24 +1722,25 @@
 son.JSON
-Stringer
+Exception
 ;%0A%0Aimpor
@@ -5463,24 +5463,147 @@
 ag);%0A    %7D%0A%0A
+    /**%0A     * %E5%B0%86 protobuf %E5%AE%9E%E4%BD%93%E8%BD%AC%E6%8D%A2%E4%B8%BA JSON %E5%AD%97%E7%AC%A6%E4%B8%B2%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%96%B9%E6%B3%95%E3%80%82%0A     * %E6%B3%A8%EF%BC%9A%E9%BB%98%E8%AE%A4%E6%B2%A1%E6%9C%89%E5%AE%9E%E7%8E%B0%EF%BC%81%0A     *%0A     * @return%0A     * @throws JSONException%0A     */%0A
     public S
@@ -5652,56 +5652,55 @@
     
-JSONStringer stringer = new JSONStringer
+throw new UnsupportedOperationException
 ();%0A    
     
@@ -5699,93 +5699,201 @@
     
+%7D%0A%0A
     
-stringer.object();
+/**%0A     * %E5%B0%86 protobuf %E5%AE%9E%E4%BD%93%E8%BD%AC%E6%8D%A2%E4%B8%BA %7B@link android.os.Bundle%7D %E5%AF%B9%E8%B1%A1%E7%9A%84%E6%96%B9%E6%B3%95%E3%80%82%0A     * %E6%B3%A8%EF%BC%9A%E9%BB%98%E8%AE%A4%E6%B2%A1%E6%9C%89%E5%AE%9E%E7%8E%B0%EF%BC%81
 %0A     
+*%0A
    
-stringer.endObject();%0A        return stringer.toString
+  * @return%0A     */%0A    public Bundle toBundle() %7B%0A        throw new UnsupportedOperationException
 ();%0A

添加对 Android Bundle 的支持