1 Protocol Buffers - Google's data interchange format
2 Copyright 2008 Google Inc.
3
4 This directory contains the Java Protocol Buffers runtime library.
5
6 Installation - With Maven
7 =========================
8
9 The Protocol Buffers build is managed using Maven. If you would
10 rather build without Maven, see below.
11
12 1) Install Apache Maven if you don't have it:
13
14 http://maven.apache.org/
15
16 2) 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
25 3) 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
32 4) Install the library into your Maven repository:
33
34 $ mvn install
35
36 5) 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
43 Installation - 'Lite' Version - With Maven
44 ==========================================
45
46 Building the 'lite' version of the Java Protocol Buffers library is
47 the same as building the full version, except that all commands are
48 run using the 'lite' profile. (see
49 http://maven.apache.org/guides/introduction/introduction-to-profiles.html)
50
51 E.g. to install the lite version of the jar, you would run:
52
53 $ mvn install -P lite
54
55 The resulting artifact has the 'lite' classifier. To reference it
56 for 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
65 Installation - Without Maven
66 ============================
67
68 If you would rather not install Maven to build the library, you may
69 follow these instructions instead. Note that these instructions skip
70 running unit tests.
71
72 1) 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
81 2) Invoke protoc to build DescriptorProtos.java:
82
83 $ protoc --java_out=src/main/java -I../src \
84 ../src/google/protobuf/descriptor.proto
85
86 3) Compile the code in src/main/java using whatever means you prefer.
87
88 4) Install the classes wherever you prefer.
89
90 Micro version
91 ============================
92
93 The runtime and generated code for MICRO_RUNTIME is smaller
94 because it does not include support for the descriptor and
95 reflection, and enums are generated as integer constants in
96 the parent message or the file's outer class. Also, not
97 currently supported are packed repeated elements or
98 extensions.
99
100 To create a jar file for the runtime and run tests invoke
101 "mvn package -P micro" from the <protobuf-root>/java
102 directory. The generated jar file is
103 <protobuf-root>java/target/protobuf-java-2.2.0-micro.jar.
104
105 If you wish to compile the MICRO_RUNTIME your self, place
106 the 7 files below, in <root>/com/google/protobuf and
107 create a jar file for use with your code and the generated
108 code:
109
110 ByteStringMicro.java
111 CodedInputStreamMicro.java
112 CodedOutputStreamMicro.java
113 InvalidProtocolBufferException.java
114 MessageMicro.java
115 WireFormatMicro.java
116
117 If you wish to change on the code generator it is located
118 in /src/google/protobuf/compiler/javamicro.
119
120 To generate code for the MICRO_RUNTIME invoke protoc with
121 --javamicro_out command line parameter. javamicro_out takes
122 a series of optional sub-parameters separated by commas
123 and a final parameter, with a colon separator, which defines
124 the source directory. Sub-parameters begin with a name
125 followed by an equal and if that sub-parameter has multiple
126 parameters they are seperated by "|". The command line options
127 are:
128
129 opt -> speed or space
130 java_use_vector -> true or false
131 java_package -> <file-name>|<package-name>
132 java_outer_classname -> <file-name>|<package-name>
133 java_multiple_files -> true or false
134
135 opt={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
141 java_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
171 java_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
180 java_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
191 java_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
210 IMPORTANT: change of javamicro_out behavior:
211
212 In previous versions, if the outer class name has not been
213 given explicitly, javamicro_out would not infer the outer
214 class name from the file name, and would skip the outer
215 class generation. This makes the compilation succeed only
216 if the source file contains a single message and no enums,
217 and the generated class for that message is placed at the
218 package level. To re-align with java_out, javamicro_out
219 will now always generate the outer class, inferring its
220 name from the file name if not given, as a container of the
221 message classes and enum constants. To keep any existing
222 single-message source file from causing the generation of
223 an unwanted outer class, you can set the option
224 java_multiple_files to true, either in the file or as a
225 command line option.
226
227
228 Below are a series of examples for clarification of the
229 various parameters and options. Assuming this file:
230
231 src/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
241 and the compiled protoc in the current working directory,
242 then a simple command line to compile this file would be:
243
244 ./protoc --javamicro_out=. src/proto/simple-data-protos.proto
245
246 This will create testprotobuf/SimpleDataProtos.java, which
247 has 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
258 The message SimpleData is compiled into the SimpleData
259 class, nested in the file's outer class SimpleDataProtos,
260 whose name is implicitly defined by the proto file name
261 "simple-data-protos".
262
263 The directory, aka Java package, testprotobuf is created
264 because on line 1 of simple-data-protos.proto is
265 "package testprotobuf;". If you wanted a different
266 package name you could use the java_package option in the
267 file:
268
269 option java_package = "my_package";
270
271 or in command line sub-parameter:
272
273 ./protoc '--javamicro_out=\
274 java_package=src/proto/simple-data-protos.proto|my_package:\
275 .' src/proto/simple-data-protos.proto
276
277 Here you see the new java_package sub-parameter which
278 itself needs two parameters the file name and the
279 package name, these are separated by "|". The value set
280 in the command line overrides the value set in the file.
281 Now you'll find SimpleDataProtos.java in the my_package/
282 directory.
283
284 If you wanted to also change the optimization for
285 speed you'd add opt=speed with the comma seperator
286 as follows:
287
288 ./protoc '--javamicro_out=\
289 opt=speed,\
290 java_package=src/proto/simple-data-protos.proto|my_package:
291 .' src/proto/simple-data-protos.proto
292
293 If you also wanted a different outer class name you'd
294 do the following:
295
296 ./protoc '--javamicro_out=\
297 opt=speed,\
298 java_package=src/proto/simple-data-protos.proto|my_package,\
299 java_outer_classname=src/proto/simple-data-protos.proto|OuterName:\
300 .' src/proto/simple-data-protos.proto
301
302 Now you'll find my_package/OuterName.java and the
303 message class SimpleData nested in it.
304
305 As mentioned java_package, java_outer_classname and
306 java_multiple_files may also be specified in the file.
307 In the example below we must define
308 java_outer_classname because otherwise the outer class
309 and one of the message classes will have the same name,
310 which is forbidden to prevent name ambiguity:
311
312 src/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
333 This could be compiled using:
334
335 ./protoc --javamicro_out=. src/proto/sample-message.proto
336
337 and the output will be:
338
339 com/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
356 As you can see the file-scope enum MessageType is
357 disassembled into two integer constants in the outer class.
358 In javamicro_out, all enums are disassembled and compiled
359 into integer constants in the parent scope (the containing
360 message's class or the file's (i.e. outer) class).
361
362 You may prefer the file-scope messages to be saved in
363 separate files. You can do this by setting the option
364 java_multiple_files to true, in either the file like this:
365
366 option java_multiple_files = true;
367
368 or the command line like this:
369
370 ./protoc --javamicro_out=\
371 java_multiple_files=true:\
372 . src/proto/sample-message.proto
373
374 The java_multiple_files option causes javamicro to use a
375 separate file for each file-scope message, which resides
376 directly in the Java package alongside the outer class:
377
378 com/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
386 com/example/SampleMessage.java:
387
388 package com.example;
389 public final class SampleMessage
390 extends MessageMicro {
391 ...
392 }
393
394 com/example/SampleMessageContainer.java:
395
396 package com.example;
397 public final class SampleMessageContainer
398 extends MessageMicro {
399 ...
400 }
401
402 As you can see, the outer class now contains only the
403 integer constants, generated from the file-scope enum
404 "MessageType". Please note that message-scope enums are
405 still generated as integer constants in the message class.
406
407
408 Nano version
409 ============================
410
411 Nano is even smaller than micro, especially in the number of generated
412 functions. 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
429 IMPORTANT: If you have fields with defaults
430
431 How fields with defaults are serialized has changed. Because we don't
432 keep "has" state, any field equal to its default is assumed to be not
433 set and therefore is not serialized. Consider the situation where we
434 change the default value of a field. Senders compiled against an older
435 version of the proto continue to match against the old default, and
436 don't send values to the receiver even though the receiver assumes the
437 new default value. Therefore, think carefully about the implications
438 of changing the default value.
439
440 IMPORTANT: If you have "bytes" fields with non-empty defaults
441
442 Because the byte buffer is now of mutable type byte[], the default
443 static final cannot be exposed through a public field. Each time a
444 message's constructor or clear() function is called, the default value
445 (kept in a private byte[]) is cloned. This causes a small memory
446 penalty. This is not a problem if the field has no default or is an
447 empty default.
448
449 Nano Generator options
450
451 java_package -> <file-name>|<package-name>
452 java_outer_classname -> <file-name>|<package-name>
453 java_multiple_files -> true or false
454 java_nano_generate_has -> true or false
455
456 java_package:
457 java_outer_classname:
458 java_multiple_files:
459 Same as Micro version.
460
461 java_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
476 To 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=\
483 java_package=src/proto/simple-data.proto|my_package,\
484 java_outer_classname=src/proto/simple-data.proto|OuterName:\
485 .' src/proto/simple-data.proto
486
487 Contributing to nano:
488
489 Please add/edit tests in NanoTest.java.
490
491 Please 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
507 Usage
508 =====
509
510 The complete documentation for Protocol Buffers is available via the
511 web at:
512
513 http://code.google.com/apis/protocolbuffers/
514