README.txt revision 26266cd4660ffe1f3d6015b715713ee654c5b936
1Protocol Buffers - Google's data interchange format
2Copyright 2008 Google Inc.
3
4This directory contains the Java Protocol Buffers runtime library.
5
6Installation - With Maven
7=========================
8
9The Protocol Buffers build is managed using Maven.  If you would
10rather build without Maven, see below.
11
121) Install Apache Maven if you don't have it:
13
14     http://maven.apache.org/
15
162) Build the C++ code, or obtain a binary distribution of protoc.  If
17   you install a binary distribution, make sure that it is the same
18   version as this package.  If in doubt, run:
19
20     $ protoc --version
21
22   You will need to place the protoc executable in ../src.  (If you
23   built it yourself, it should already be there.)
24
253) Run the tests:
26
27     $ mvn test
28
29   If some tests fail, this library may not work correctly on your
30   system.  Continue at your own risk.
31
324) Install the library into your Maven repository:
33
34     $ mvn install
35
365) If you do not use Maven to manage your own build, you can build a
37   .jar file to use:
38
39     $ mvn package
40
41   The .jar will be placed in the "target" directory.
42
43Installation - 'Lite' Version - With Maven
44==========================================
45
46Building the 'lite' version of the Java Protocol Buffers library is
47the same as building the full version, except that all commands are
48run using the 'lite' profile.  (see
49http://maven.apache.org/guides/introduction/introduction-to-profiles.html)
50
51E.g. to install the lite version of the jar, you would run:
52
53  $ mvn install -P lite
54
55The resulting artifact has the 'lite' classifier.  To reference it
56for dependency resolution, you would specify it as:
57
58  <dependency>
59    <groupId>com.google.protobuf</groupId>
60    <artifactId>protobuf-java</artifactId>
61    <version>${version}</version>
62    <classifier>lite</classifier>
63  </dependency>
64
65Installation - Without Maven
66============================
67
68If you would rather not install Maven to build the library, you may
69follow these instructions instead.  Note that these instructions skip
70running unit tests.
71
721) Build the C++ code, or obtain a binary distribution of protoc.  If
73   you install a binary distribution, make sure that it is the same
74   version as this package.  If in doubt, run:
75
76     $ protoc --version
77
78   If you built the C++ code without installing, the compiler binary
79   should be located in ../src.
80
812) Invoke protoc to build DescriptorProtos.java:
82
83     $ protoc --java_out=src/main/java -I../src \
84         ../src/google/protobuf/descriptor.proto
85
863) Compile the code in src/main/java using whatever means you prefer.
87
884) Install the classes wherever you prefer.
89
90Micro version
91============================
92
93The runtime and generated code for MICRO_RUNTIME is smaller
94because it does not include support for the descriptor and
95reflection, and enums are generated as integer constants in
96the parent message or the file's outer class, with no
97protection against invalid values set to enum fields. Also,
98not currently supported are packed repeated elements or
99extensions.
100
101To create a jar file for the runtime and run tests invoke
102"mvn package -P micro" from the <protobuf-root>/java
103directory. The generated jar file is
104<protobuf-root>java/target/protobuf-java-2.2.0-micro.jar.
105
106If you wish to compile the MICRO_RUNTIME your self, place
107the 7 files below, in <root>/com/google/protobuf and
108create a jar file for use with your code and the generated
109code:
110
111ByteStringMicro.java
112CodedInputStreamMicro.java
113CodedOutputStreamMicro.java
114InvalidProtocolBufferException.java
115MessageMicro.java
116WireFormatMicro.java
117
118If you wish to change on the code generator it is located
119in /src/google/protobuf/compiler/javamicro.
120
121To generate code for the MICRO_RUNTIME invoke protoc with
122--javamicro_out command line parameter. javamicro_out takes
123a series of optional sub-parameters separated by commas
124and a final parameter, with a colon separator, which defines
125the source directory. Sub-parameters begin with a name
126followed by an equal and if that sub-parameter has multiple
127parameters they are seperated by "|". The command line options
128are:
129
130opt                  -> speed or space
131java_use_vector      -> true or false
132java_package         -> <file-name>|<package-name>
133java_outer_classname -> <file-name>|<package-name>
134java_multiple_files  -> true or false
135
136opt={speed,space} (default: space)
137  This changes the code generation to optimize for speed or
138  space. When opt=speed this changes the code generation
139  for strings so that multiple conversions to Utf8 are
140  eliminated.
141
142java_use_vector={true,false} (default: false)
143  This specifies the collection class for repeated elements.
144  If false, repeated elements use java.util.ArrayList<> and
145  the code must be compiled with Java 1.5 or above. If true,
146  repeated elements use java.util.Vector and the code can
147  be compiled with Java 1.3 or above. The 'source'
148  parameter of 'javac' may be used to control the version
149  of the source: "javac -source 1.3". You can also change
150  the <source> xml element for the maven-compiler-plugin.
151  Below is for 1.5 sources:
152
153      <plugin>
154        <artifactId>maven-compiler-plugin</artifactId>
155        <configuration>
156          <source>1.5</source>
157          <target>1.5</target>
158        </configuration>
159      </plugin>
160
161  And below would be for 1.3 sources (note when changing
162  to 1.3 you must also set java_use_vector=true):
163
164      <plugin>
165        <artifactId>maven-compiler-plugin</artifactId>
166        <configuration>
167          <source>1.3</source>
168          <target>1.5</target>
169        </configuration>
170      </plugin>
171
172java_package=<file-name>|<package-name> (no default)
173  This allows overriding the 'java_package' option value
174  for the given file from the command line. Use multiple
175  java_package options to override the option for multiple
176  files. The final Java package for each file is the value
177  of this command line option if present, or the value of
178  the same option defined in the file if present, or the
179  proto package if present, or the default Java package.
180
181java_outer_classname=<file-name>|<outer-classname> (no default)
182  This allows overriding the 'java_outer_classname' option
183  for the given file from the command line. Use multiple
184  java_outer_classname options to override the option for
185  multiple files. The final Java outer class name for each
186  file is the value of this command line option if present,
187  or the value of the same option defined in the file if
188  present, or the file name converted to CamelCase. This
189  outer class will nest all classes and integer constants
190  generated from file-scope messages and enums.
191
192java_multiple_files={true,false} (no default)
193  This allows overriding the 'java_multiple_files' option
194  in all source files and their imported files from the
195  command line. The final value of this option for each
196  file is the value defined in this command line option, or
197  the value of the same option defined in the file if
198  present, or false. This specifies whether to generate
199  package-level classes for the file-scope messages in the
200  same Java package as the outer class (instead of nested
201  classes in the outer class). File-scope enum constants
202  are still generated as integer constants in the outer
203  class. This affects the fully qualified references in the
204  Java code. NOTE: because the command line option
205  overrides the value for all files and their imported
206  files, using this option inconsistently may result in
207  incorrect references to the imported messages and enum
208  constants.
209
210
211IMPORTANT: change of javamicro_out behavior:
212
213In previous versions, if the outer class name has not been
214given explicitly, javamicro_out would not infer the outer
215class name from the file name, and would skip the outer
216class generation. This makes the compilation succeed only
217if the source file contains a single message and no enums,
218and the generated class for that message is placed at the
219package level. To re-align with java_out, javamicro_out
220will now always generate the outer class, inferring its
221name from the file name if not given, as a container of the
222message classes and enum constants. To keep any existing
223single-message source file from causing the generation of
224an unwanted outer class, you can set the option
225java_multiple_files to true, either in the file or as a
226command line option.
227
228
229Below are a series of examples for clarification of the
230various parameters and options. Assuming this file:
231
232src/proto/simple-data-protos.proto:
233
234    package testprotobuf;
235
236    message SimpleData {
237      optional fixed64 id = 1;
238      optional string description = 2;
239      optional bool ok = 3 [default = false];
240    };
241
242and the compiled protoc in the current working directory,
243then a simple command line to compile this file would be:
244
245./protoc --javamicro_out=. src/proto/simple-data-protos.proto
246
247This will create testprotobuf/SimpleDataProtos.java, which
248has the following content (extremely simplified):
249
250    package testprotobuf;
251
252    public final class SimpleDataProtos {
253      public static final class SimpleData
254          extends MessageMicro {
255        ...
256      }
257    }
258
259The message SimpleData is compiled into the SimpleData
260class, nested in the file's outer class SimpleDataProtos,
261whose name is implicitly defined by the proto file name
262"simple-data-protos".
263
264The directory, aka Java package, testprotobuf is created
265because on line 1 of simple-data-protos.proto is
266"package testprotobuf;". If you wanted a different
267package name you could use the java_package option in the
268file:
269
270    option java_package = "my_package";
271
272or in command line sub-parameter:
273
274./protoc '--javamicro_out=\
275java_package=src/proto/simple-data-protos.proto|my_package:\
276.' src/proto/simple-data-protos.proto
277
278Here you see the new java_package sub-parameter which
279itself needs two parameters the file name and the
280package name, these are separated by "|". The value set
281in the command line overrides the value set in the file.
282Now you'll find SimpleDataProtos.java in the my_package/
283directory.
284
285If you wanted to also change the optimization for
286speed you'd add opt=speed with the comma seperator
287as follows:
288
289./protoc '--javamicro_out=\
290opt=speed,\
291java_package=src/proto/simple-data-protos.proto|my_package:
292.' src/proto/simple-data-protos.proto
293
294If you also wanted a different outer class name you'd
295do the following:
296
297./protoc '--javamicro_out=\
298opt=speed,\
299java_package=src/proto/simple-data-protos.proto|my_package,\
300java_outer_classname=src/proto/simple-data-protos.proto|OuterName:\
301.' src/proto/simple-data-protos.proto
302
303Now you'll find my_package/OuterName.java and the
304message class SimpleData nested in it.
305
306As mentioned java_package, java_outer_classname and
307java_multiple_files may also be specified in the file.
308In the example below we must define
309java_outer_classname because otherwise the outer class
310and one of the message classes will have the same name,
311which is forbidden to prevent name ambiguity:
312
313src/proto/sample-message.proto:
314
315    package testmicroruntime;
316
317    option java_package = "com.example";
318    option java_outer_classname = "SampleMessageProtos";
319
320    enum MessageType {
321      SAMPLE = 1;
322      EXAMPLE = 2;
323    }
324
325    message SampleMessage {
326      required int32 id = 1;
327      required MessageType type = 2;
328    }
329
330    message SampleMessageContainer {
331      required SampleMessage message = 1;
332    }
333
334This could be compiled using:
335
336./protoc --javamicro_out=. src/proto/sample-message.proto
337
338and the output will be:
339
340com/example/SampleMessageProtos.java:
341
342    package com.example;
343
344    public final class SampleMessageProtos {
345      public static final int SAMPLE = 1;
346      public static final int EXAMPLE = 2;
347      public static final class SampleMessage
348          extends MessageMicro {
349        ...
350      }
351      public static final class SampleMessageContainer
352          extends MessageMicro {
353        ...
354      }
355    }
356
357As you can see the file-scope enum MessageType is
358disassembled into two integer constants in the outer class.
359In javamicro_out, all enums are disassembled and compiled
360into integer constants in the parent scope (the containing
361message's class or the file's (i.e. outer) class).
362
363You may prefer the file-scope messages to be saved in
364separate files. You can do this by setting the option
365java_multiple_files to true, in either the file like this:
366
367    option java_multiple_files = true;
368
369or the command line like this:
370
371./protoc --javamicro_out=\
372java_multiple_files=true:\
373. src/proto/sample-message.proto
374
375The java_multiple_files option causes javamicro to use a
376separate file for each file-scope message, which resides
377directly in the Java package alongside the outer class:
378
379com/example/SampleMessageProtos.java:
380
381    package com.example;
382    public final class SampleMessageProtos {
383      public static final int SAMPLE = 1;
384      public static final int EXAMPLE = 2;
385    }
386
387com/example/SampleMessage.java:
388
389    package com.example;
390    public final class SampleMessage
391        extends MessageMicro {
392      ...
393    }
394
395com/example/SampleMessageContainer.java:
396
397    package com.example;
398    public final class SampleMessageContainer
399        extends MessageMicro {
400      ...
401    }
402
403As you can see, the outer class now contains only the
404integer constants, generated from the file-scope enum
405"MessageType". Please note that message-scope enums are
406still generated as integer constants in the message class.
407
408
409Nano version
410============================
411
412Nano is even smaller than micro, especially in the number of generated
413functions. It is like micro:
414
415- No support for descriptors and reflection;
416- Enum constants are integers with no protection against invalid
417  values set to enum fields.
418
419Except:
420
421- Setter/getter/hazzer/clearer functions are opt-in.
422- If not opted in, has state is not available. Serialization outputs
423  all fields not equal to their default. (See important implications
424  below.)
425- Enum constants can be generated into container interfaces bearing
426  the enum's name (so the referencing code is in Java style).
427- CodedInputStreamMicro is renamed to CodedInputByteBufferNano and can
428  only take byte[] (not InputStream).
429- Similar rename from CodedOutputStreamMicro to
430  CodedOutputByteBufferNano.
431- Repeated fields are in arrays, not ArrayList or Vector.
432- Unset messages/groups are null, not an immutable empty default
433  instance.
434- Required fields are always serialized.
435- toByteArray(...) and mergeFrom(...) are now static functions of
436  MessageNano.
437- "bytes" are of java type byte[].
438
439IMPORTANT: If you have fields with defaults and opt out of accessors
440
441How fields with defaults are serialized has changed. Because we don't
442keep "has" state, any field equal to its default is assumed to be not
443set and therefore is not serialized. Consider the situation where we
444change the default value of a field. Senders compiled against an older
445version of the proto continue to match against the old default, and
446don't send values to the receiver even though the receiver assumes the
447new default value. Therefore, think carefully about the implications
448of changing the default value. Alternatively, turn on accessors and
449enjoy the benefit of the explicit has() checks.
450
451IMPORTANT: If you have "bytes" fields with non-empty defaults
452
453Because the byte buffer is now of mutable type byte[], the default
454static final cannot be exposed through a public field. Each time a
455message's constructor or clear() function is called, the default value
456(kept in a private byte[]) is cloned. This causes a small memory
457penalty. This is not a problem if the field has no default or is an
458empty default.
459
460Nano Generator options
461
462java_package           -> <file-name>|<package-name>
463java_outer_classname   -> <file-name>|<package-name>
464java_multiple_files    -> true or false
465java_nano_generate_has -> true or false [DEPRECATED]
466optional_field_style   -> default or accessors
467
468java_package:
469java_outer_classname:
470java_multiple_files:
471  Same as Micro version.
472
473java_nano_generate_has={true,false} (default: false)
474  DEPRECATED. Use optional_field_style=accessors.
475
476  If true, generates a public boolean variable has<fieldname>
477  accompanying each optional or required field (not present for
478  repeated fields, groups or messages). It is set to false initially
479  and upon clear(). If parseFrom(...) reads the field from the wire,
480  it is set to true. This is a way for clients to inspect the "has"
481  value upon parse. If it is set to true, writeTo(...) will ALWAYS
482  output that field (even if field value is equal to its
483  default).
484
485  IMPORTANT: This option costs an extra 4 bytes per primitive field in
486  the message. Think carefully about whether you really need this. In
487  many cases reading the default works and determining whether the
488  field was received over the wire is irrelevant.
489
490optional_field_style={default,accessors} (default: default)
491  Defines the style of the generated code for _optional_ fields only.
492  In the default style, optional fields translate into public mutable
493  Java fields, and the serialization process is as discussed in the
494  "IMPORTANT" section above. When set to 'accessors', each optional
495  field is encapsulated behind 4 accessors, namely get<fieldname>(),
496  set<fieldname>(), has<fieldname>() and clear<fieldname>() methods,
497  with the standard semantics. The hazzer's return value determines
498  whether a field is serialized, so this style is useful when you need
499  to serialize a field with the default value, or check if a field has
500  been explicitly set to its default value from the wire.
501
502  Required fields are still translated to one public mutable Java
503  field each, and repeated fields are still translated to arrays. No
504  accessors are generated for them.
505
506  optional_field_style=accessors cannot be used together with
507  java_nano_generate_has=true. If you need the 'has' flag for any
508  required field (you have no reason to), you can only use
509  java_nano_generate_has=true.
510
511  IMPORTANT: When using the 'accessor' style, ProGuard should always
512  be enabled with optimization (don't use -dontoptimize) and allowing
513  access modification (use -allowaccessmodification). This removes the
514  unused accessors and maybe inline the rest at the call sites,
515  reducing the final code size.
516  TODO(maxtroy): find ProGuard config that would work the best.
517
518To use nano protobufs:
519
520- Link with the generated jar file
521  <protobuf-root>java/target/protobuf-java-2.3.0-nano.jar.
522- Invoke with --javanano_out, e.g.:
523
524./protoc '--javanano_out=\
525java_package=src/proto/simple-data.proto|my_package,\
526java_outer_classname=src/proto/simple-data.proto|OuterName:\
527.' src/proto/simple-data.proto
528
529Contributing to nano:
530
531Please add/edit tests in NanoTest.java.
532
533Please run the following steps to test:
534
535- cd external/protobuf
536- ./configure
537- Run "make -j12 check" and verify all tests pass.
538- cd java
539- Run "mvn test" and verify all tests pass.
540- cd ../../..
541- . build/envsetup.sh
542- lunch 1
543- "make -j12 aprotoc libprotobuf-java-2.3.0-nano aprotoc-test-nano-params" and
544  check for build errors.
545- repo sync -c -j256
546- "make -j12" and check for build errors
547
548
549Usage
550=====
551
552The complete documentation for Protocol Buffers is available via the
553web at:
554
555  http://code.google.com/apis/protocolbuffers/
556