README.txt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. Protocol Buffers - Google's data interchange format
  2. Copyright 2008 Google Inc.
  3. This directory contains the Java Protocol Buffers runtime library.
  4. Installation - With Maven
  5. =========================
  6. The Protocol Buffers build is managed using Maven. If you would
  7. rather build without Maven, see below.
  8. 1) Install Apache Maven if you don't have it:
  9. http://maven.apache.org/
  10. 2) Build the C++ code, or obtain a binary distribution of protoc. If
  11. you install a binary distribution, make sure that it is the same
  12. version as this package. If in doubt, run:
  13. $ protoc --version
  14. You will need to place the protoc executable in ../src. (If you
  15. built it yourself, it should already be there.)
  16. 3) Run the tests:
  17. $ mvn test
  18. If some tests fail, this library may not work correctly on your
  19. system. Continue at your own risk.
  20. 4) Install the library into your Maven repository:
  21. $ mvn install
  22. 5) If you do not use Maven to manage your own build, you can build a
  23. .jar file to use:
  24. $ mvn package
  25. The .jar will be placed in the "target" directory.
  26. Installation - 'Lite' Version - With Maven
  27. ==========================================
  28. Building the 'lite' version of the Java Protocol Buffers library is
  29. the same as building the full version, except that all commands are
  30. run using the 'lite' profile. (see
  31. http://maven.apache.org/guides/introduction/introduction-to-profiles.html)
  32. E.g. to install the lite version of the jar, you would run:
  33. $ mvn install -P lite
  34. The resulting artifact has the 'lite' classifier. To reference it
  35. for dependency resolution, you would specify it as:
  36. <dependency>
  37. <groupId>com.google.protobuf</groupId>
  38. <artifactId>protobuf-java</artifactId>
  39. <version>${version}</version>
  40. <classifier>lite</classifier>
  41. </dependency>
  42. Installation - Without Maven
  43. ============================
  44. If you would rather not install Maven to build the library, you may
  45. follow these instructions instead. Note that these instructions skip
  46. running unit tests.
  47. 1) Build the C++ code, or obtain a binary distribution of protoc. If
  48. you install a binary distribution, make sure that it is the same
  49. version as this package. If in doubt, run:
  50. $ protoc --version
  51. If you built the C++ code without installing, the compiler binary
  52. should be located in ../src.
  53. 2) Invoke protoc to build DescriptorProtos.java:
  54. $ protoc --java_out=src/main/java -I../src \
  55. ../src/google/protobuf/descriptor.proto
  56. 3) Compile the code in src/main/java using whatever means you prefer.
  57. 4) Install the classes wherever you prefer.
  58. Micro version
  59. ============================
  60. The runtime and generated code for MICRO_RUNTIME is smaller
  61. because it does not include support for the descriptor and
  62. reflection, and enums are generated as integer constants in
  63. the parent message or the file's outer class. Also, not
  64. currently supported are packed repeated elements or
  65. extensions.
  66. To create a jar file for the runtime and run tests invoke
  67. "mvn package -P micro" from the <protobuf-root>/java
  68. directory. The generated jar file is
  69. <protobuf-root>java/target/protobuf-java-2.2.0-micro.jar.
  70. If you wish to compile the MICRO_RUNTIME your self, place
  71. the 7 files below, in <root>/com/google/protobuf and
  72. create a jar file for use with your code and the generated
  73. code:
  74. ByteStringMicro.java
  75. CodedInputStreamMicro.java
  76. CodedOutputStreamMicro.java
  77. InvalidProtocolBufferException.java
  78. MessageMicro.java
  79. WireFormatMicro.java
  80. If you wish to change on the code generator it is located
  81. in /src/google/protobuf/compiler/javamicro.
  82. To generate code for the MICRO_RUNTIME invoke protoc with
  83. --javamicro_out command line parameter. javamicro_out takes
  84. a series of optional sub-parameters separated by commas
  85. and a final parameter, with a colon separator, which defines
  86. the source directory. Sub-parameters begin with a name
  87. followed by an equal and if that sub-parameter has multiple
  88. parameters they are seperated by "|". The command line options
  89. are:
  90. opt -> speed or space
  91. java_use_vector -> true or false
  92. java_package -> <file-name>|<package-name>
  93. java_outer_classname -> <file-name>|<package-name>
  94. java_multiple_files -> true or false
  95. opt={speed,space} (default: space)
  96. This changes the code generation to optimize for speed or
  97. space. When opt=speed this changes the code generation
  98. for strings so that multiple conversions to Utf8 are
  99. eliminated.
  100. java_use_vector={true,false} (default: false)
  101. This specifies the collection class for repeated elements.
  102. If false, repeated elements use java.util.ArrayList<> and
  103. the code must be compiled with Java 1.5 or above. If true,
  104. repeated elements use java.util.Vector and the code can
  105. be compiled with Java 1.3 or above. The 'source'
  106. parameter of 'javac' may be used to control the version
  107. of the source: "javac -source 1.3". You can also change
  108. the <source> xml element for the maven-compiler-plugin.
  109. Below is for 1.5 sources:
  110. <plugin>
  111. <artifactId>maven-compiler-plugin</artifactId>
  112. <configuration>
  113. <source>1.5</source>
  114. <target>1.5</target>
  115. </configuration>
  116. </plugin>
  117. And below would be for 1.3 sources (note when changing
  118. to 1.3 you must also set java_use_vector=true):
  119. <plugin>
  120. <artifactId>maven-compiler-plugin</artifactId>
  121. <configuration>
  122. <source>1.3</source>
  123. <target>1.5</target>
  124. </configuration>
  125. </plugin>
  126. java_package=<file-name>|<package-name> (no default)
  127. This allows overriding the 'java_package' option value
  128. for the given file from the command line. Use multiple
  129. java_package options to override the option for multiple
  130. files. The final Java package for each file is the value
  131. of this command line option if present, or the value of
  132. the same option defined in the file if present, or the
  133. proto package if present, or the default Java package.
  134. java_outer_classname=<file-name>|<outer-classname> (no default)
  135. This allows overriding the 'java_outer_classname' option
  136. for the given file from the command line. Use multiple
  137. java_outer_classname options to override the option for
  138. multiple files. The final Java outer class name for each
  139. file is the value of this command line option if present,
  140. or the value of the same option defined in the file if
  141. present, or the file name converted to CamelCase. This
  142. outer class will nest all classes and integer constants
  143. generated from file-scope messages and enums.
  144. java_multiple_files={true,false} (no default)
  145. This allows overriding the 'java_multiple_files' option
  146. in all source files and their imported files from the
  147. command line. The final value of this option for each
  148. file is the value defined in this command line option, or
  149. the value of the same option defined in the file if
  150. present, or false. This specifies whether to generate
  151. package-level classes for the file-scope messages in the
  152. same Java package as the outer class (instead of nested
  153. classes in the outer class). File-scope enum constants
  154. are still generated as integer constants in the outer
  155. class. This affects the fully qualified references in the
  156. Java code. NOTE: because the command line option
  157. overrides the value for all files and their imported
  158. files, using this option inconsistently may result in
  159. incorrect references to the imported messages and enum
  160. constants.
  161. IMPORTANT: change of javamicro_out behavior:
  162. In previous versions, if the outer class name has not been
  163. given explicitly, javamicro_out would not infer the outer
  164. class name from the file name, and would skip the outer
  165. class generation. This makes the compilation succeed only
  166. if the source file contains a single message and no enums,
  167. and the generated class for that message is placed at the
  168. package level. To re-align with java_out, javamicro_out
  169. will now always generate the outer class, inferring its
  170. name from the file name if not given, as a container of the
  171. message classes and enum constants. To keep any existing
  172. single-message source file from causing the generation of
  173. an unwanted outer class, you can set the option
  174. java_multiple_files to true, either in the file or as a
  175. command line option.
  176. Below are a series of examples for clarification of the
  177. various parameters and options. Assuming this file:
  178. src/proto/simple-data-protos.proto:
  179. package testprotobuf;
  180. message SimpleData {
  181. optional fixed64 id = 1;
  182. optional string description = 2;
  183. optional bool ok = 3 [default = false];
  184. };
  185. and the compiled protoc in the current working directory,
  186. then a simple command line to compile this file would be:
  187. ./protoc --javamicro_out=. src/proto/simple-data-protos.proto
  188. This will create testprotobuf/SimpleDataProtos.java, which
  189. has the following content (extremely simplified):
  190. package testprotobuf;
  191. public final class SimpleDataProtos {
  192. public static final class SimpleData
  193. extends MessageMicro {
  194. ...
  195. }
  196. }
  197. The message SimpleData is compiled into the SimpleData
  198. class, nested in the file's outer class SimpleDataProtos,
  199. whose name is implicitly defined by the proto file name
  200. "simple-data-protos".
  201. The directory, aka Java package, testprotobuf is created
  202. because on line 1 of simple-data-protos.proto is
  203. "package testprotobuf;". If you wanted a different
  204. package name you could use the java_package option in the
  205. file:
  206. option java_package = "my_package";
  207. or in command line sub-parameter:
  208. ./protoc '--javamicro_out=\
  209. java_package=src/proto/simple-data-protos.proto|my_package:\
  210. .' src/proto/simple-data-protos.proto
  211. Here you see the new java_package sub-parameter which
  212. itself needs two parameters the file name and the
  213. package name, these are separated by "|". The value set
  214. in the command line overrides the value set in the file.
  215. Now you'll find SimpleDataProtos.java in the my_package/
  216. directory.
  217. If you wanted to also change the optimization for
  218. speed you'd add opt=speed with the comma seperator
  219. as follows:
  220. ./protoc '--javamicro_out=\
  221. opt=speed,\
  222. java_package=src/proto/simple-data-protos.proto|my_package:
  223. .' src/proto/simple-data-protos.proto
  224. If you also wanted a different outer class name you'd
  225. do the following:
  226. ./protoc '--javamicro_out=\
  227. opt=speed,\
  228. java_package=src/proto/simple-data-protos.proto|my_package,\
  229. java_outer_classname=src/proto/simple-data-protos.proto|OuterName:\
  230. .' src/proto/simple-data-protos.proto
  231. Now you'll find my_package/OuterName.java and the
  232. message class SimpleData nested in it.
  233. As mentioned java_package, java_outer_classname and
  234. java_multiple_files may also be specified in the file.
  235. In the example below we must define
  236. java_outer_classname because otherwise the outer class
  237. and one of the message classes will have the same name,
  238. which is forbidden to prevent name ambiguity:
  239. src/proto/sample-message.proto:
  240. package testmicroruntime;
  241. option java_package = "com.example";
  242. option java_outer_classname = "SampleMessageProtos";
  243. enum MessageType {
  244. SAMPLE = 1;
  245. EXAMPLE = 2;
  246. }
  247. message SampleMessage {
  248. required int32 id = 1;
  249. required MessageType type = 2;
  250. }
  251. message SampleMessageContainer {
  252. required SampleMessage message = 1;
  253. }
  254. This could be compiled using:
  255. ./protoc --javamicro_out=. src/proto/sample-message.proto
  256. and the output will be:
  257. com/example/SampleMessageProtos.java:
  258. package com.example;
  259. public final class SampleMessageProtos {
  260. public static final int SAMPLE = 1;
  261. public static final int EXAMPLE = 2;
  262. public static final class SampleMessage
  263. extends MessageMicro {
  264. ...
  265. }
  266. public static final class SampleMessageContainer
  267. extends MessageMicro {
  268. ...
  269. }
  270. }
  271. As you can see the file-scope enum MessageType is
  272. disassembled into two integer constants in the outer class.
  273. In javamicro_out, all enums are disassembled and compiled
  274. into integer constants in the parent scope (the containing
  275. message's class or the file's (i.e. outer) class).
  276. You may prefer the file-scope messages to be saved in
  277. separate files. You can do this by setting the option
  278. java_multiple_files to true, in either the file like this:
  279. option java_multiple_files = true;
  280. or the command line like this:
  281. ./protoc --javamicro_out=\
  282. java_multiple_files=true:\
  283. . src/proto/sample-message.proto
  284. The java_multiple_files option causes javamicro to use a
  285. separate file for each file-scope message, which resides
  286. directly in the Java package alongside the outer class:
  287. com/example/SampleMessageProtos.java:
  288. package com.example;
  289. public final class SampleMessageProtos {
  290. public static final int SAMPLE = 1;
  291. public static final int EXAMPLE = 2;
  292. }
  293. com/example/SampleMessage.java:
  294. package com.example;
  295. public final class SampleMessage
  296. extends MessageMicro {
  297. ...
  298. }
  299. com/example/SampleMessageContainer.java:
  300. package com.example;
  301. public final class SampleMessageContainer
  302. extends MessageMicro {
  303. ...
  304. }
  305. As you can see, the outer class now contains only the
  306. integer constants, generated from the file-scope enum
  307. "MessageType". Please note that message-scope enums are
  308. still generated as integer constants in the message class.
  309. Nano version
  310. ============================
  311. Nano is even smaller than micro, especially in the number of generated
  312. functions. It is like micro except:
  313. - No setter/getter/hazzer functions.
  314. - Has state is not available. Outputs all fields not equal to their
  315. default. (See important implications below.)
  316. - CodedInputStreamMicro is renamed to CodedInputByteBufferNano and can
  317. only take byte[] (not InputStream).
  318. - Similar rename from CodedOutputStreamMicro to
  319. CodedOutputByteBufferNano.
  320. - Repeated fields are in arrays, not ArrayList or Vector.
  321. - Unset messages/groups are null, not an immutable empty default
  322. instance.
  323. - Required fields are always serialized.
  324. - toByteArray(...) and mergeFrom(...) are now static functions of
  325. MessageNano.
  326. - "bytes" are of java type byte[].
  327. IMPORTANT: If you have fields with defaults
  328. How fields with defaults are serialized has changed. Because we don't
  329. keep "has" state, any field equal to its default is assumed to be not
  330. set and therefore is not serialized. Consider the situation where we
  331. change the default value of a field. Senders compiled against an older
  332. version of the proto continue to match against the old default, and
  333. don't send values to the receiver even though the receiver assumes the
  334. new default value. Therefore, think carefully about the implications
  335. of changing the default value.
  336. IMPORTANT: If you have "bytes" fields with non-empty defaults
  337. Because the byte buffer is now of mutable type byte[], the default
  338. static final cannot be exposed through a public field. Each time a
  339. message's constructor or clear() function is called, the default value
  340. (kept in a private byte[]) is cloned. This causes a small memory
  341. penalty. This is not a problem if the field has no default or is an
  342. empty default.
  343. Nano Generator options
  344. java_package -> <file-name>|<package-name>
  345. java_outer_classname -> <file-name>|<package-name>
  346. java_multiple_files -> true or false
  347. java_nano_generate_has -> true or false
  348. java_package:
  349. java_outer_classname:
  350. java_multiple_files:
  351. Same as Micro version.
  352. java_nano_generate_has={true,false} (default: false)
  353. If true, generates a public boolean variable has<fieldname>
  354. accompanying each optional or required field (not present for
  355. repeated fields, groups or messages). It is set to false initially
  356. and upon clear(). If parseFrom(...) reads the field from the wire,
  357. it is set to true. This is a way for clients to inspect the "has"
  358. value upon parse. If it is set to true, writeTo(...) will ALWAYS
  359. output that field (even if field value is equal to its
  360. default).
  361. IMPORTANT: This option costs an extra 4 bytes per primitive field in
  362. the message. Think carefully about whether you really need this. In
  363. many cases reading the default works and determining whether the
  364. field was received over the wire is irrelevant.
  365. To use nano protobufs:
  366. - Link with the generated jar file
  367. <protobuf-root>java/target/protobuf-java-2.3.0-nano.jar.
  368. - Invoke with --javanano_out, e.g.:
  369. ./protoc '--javanano_out=\
  370. java_package=src/proto/simple-data.proto|my_package,\
  371. java_outer_classname=src/proto/simple-data.proto|OuterName:\
  372. .' src/proto/simple-data.proto
  373. Contributing to nano:
  374. Please add/edit tests in NanoTest.java.
  375. Please run the following steps to test:
  376. - cd external/protobuf
  377. - ./configure
  378. - Run "make -j12 check" and verify all tests pass.
  379. - cd java
  380. - Run "mvn test" and verify all tests pass.
  381. - cd ../../..
  382. - . build/envsetup.sh
  383. - lunch 1
  384. - "make -j12 aprotoc libprotobuf-java-2.3.0-nano aprotoc-test-nano-params" and
  385. check for build errors.
  386. - repo sync -c -j256
  387. - "make -j12" and check for build errors
  388. Usage
  389. =====
  390. The complete documentation for Protocol Buffers is available via the
  391. web at:
  392. http://code.google.com/apis/protocolbuffers/