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, 95reflection or extensions. Also, not currently supported 96are packed repeated elements nor testing of java_multiple_files. 97 98To create a jar file for the runtime and run tests invoke 99"mvn package -P micro" from the <protobuf-root>/java 100directory. The generated jar file is 101<protobuf-root>java/target/protobuf-java-2.2.0-micro.jar. 102 103If you wish to compile the MICRO_RUTIME your self, place 104the 7 files below, in <root>/com/google/protobuf and 105create a jar file for use with your code and the generated 106code: 107 108ByteStringMicro.java 109CodedInputStreamMicro.java 110CodedOutputStreamMicro.java 111InvalidProtocolBufferException.java 112MessageMicro.java 113WireFormatMicro.java 114 115If you wish to change on the code generator it is located 116in /src/google/protobuf/compiler/javamicro. 117 118To generate code for the MICRO_RUNTIME invoke protoc with 119--javamicro_out command line parameter. javamciro_out takes 120a series of optional sub-parameters separated by comma's 121and a final parameter, with a colon separator, which defines 122the source directory. Sub-paraemeters begin with a name 123followed by an equal and if that sub-parameter has multiple 124parameters they are seperated by "|". The command line options 125are: 126 127opt -> speed or space 128java_use_vector -> true or false 129java_package -> <file-name>|<package-name> 130java_outer_classname -> <file-name>|<package-name> 131 132opt: 133 This change the code generation to optimize for speed, 134 opt=speed, or space, opt=space. When opt=speed this 135 changes the code generation for strings so that multiple 136 conversions to Utf8 are eliminated. The default value 137 is opt=space. 138 139java_use_vector: 140 Is a boolean flag either java_use_vector=true or 141 java_use_vector=false. When java_use_vector=true the 142 code generated for repeated elements uses 143 java.util.Vector and when java_use_vector=false the 144 java.util.ArrayList<> is used. When java.util.Vector 145 is used the code must be compiled with Java 1.3 and 146 when ArrayList is used Java 1.5 or above must be used. 147 The using javac the source parameter maybe used to 148 control the version of the srouce: "javac -source 1.3". 149 You can also change the <source> xml element for the 150 maven-compiler-plugin. 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 When compiling for 1.5 java_use_vector=false or not 161 present where the default value is false. 162 163 And below would be for 1.3 sources note when changing 164 to 1.3 you must also set java_use_vector=true: 165 166 <plugin> 167 <artifactId>maven-compiler-plugin</artifactId> 168 <configuration> 169 <source>1.3</source> 170 <target>1.5</target> 171 </configuration> 172 </plugin> 173 174java_package: 175 The allows setting/overriding the java_package option 176 and associates allows a package name for a file to 177 be specified on the command line. Overriding any 178 "option java_package xxxx" in the file. The default 179 if not present is to use the value from the package 180 statment or "option java_package xxxx" in the file. 181 182java_outer_classname: 183 This allows the setting/overriding of the outer 184 class name option and associates a class name 185 to a file. An outer class name is required and 186 must be specified if there are multiple messages 187 in a single proto file either in the proto source 188 file or on the command line. If not present the 189 no outer class name will be used. 190 191Below are a series of examples for clarification of the 192various javamicro_out parameters using 193src/test/proto/simple-data.proto: 194 195package testprotobuf; 196 197message SimpleData { 198 optional fixed64 id = 1; 199 optional string description = 2; 200 optional bool ok = 3 [default = false]; 201}; 202 203 204Assuming you've only compiled and not installed protoc and 205your current working directory java/, then a simple 206command line to compile simple-data would be: 207 208../src/protoc --javamicro_out=. src/test/proto/simple-data.proto 209 210This will create testprotobuf/SimpleData.java 211 212The directory testprotobuf is created because on line 1 213of simple-data.proto is "package testprotobuf;". If you 214wanted a different package name you could use the 215java_package option command line sub-parameter: 216 217../src/protoc '--javamicro_out=java_package=src/test/proto/simple-data.proto|my_package:.' src/test/proto/simple-data.proto 218 219Here you see the new java_package sub-parameter which 220itself needs two parameters the file name and the 221package name, these are separated by "|". Now you'll 222find my_package/SimpleData.java. 223 224If you wanted to also change the optimization for 225speed you'd add opt=speed with the comma seperator 226as follows: 227 228../src/protoc '--javamicro_out=opt=speed,java_package=src/test/proto/simple-data.proto|my_package:.' src/test/proto/simple-data.proto 229 230Finally if you also wanted an outer class name you'd 231do the following: 232 233../src/protoc '--javamicro_out=opt=speed,java_package=src/test/proto/simple-data.proto|my_package,java_outer_classname=src/test/proto/simple-data.proto|OuterName:.' src/test/proto/simple-data.proto 234 235Now you'll find my_packate/OuterName.java. 236 237As mentioned java_package and java_outer_classname 238may also be specified in the file. In the example 239below we must define java_outer_classname because 240there are multiple messages in 241src/test/proto/two-messages.proto 242 243package testmicroruntime; 244 245option java_package = "com.example"; 246option java_outer_classname = "TestMessages"; 247 248message TestMessage1 { 249 required int32 id = 1; 250} 251 252message TestMessage2 { 253 required int32 id = 1; 254} 255 256This could be compiled using: 257 258../src/protoc --javamicro_out=. src/test/proto/two-message.proto 259 260With the result will be com/example/TestMessages.java 261 262 263Nano version 264============================ 265 266Nano is even smaller than micro, especially in the number of generated 267functions. It is like micro except: 268 269- No setter/getter/hazzer functions. 270- Has state is not available. Outputs all fields not equal to their 271 default. (See important implications below.) 272- CodedInputStreamMicro is renamed to CodedInputByteBufferNano and can 273 only take byte[] (not InputStream). 274- Similar rename from CodedOutputStreamMicro to 275 CodedOutputByteBufferNano. 276- Repeated fields are in arrays, not ArrayList or Vector. 277- Unset messages/groups are null, not an immutable empty default 278 instance. 279- Required fields are always serialized. 280- toByteArray(...) and mergeFrom(...) are now static functions of 281 MessageNano. 282- "bytes" are of java type byte[]. 283 284IMPORTANT: If you have fields with defaults 285 286How fields with defaults are serialized has changed. Because we don't 287keep "has" state, any field equal to its default is assumed to be not 288set and therefore is not serialized. Consider the situation where we 289change the default value of a field. Senders compiled against an older 290version of the proto continue to match against the old default, and 291don't send values to the receiver even though the receiver assumes the 292new default value. Therefore, think carefully about the implications 293of changing the default value. 294 295IMPORTANT: If you have "bytes" fields with non-empty defaults 296 297Because the byte buffer is now of mutable type byte[], the default 298static final cannot be exposed through a public field. Each time a 299message's constructor or clear() function is called, the default value 300(kept in a private byte[]) is cloned. This causes a small memory 301penalty. This is not a problem if the field has no default or is an 302empty default. 303 304 305To use nano protobufs: 306 307- Link with the generated jar file 308 <protobuf-root>java/target/protobuf-java-2.3.0-nano.jar. 309- Invoke with --javanano_out, e.g.: 310 311../src/protoc '--javanano_out=java_package=src/test/proto/simple-data.proto|my_package,java_outer_classname=src/test/proto/simple-data.proto|OuterName:.' src/test/proto/simple-data.proto 312 313 314Usage 315===== 316 317The complete documentation for Protocol Buffers is available via the 318web at: 319 320 http://code.google.com/apis/protocolbuffers/ 321