diff options
author | Adam Cozzette <acozzette@google.com> | 2016-11-17 17:04:30 -0800 |
---|---|---|
committer | Adam Cozzette <acozzette@google.com> | 2016-11-17 17:04:30 -0800 |
commit | 5d63097fc2b7f405f53d6ca4ad3c1ebd98d80ddd (patch) | |
tree | 50d8116271f024e16334785464c794da85f3ce12 /java | |
parent | 5a76e633ea9b5adb215e93fdc11e1c0c08b3fc74 (diff) | |
parent | cd315dcbadc02569e145bde16e3f66c2fbb08e31 (diff) | |
download | protobuf-5d63097fc2b7f405f53d6ca4ad3c1ebd98d80ddd.tar.gz protobuf-5d63097fc2b7f405f53d6ca4ad3c1ebd98d80ddd.tar.bz2 protobuf-5d63097fc2b7f405f53d6ca4ad3c1ebd98d80ddd.zip |
Merge branch 'master' into down-integrate-with-msvc-fix
Diffstat (limited to 'java')
-rw-r--r-- | java/compatibility_tests/v2.5.0/protos/pom.xml | 2 | ||||
-rw-r--r-- | java/core/pom.xml | 33 | ||||
-rw-r--r-- | java/core/src/main/java/com/google/protobuf/ByteBufferWriter.java | 50 | ||||
-rw-r--r-- | java/core/src/main/java/com/google/protobuf/MapFieldLite.java | 2 | ||||
-rw-r--r-- | java/lite/pom.xml | 32 | ||||
-rw-r--r-- | java/pom.xml | 2 | ||||
-rw-r--r-- | java/util/pom.xml | 22 |
7 files changed, 123 insertions, 20 deletions
diff --git a/java/compatibility_tests/v2.5.0/protos/pom.xml b/java/compatibility_tests/v2.5.0/protos/pom.xml index 24447bdc..a22e91ed 100644 --- a/java/compatibility_tests/v2.5.0/protos/pom.xml +++ b/java/compatibility_tests/v2.5.0/protos/pom.xml @@ -28,7 +28,7 @@ <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> - <version>3.3</version> + <version>3.6.0</version> <configuration> <source>1.6</source> <target>1.6</target> diff --git a/java/core/pom.xml b/java/core/pom.xml index 8a83eb4e..cced344e 100644 --- a/java/core/pom.xml +++ b/java/core/pom.xml @@ -92,11 +92,34 @@ <!-- Add the generated sources to the build --> <plugin> - <artifactId>maven-compiler-plugin</artifactId> - <configuration> - <generatedSourcesDirectory>${generated.sources.dir}</generatedSourcesDirectory> - <generatedTestSourcesDirectory>${generated.testsources.dir}</generatedTestSourcesDirectory> - </configuration> + <groupId>org.codehaus.mojo</groupId> + <artifactId>build-helper-maven-plugin</artifactId> + <executions> + <execution> + <id>add-generated-sources</id> + <phase>generate-sources</phase> + <goals> + <goal>add-source</goal> + </goals> + <configuration> + <sources> + <source>${generated.sources.dir}</source> + </sources> + </configuration> + </execution> + <execution> + <id>add-generated-test-sources</id> + <phase>generate-test-sources</phase> + <goals> + <goal>add-test-source</goal> + </goals> + <configuration> + <sources> + <source>${generated.testsources.dir}</source> + </sources> + </configuration> + </execution> + </executions> </plugin> <!-- OSGI bundle configuration --> diff --git a/java/core/src/main/java/com/google/protobuf/ByteBufferWriter.java b/java/core/src/main/java/com/google/protobuf/ByteBufferWriter.java index 0cc38175..6157a52f 100644 --- a/java/core/src/main/java/com/google/protobuf/ByteBufferWriter.java +++ b/java/core/src/main/java/com/google/protobuf/ByteBufferWriter.java @@ -33,11 +33,12 @@ package com.google.protobuf; import static java.lang.Math.max; import static java.lang.Math.min; -import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.lang.ref.SoftReference; +import java.lang.reflect.Field; import java.nio.ByteBuffer; +import java.nio.channels.WritableByteChannel; /** * Utility class to provide efficient writing of {@link ByteBuffer}s to {@link OutputStream}s. @@ -75,6 +76,12 @@ final class ByteBufferWriter { new ThreadLocal<SoftReference<byte[]>>(); /** + * This is a hack for GAE, where {@code FileOutputStream} is unavailable. + */ + private static final Class<?> FILE_OUTPUT_STREAM_CLASS = safeGetClass("java.io.FileOutputStream"); + private static final long CHANNEL_FIELD_OFFSET = getChannelFieldOffset(FILE_OUTPUT_STREAM_CLASS); + + /** * For testing purposes only. Clears the cached buffer to force a new allocation on the next * invocation. */ @@ -93,10 +100,7 @@ final class ByteBufferWriter { // Optimized write for array-backed buffers. // Note that we're taking the risk that a malicious OutputStream could modify the array. output.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); - } else if (output instanceof FileOutputStream) { - // Use a channel to write out the ByteBuffer. This will automatically empty the buffer. - ((FileOutputStream) output).getChannel().write(buffer); - } else { + } else if (!writeToChannel(buffer, output)){ // Read all of the data from the buffer to an array. // TODO(nathanmittler): Consider performance improvements for other "known" stream types. final byte[] array = getOrCreateBuffer(buffer.remaining()); @@ -142,4 +146,40 @@ final class ByteBufferWriter { private static void setBuffer(byte[] value) { BUFFER.set(new SoftReference<byte[]>(value)); } + + private static boolean writeToChannel(ByteBuffer buffer, OutputStream output) throws IOException { + if (CHANNEL_FIELD_OFFSET >= 0 && FILE_OUTPUT_STREAM_CLASS.isInstance(output)) { + // Use a channel to write out the ByteBuffer. This will automatically empty the buffer. + WritableByteChannel channel = null; + try { + channel = (WritableByteChannel) UnsafeUtil.getObject(output, CHANNEL_FIELD_OFFSET); + } catch (ClassCastException e) { + // Absorb. + } + if (channel != null) { + channel.write(buffer); + return true; + } + } + return false; + } + + private static Class<?> safeGetClass(String className) { + try { + return Class.forName(className); + } catch (ClassNotFoundException e) { + return null; + } + } + private static long getChannelFieldOffset(Class<?> clazz) { + try { + if (clazz != null && UnsafeUtil.hasUnsafeArrayOperations()) { + Field field = clazz.getDeclaredField("channel"); + return UnsafeUtil.objectFieldOffset(field); + } + } catch (Throwable e) { + // Absorb + } + return -1; + } } diff --git a/java/core/src/main/java/com/google/protobuf/MapFieldLite.java b/java/core/src/main/java/com/google/protobuf/MapFieldLite.java index 3c0ad89a..16b3fefe 100644 --- a/java/core/src/main/java/com/google/protobuf/MapFieldLite.java +++ b/java/core/src/main/java/com/google/protobuf/MapFieldLite.java @@ -58,7 +58,7 @@ public final class MapFieldLite<K, V> extends LinkedHashMap<K, V> { } @SuppressWarnings({"rawtypes", "unchecked"}) - private static final MapFieldLite EMPTY_MAP_FIELD = new MapFieldLite(Collections.emptyMap()); + private static final MapFieldLite EMPTY_MAP_FIELD = new MapFieldLite(); static { EMPTY_MAP_FIELD.makeImmutable(); } diff --git a/java/lite/pom.xml b/java/lite/pom.xml index 9862cd94..d7b15097 100644 --- a/java/lite/pom.xml +++ b/java/lite/pom.xml @@ -76,10 +76,38 @@ <!-- Only compile a subset of the files --> <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>build-helper-maven-plugin</artifactId> + <executions> + <execution> + <id>add-generated-sources</id> + <phase>generate-sources</phase> + <goals> + <goal>add-source</goal> + </goals> + <configuration> + <sources> + <source>${generated.sources.lite.dir}</source> + </sources> + </configuration> + </execution> + <execution> + <id>add-generated-test-sources</id> + <phase>generate-test-sources</phase> + <goals> + <goal>add-test-source</goal> + </goals> + <configuration> + <sources> + <source>${generated.testsources.lite.dir}</source> + </sources> + </configuration> + </execution> + </executions> + </plugin> + <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> - <generatedSourcesDirectory>${generated.sources.lite.dir}</generatedSourcesDirectory> - <generatedTestSourcesDirectory>${generated.testsources.lite.dir}</generatedTestSourcesDirectory> <includes> <include>**/AbstractMessageLite.java</include> <include>**/AbstractParser.java</include> diff --git a/java/pom.xml b/java/pom.xml index 881473f3..6789e7c1 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -94,7 +94,7 @@ <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> - <version>3.3</version> + <version>3.6.0</version> <configuration> <source>1.6</source> <target>1.6</target> diff --git a/java/util/pom.xml b/java/util/pom.xml index 6b07bcdf..0ccfc848 100644 --- a/java/util/pom.xml +++ b/java/util/pom.xml @@ -79,12 +79,24 @@ </executions> </plugin> + <!-- Add the generated test sources to the build --> <plugin> - <artifactId>maven-compiler-plugin</artifactId> - <configuration> - <!-- Add the generated test sources to the build --> - <generatedTestSourcesDirectory>${generated.testsources.dir}</generatedTestSourcesDirectory> - </configuration> + <groupId>org.codehaus.mojo</groupId> + <artifactId>build-helper-maven-plugin</artifactId> + <executions> + <execution> + <id>add-generated-test-sources</id> + <phase>generate-test-sources</phase> + <goals> + <goal>add-test-source</goal> + </goals> + <configuration> + <sources> + <source>${generated.testsources.dir}</source> + </sources> + </configuration> + </execution> + </executions> </plugin> <!-- Configure the OSGI bundle --> |