1fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Protocol Buffers - Google's data interchange format
2fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Copyright 2008 Google Inc.  All rights reserved.
3fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// http://code.google.com/p/protobuf/
4fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
5fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Redistribution and use in source and binary forms, with or without
6fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// modification, are permitted provided that the following conditions are
7fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// met:
8fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
9fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Redistributions of source code must retain the above copyright
10fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// notice, this list of conditions and the following disclaimer.
11fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Redistributions in binary form must reproduce the above
12fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// copyright notice, this list of conditions and the following disclaimer
13fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// in the documentation and/or other materials provided with the
14fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// distribution.
15fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Neither the name of Google Inc. nor the names of its
16fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// contributors may be used to endorse or promote products derived from
17fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// this software without specific prior written permission.
18fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
19fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
31fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillepackage com.google.protobuf;
32fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
33fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleimport java.io.UnsupportedEncodingException;
34fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
35fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville/**
36fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville * The classes contained within are used internally by the Protocol Buffer
37fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville * library and generated message implementations. They are public only because
38fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville * those generated messages do not reside in the {@code protobuf} package.
39fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville * Others should not use this class directly.
40fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville *
41fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville * @author kenton@google.com (Kenton Varda)
42fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville */
43fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillepublic class Internal {
44fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  /**
45fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * Helper called by generated code to construct default values for string
46fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * fields.
47fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * <p>
48fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * The protocol compiler does not actually contain a UTF-8 decoder -- it
49fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * just pushes UTF-8-encoded text around without touching it.  The one place
50fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * where this presents a problem is when generating Java string literals.
51fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * Unicode characters in the string literal would normally need to be encoded
52fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * using a Unicode escape sequence, which would require decoding them.
53fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * To get around this, protoc instead embeds the UTF-8 bytes into the
54fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * generated code and leaves it to the runtime library to decode them.
55fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * <p>
56fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * It gets worse, though.  If protoc just generated a byte array, like:
57fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   *   new byte[] {0x12, 0x34, 0x56, 0x78}
58fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * Java actually generates *code* which allocates an array and then fills
59fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * in each value.  This is much less efficient than just embedding the bytes
60fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * directly into the bytecode.  To get around this, we need another
61fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * work-around.  String literals are embedded directly, so protoc actually
62fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * generates a string literal corresponding to the bytes.  The easiest way
63fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * to do this is to use the ISO-8859-1 character set, which corresponds to
64fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * the first 256 characters of the Unicode range.  Protoc can then use
65fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * good old CEscape to generate the string.
66fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * <p>
67fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * So we have a string literal which represents a set of bytes which
68fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * represents another string.  This function -- stringDefaultValue --
69fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * converts from the generated string to the string we actually want.  The
70fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * generated code calls this automatically.
71fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   */
72fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  public static String stringDefaultValue(String bytes) {
73fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    try {
74fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      return new String(bytes.getBytes("ISO-8859-1"), "UTF-8");
75fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    } catch (UnsupportedEncodingException e) {
76fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      // This should never happen since all JVMs are required to implement
77fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      // both of the above character sets.
78fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      throw new IllegalStateException(
79fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville          "Java VM does not support a standard character set.", e);
80fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    }
81fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
82fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
83fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  /**
84fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * Helper called by generated code to construct default values for bytes
85fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * fields.
86fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * <p>
87fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * This is a lot like {@link #stringDefaultValue}, but for bytes fields.
88fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * In this case we only need the second of the two hacks -- allowing us to
89fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * embed raw bytes as a string literal with ISO-8859-1 encoding.
90fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   */
91fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  public static ByteString bytesDefaultValue(String bytes) {
92fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    try {
93fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      return ByteString.copyFrom(bytes.getBytes("ISO-8859-1"));
94fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    } catch (UnsupportedEncodingException e) {
95fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      // This should never happen since all JVMs are required to implement
96fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      // ISO-8859-1.
97fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      throw new IllegalStateException(
98fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville          "Java VM does not support a standard character set.", e);
99fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    }
100fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
101fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
102fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  /**
103fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * Interface for an enum value or value descriptor, to be used in FieldSet.
104fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * The lite library stores enum values directly in FieldSets but the full
105fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * library stores EnumValueDescriptors in order to better support reflection.
106fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   */
107fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  public interface EnumLite {
108fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    int getNumber();
109fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
110fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
111fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  /**
112fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * Interface for an object which maps integers to {@link EnumLite}s.
113fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * {@link Descriptors.EnumDescriptor} implements this interface by mapping
114fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * numbers to {@link Descriptors.EnumValueDescriptor}s.  Additionally,
115fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * every generated enum type has a static method internalGetValueMap() which
116fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   * returns an implementation of this type that maps numbers to enum values.
117fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville   */
118fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  public interface EnumLiteMap<T extends EnumLite> {
119fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    T findValueByNumber(int number);
120fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
121fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
122