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. Also, not
97currently supported are packed repeated elements or
98extensions.
99
100To create a jar file for the runtime and run tests invoke
101"mvn package -P micro" from the <protobuf-root>/java
102directory. The generated jar file is
103<protobuf-root>java/target/protobuf-java-2.2.0-micro.jar.
104
105If you wish to compile the MICRO_RUNTIME your self, place
106the 7 files below, in <root>/com/google/protobuf and
107create a jar file for use with your code and the generated
108code:
109
110ByteStringMicro.java
111CodedInputStreamMicro.java
112CodedOutputStreamMicro.java
113InvalidProtocolBufferException.java
114MessageMicro.java
115WireFormatMicro.java
116
117If you wish to change on the code generator it is located
118in /src/google/protobuf/compiler/javamicro.
119
120To generate code for the MICRO_RUNTIME invoke protoc with
121--javamicro_out command line parameter. javamicro_out takes
122a series of optional sub-parameters separated by commas
123and a final parameter, with a colon separator, which defines
124the source directory. Sub-parameters begin with a name
125followed by an equal and if that sub-parameter has multiple
126parameters they are seperated by "|". The command line options
127are:
128
129opt -> speed or space
130java_use_vector -> true or false
131java_package -> <file-name>|<package-name>
132java_outer_classname -> <file-name>|<package-name>
133java_multiple_files -> true or false
134
135opt={speed,space} (default: space)
136 This changes the code generation to optimize for speed or
137 space. When opt=speed this changes the code generation
138 for strings so that multiple conversions to Utf8 are
139 eliminated.
140
141java_use_vector={true,false} (default: false)
142 This specifies the collection class for repeated elements.
143 If false, repeated elements use java.util.ArrayList<> and
144 the code must be compiled with Java 1.5 or above. If true,
145 repeated elements use java.util.Vector and the code can
146 be compiled with Java 1.3 or above. The 'source'
147 parameter of 'javac' may be used to control the version
148 of the source: "javac -source 1.3". You can also change
149 the <source> xml element for the maven-compiler-plugin.
150 Below is for 1.5 sources:
151
152 <plugin>
153 <artifactId>maven-compiler-plugin</artifactId>
154 <configuration>
155 <source>1.5</source>
156 <target>1.5</target>
157 </configuration>
158 </plugin>
159
160 And below would be for 1.3 sources (note when changing
161 to 1.3 you must also set java_use_vector=true):
162
163 <plugin>
164 <artifactId>maven-compiler-plugin</artifactId>
165 <configuration>
166 <source>1.3</source>
167 <target>1.5</target>
168 </configuration>
169 </plugin>
170
171java_package=<file-name>|<package-name> (no default)
172 This allows overriding the 'java_package' option value
173 for the given file from the command line. Use multiple
174 java_package options to override the option for multiple
175 files. The final Java package for each file is the value
176 of this command line option if present, or the value of
177 the same option defined in the file if present, or the
178 proto package if present, or the default Java package.
179
180java_outer_classname=<file-name>|<outer-classname> (no default)
181 This allows overriding the 'java_outer_classname' option
182 for the given file from the command line. Use multiple
183 java_outer_classname options to override the option for
184 multiple files. The final Java outer class name for each
185 file is the value of this command line option if present,
186 or the value of the same option defined in the file if
187 present, or the file name converted to CamelCase. This
188 outer class will nest all classes and integer constants
189 generated from file-scope messages and enums.
190
191java_multiple_files={true,false} (no default)
192 This allows overriding the 'java_multiple_files' option
193 in all source files and their imported files from the
194 command line. The final value of this option for each
195 file is the value defined in this command line option, or
196 the value of the same option defined in the file if
197 present, or false. This specifies whether to generate
198 package-level classes for the file-scope messages in the
199 same Java package as the outer class (instead of nested
200 classes in the outer class). File-scope enum constants
201 are still generated as integer constants in the outer
202 class. This affects the fully qualified references in the
203 Java code. NOTE: because the command line option
204 overrides the value for all files and their imported
205 files, using this option inconsistently may result in
206 incorrect references to the imported messages and enum
207 constants.
208
209
210IMPORTANT: change of javamicro_out behavior:
211
212In previous versions, if the outer class name has not been
213given explicitly, javamicro_out would not infer the outer
214class name from the file name, and would skip the outer
215class generation. This makes the compilation succeed only
216if the source file contains a single message and no enums,
217and the generated class for that message is placed at the
218package level. To re-align with java_out, javamicro_out
219will now always generate the outer class, inferring its
220name from the file name if not given, as a container of the
221message classes and enum constants. To keep any existing
222single-message source file from causing the generation of
223an unwanted outer class, you can set the option
224java_multiple_files to true, either in the file or as a
225command line option.
226
227
228Below are a series of examples for clarification of the
229various parameters and options. Assuming this file:
230
231src/proto/simple-data-protos.proto:
232
233 package testprotobuf;
234
235 message SimpleData {
236 optional fixed64 id = 1;
237 optional string description = 2;
238 optional bool ok = 3 [default = false];
239 };
240
241and the compiled protoc in the current working directory,
242then a simple command line to compile this file would be:
243
244./protoc --javamicro_out=. src/proto/simple-data-protos.proto
245
246This will create testprotobuf/SimpleDataProtos.java, which
247has the following content (extremely simplified):
248
249 package testprotobuf;
250
251 public final class SimpleDataProtos {
252 public static final class SimpleData
253 extends MessageMicro {
254 ...
255 }
256 }
257
258The message SimpleData is compiled into the SimpleData
259class, nested in the file's outer class SimpleDataProtos,
260whose name is implicitly defined by the proto file name
261"simple-data-protos".
262
263The directory, aka Java package, testprotobuf is created
264because on line 1 of simple-data-protos.proto is
265"package testprotobuf;". If you wanted a different
266package name you could use the java_package option in the
267file:
268
269 option java_package = "my_package";
270
271or in command line sub-parameter:
272
273./protoc '--javamicro_out=\
274java_package=src/proto/simple-data-protos.proto|my_package:\
275.' src/proto/simple-data-protos.proto
276
277Here you see the new java_package sub-parameter which
278itself needs two parameters the file name and the
279package name, these are separated by "|". The value set
280in the command line overrides the value set in the file.
281Now you'll find SimpleDataProtos.java in the my_package/
282directory.
283
284If you wanted to also change the optimization for
285speed you'd add opt=speed with the comma seperator
286as follows:
287
288./protoc '--javamicro_out=\
289opt=speed,\
290java_package=src/proto/simple-data-protos.proto|my_package:
291.' src/proto/simple-data-protos.proto
292
293If you also wanted a different outer class name you'd
294do the following:
295
296./protoc '--javamicro_out=\
297opt=speed,\
298java_package=src/proto/simple-data-protos.proto|my_package,\
299java_outer_classname=src/proto/simple-data-protos.proto|OuterName:\
300.' src/proto/simple-data-protos.proto
301
302Now you'll find my_package/OuterName.java and the
303message class SimpleData nested in it.
304
305As mentioned java_package, java_outer_classname and
306java_multiple_files may also be specified in the file.
307In the example below we must define
308java_outer_classname because otherwise the outer class
309and one of the message classes will have the same name,
310which is forbidden to prevent name ambiguity:
311
312src/proto/sample-message.proto:
313
314 package testmicroruntime;
315
316 option java_package = "com.example";
317 option java_outer_classname = "SampleMessageProtos";
318
319 enum MessageType {
320 SAMPLE = 1;
321 EXAMPLE = 2;
322 }
323
324 message SampleMessage {
325 required int32 id = 1;
326 required MessageType type = 2;
327 }
328
329 message SampleMessageContainer {
330 required SampleMessage message = 1;
331 }
332
333This could be compiled using:
334
335./protoc --javamicro_out=. src/proto/sample-message.proto
336
337and the output will be:
338
339com/example/SampleMessageProtos.java:
340
341 package com.example;
342
343 public final class SampleMessageProtos {
344 public static final int SAMPLE = 1;
345 public static final int EXAMPLE = 2;
346 public static final class SampleMessage
347 extends MessageMicro {
348 ...
349 }
350 public static final class SampleMessageContainer
351 extends MessageMicro {
352 ...
353 }
354 }
355
356As you can see the file-scope enum MessageType is
357disassembled into two integer constants in the outer class.
358In javamicro_out, all enums are disassembled and compiled
359into integer constants in the parent scope (the containing
360message's class or the file's (i.e. outer) class).
361
362You may prefer the file-scope messages to be saved in
363separate files. You can do this by setting the option
364java_multiple_files to true, in either the file like this:
365
366 option java_multiple_files = true;
367
368or the command line like this:
369
370./protoc --javamicro_out=\
371java_multiple_files=true:\
372. src/proto/sample-message.proto
373
374The java_multiple_files option causes javamicro to use a
375separate file for each file-scope message, which resides
376directly in the Java package alongside the outer class:
377
378com/example/SampleMessageProtos.java:
379
380 package com.example;
381 public final class SampleMessageProtos {
382 public static final int SAMPLE = 1;
383 public static final int EXAMPLE = 2;
384 }
385
386com/example/SampleMessage.java:
387
388 package com.example;
389 public final class SampleMessage
390 extends MessageMicro {
391 ...
392 }
393
394com/example/SampleMessageContainer.java:
395
396 package com.example;
397 public final class SampleMessageContainer
398 extends MessageMicro {
399 ...
400 }
401
402As you can see, the outer class now contains only the
403integer constants, generated from the file-scope enum
404"MessageType". Please note that message-scope enums are
405still generated as integer constants in the message class.
406
407
408Nano version
409============================
410
411Nano is even smaller than micro, especially in the number of generated
412functions. It is like micro except:
413
414- No setter/getter/hazzer functions.
415- Has state is not available. Outputs all fields not equal to their
416 default. (See important implications below.)
417- CodedInputStreamMicro is renamed to CodedInputByteBufferNano and can
418 only take byte[] (not InputStream).
419- Similar rename from CodedOutputStreamMicro to
420 CodedOutputByteBufferNano.
421- Repeated fields are in arrays, not ArrayList or Vector.
422- Unset messages/groups are null, not an immutable empty default
423 instance.
424- Required fields are always serialized.
425- toByteArray(...) and mergeFrom(...) are now static functions of
426 MessageNano.
427- "bytes" are of java type byte[].
428
429IMPORTANT: If you have fields with defaults
430
431How fields with defaults are serialized has changed. Because we don't
432keep "has" state, any field equal to its default is assumed to be not
433set and therefore is not serialized. Consider the situation where we
434change the default value of a field. Senders compiled against an older
435version of the proto continue to match against the old default, and
436don't send values to the receiver even though the receiver assumes the
437new default value. Therefore, think carefully about the implications
438of changing the default value.
439
440IMPORTANT: If you have "bytes" fields with non-empty defaults
441
442Because the byte buffer is now of mutable type byte[], the default
443static final cannot be exposed through a public field. Each time a
444message's constructor or clear() function is called, the default value
445(kept in a private byte[]) is cloned. This causes a small memory
446penalty. This is not a problem if the field has no default or is an
447empty default.
448
449Nano Generator options
450
451java_package -> <file-name>|<package-name>
452java_outer_classname -> <file-name>|<package-name>
453java_multiple_files -> true or false
454java_nano_generate_has -> true or false
455
456java_package:
457java_outer_classname:
458java_multiple_files:
459 Same as Micro version.
460
461java_nano_generate_has={true,false} (default: false)
462 If true, generates a public boolean variable has<fieldname>
463 accompanying each optional or required field (not present for
464 repeated fields, groups or messages). It is set to false initially
465 and upon clear(). If parseFrom(...) reads the field from the wire,
466 it is set to true. This is a way for clients to inspect the "has"
467 value upon parse. If it is set to true, writeTo(...) will ALWAYS
468 output that field (even if field value is equal to its
469 default).
470
471 IMPORTANT: This option costs an extra 4 bytes per primitive field in
472 the message. Think carefully about whether you really need this. In
473 many cases reading the default works and determining whether the
474 field was received over the wire is irrelevant.
475
476To use nano protobufs:
477
478- Link with the generated jar file
479 <protobuf-root>java/target/protobuf-java-2.3.0-nano.jar.
480- Invoke with --javanano_out, e.g.:
481
482./protoc '--javanano_out=\
483java_package=src/proto/simple-data.proto|my_package,\
484java_outer_classname=src/proto/simple-data.proto|OuterName:\
485.' src/proto/simple-data.proto
486
487Contributing to nano:
488
489Please add/edit tests in NanoTest.java.
490
491Please run the following steps to test:
492
493- cd external/protobuf
494- ./configure
495- Run "make -j12 check" and verify all tests pass.
496- cd java
497- Run "mvn test" and verify all tests pass.
498- cd ../../..
499- . build/envsetup.sh
500- lunch 1
501- "make -j12 aprotoc libprotobuf-java-2.3.0-nano aprotoc-test-nano-params" and
502 check for build errors.
503- repo sync -c -j256
504- "make -j12" and check for build errors
505
506
507Usage
508=====
509
510The complete documentation for Protocol Buffers is available via the
511web at:
512
513 http://code.google.com/apis/protocolbuffers/
514