diff options
author | Jesse Wilson <jessewilson@google.com> | 2009-05-21 17:39:08 -0700 |
---|---|---|
committer | Jesse Wilson <jessewilson@google.com> | 2009-06-09 16:09:47 -0700 |
commit | 57995e8186b54515d5a03bf2ab104c3dc247f1b6 (patch) | |
tree | fb4502a727023f0021f949c366304b3a082d03f7 /luni | |
parent | 796290e84a25f1444d43604af31cf872648c583f (diff) | |
download | libcore-57995e8186b54515d5a03bf2ab104c3dc247f1b6.zip libcore-57995e8186b54515d5a03bf2ab104c3dc247f1b6.tar.gz libcore-57995e8186b54515d5a03bf2ab104c3dc247f1b6.tar.bz2 |
Updating archive to Harmony r772995.
Squashed commit of the following:
Adding @TestTargetNew tags
Initial merge of branch 'archive_772995' into archive_dalvik.
Fixed some problems, including InflaterInputStream.available()
and JarFile.skip() bugs.
Conflicts:
libcore/archive/.classpath
libcore/archive/META-INF/MANIFEST.MF
libcore/archive/build.xml
libcore/archive/make/hyproperties.xml
libcore/archive/src/main/java/java/util/jar/Attributes.java
libcore/archive/src/main/java/java/util/jar/JarEntry.java
libcore/archive/src/main/java/java/util/jar/JarException.java
libcore/archive/src/main/java/java/util/jar/JarFile.java
libcore/archive/src/main/java/java/util/jar/JarInputStream.java
libcore/archive/src/main/java/java/util/jar/JarVerifier.java
libcore/archive/src/main/java/java/util/jar/Manifest.java
libcore/archive/src/main/java/java/util/jar/Pack200.java
libcore/archive/src/main/java/java/util/zip/Adler32.java
libcore/archive/src/main/java/java/util/zip/CRC32.java
libcore/archive/src/main/java/java/util/zip/Checksum.java
libcore/archive/src/main/java/java/util/zip/DataFormatException.java
libcore/archive/src/main/java/java/util/zip/Deflater.java
libcore/archive/src/main/java/java/util/zip/DeflaterOutputStream.java
libcore/archive/src/main/java/java/util/zip/GZIPInputStream.java
libcore/archive/src/main/java/java/util/zip/GZIPOutputStream.java
libcore/archive/src/main/java/java/util/zip/Inflater.java
libcore/archive/src/main/java/java/util/zip/InflaterInputStream.java
libcore/archive/src/main/java/java/util/zip/ZipEntry.java
libcore/archive/src/main/java/java/util/zip/ZipException.java
libcore/archive/src/main/java/java/util/zip/ZipFile.java
libcore/archive/src/main/java/java/util/zip/ZipInputStream.java
libcore/archive/src/main/java/java/util/zip/ZipOutputStream.java
libcore/archive/src/main/java/org/apache/harmony/archive/internal/nls/Messages.java
libcore/archive/src/main/native/archive/shared/archiveglob.c
libcore/archive/src/main/native/archive/shared/jarfile.c
libcore/archive/src/main/native/archive/shared/zip.c
libcore/archive/src/main/native/archive/shared/zip.h
libcore/archive/src/main/native/archive/unix/makefile
libcore/archive/src/main/native/archive/windows/makefile
libcore/archive/src/main/native/java_util_zip_Adler32.c
libcore/archive/src/main/native/java_util_zip_CRC32.c
libcore/archive/src/main/native/java_util_zip_Deflater.c
libcore/archive/src/main/native/java_util_zip_Inflater.c
libcore/archive/src/main/native/zip/shared/hyzip.nls
libcore/archive/src/main/native/zip/unix/makefile
libcore/archive/src/main/native/zipsup.h
libcore/archive/src/main/native/zlib/unix/makefile
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/AttributesNameTest.java
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/AttributesTest.java
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarEntryTest.java
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarFileTest.java
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarInputStreamTest.java
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarOutputStreamTest.java
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/ManifestTest.java
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPInputStreamTest.java
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPOutputStreamTest.java
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterTest.java
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java
commit 946f165f5b592f4453fd8f2c19766921544d38dd
Author: Jesse Wilson <jessewilson@google.com>
Date: Tue May 12 15:12:55 2009 -0700
Strip @Since Android 1.0 from Archive
commit 3498f216d7e826bfc9c4cc7c0da35830ca239367
Author: Jesse Wilson <jessewilson@google.com>
Date: Tue May 12 15:09:05 2009 -0700
Dalvik archive
commit 62e9db90bc6aa6b5d1c897cccdd616d812672677
Author: Jesse Wilson <jessewilson@google.com>
Date: Tue May 12 15:07:51 2009 -0700
Archive 772995
commit d1bf618681d6badf1b50edaf204a083d3912213d
Author: Jesse Wilson <jessewilson@google.com>
Date: Tue May 12 15:06:36 2009 -0700
Archive 527399
Diffstat (limited to 'luni')
-rw-r--r-- | luni/src/main/java/org/apache/harmony/luni/util/InputStreamExposer.java | 117 | ||||
-rw-r--r-- | luni/src/main/java/org/apache/harmony/luni/util/ThreadLocalCache.java | 103 |
2 files changed, 220 insertions, 0 deletions
diff --git a/luni/src/main/java/org/apache/harmony/luni/util/InputStreamExposer.java b/luni/src/main/java/org/apache/harmony/luni/util/InputStreamExposer.java new file mode 100644 index 0000000..d7c2ab8 --- /dev/null +++ b/luni/src/main/java/org/apache/harmony/luni/util/InputStreamExposer.java @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.luni.util; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Field; +import java.security.AccessController; +import java.security.PrivilegedAction; + +/** + * The class contains static {@link java.io.InputStream} utilities. + */ +public class InputStreamExposer { + + /** + * Provides access to a protected underlying buffer of + * <code>ByteArrayInputStream</code>. + */ + private static final Field BAIS_BUF; + + /** + * Provides access to a protected position in the underlying buffer of + * <code>ByteArrayInputStream</code>. + */ + private static final Field BAIS_POS; + + static { + final Field[] f = new Field[2]; + AccessController.doPrivileged(new PrivilegedAction<Object>() { + public Object run() { + try { + f[0] = ByteArrayInputStream.class.getDeclaredField("buf"); + f[0].setAccessible(true); + f[1] = ByteArrayInputStream.class.getDeclaredField("pos"); + f[1].setAccessible(true); + } catch (NoSuchFieldException nsfe) { + throw new InternalError(nsfe.getLocalizedMessage()); + } + return null; + } + }); + BAIS_BUF = f[0]; + BAIS_POS = f[1]; + } + + /** + * Reads all bytes from {@link java.io.ByteArrayInputStream} using its + * underlying buffer directly. + * + * @return an underlying buffer, if a current position is at the buffer + * beginning, and an end position is at the buffer end, or a copy of + * the underlying buffer part. + */ + private static byte[] expose(ByteArrayInputStream bais) { + byte[] buffer, buf; + int pos; + synchronized (bais) { + int available = bais.available(); + try { + buf = (byte[]) BAIS_BUF.get(bais); + pos = BAIS_POS.getInt(bais); + } catch (IllegalAccessException iae) { + throw new InternalError(iae.getLocalizedMessage()); + } + if (pos == 0 && available == buf.length) { + buffer = buf; + } else { + buffer = new byte[available]; + System.arraycopy(buf, pos, buffer, 0, available); + } + bais.skip(available); + } + return buffer; + } + + /** + * The utility method for reading the whole input stream into a snapshot + * buffer. To speed up the access it works with an underlying buffer for a + * given {@link java.io.ByteArrayInputStream}. + * + * @param is + * the stream to be read. + * @return the snapshot wrapping the buffer where the bytes are read to. + * @throws UnsupportedOperationException if the input stream data cannot be exposed + */ + public static byte[] expose(InputStream is) throws IOException, UnsupportedOperationException { + // BEGIN android-changed + // if (is instanceof ExposedByteArrayInputStream) { + // return ((ExposedByteArrayInputStream) is).expose(); + // } + + if (is.getClass().equals(ByteArrayInputStream.class)) { + return expose((ByteArrayInputStream) is); + } + + // We don't know how to do this + throw new UnsupportedOperationException(); + } +} diff --git a/luni/src/main/java/org/apache/harmony/luni/util/ThreadLocalCache.java b/luni/src/main/java/org/apache/harmony/luni/util/ThreadLocalCache.java new file mode 100644 index 0000000..cdfe0c8 --- /dev/null +++ b/luni/src/main/java/org/apache/harmony/luni/util/ThreadLocalCache.java @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.luni.util; + +import java.lang.ref.SoftReference; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CharsetEncoder; + +/** + * The class extends the functionality of {@link java.lang.ThreadLocal} with + * possibility of discarding the thread local storage content when a heap is + * exhausted. + */ +public class ThreadLocalCache<T> { + + private SoftReference<ThreadLocal<T>> storage = new SoftReference<ThreadLocal<T>>( + null); + + private ThreadLocal<T> getThreadLocal() { + ThreadLocal<T> tls = storage.get(); + if (tls == null) { + tls = new ThreadLocal<T>() { + public T initialValue() { + return ThreadLocalCache.this.initialValue(); + } + }; + storage = new SoftReference<ThreadLocal<T>>(tls); + } + return tls; + } + + /** + * Returns the initial value for the cache for the current thread. + */ + protected T initialValue() { + return null; + } + + /** + * Returns the thread local value of this object. + */ + public T get() { + return getThreadLocal().get(); + } + + /** + * Sets the value of this variable for the current thread. Might be useful + * for expanding the thread local cache. + */ + public void set(T value) { + getThreadLocal().set(value); + } + + /** + * Discards the cache for all threads. + */ + public void remove() { + storage.clear(); + } + + public static ThreadLocalCache<CharsetDecoder> utf8Decoder = new ThreadLocalCache<CharsetDecoder>() { + protected CharsetDecoder initialValue() { + return Charset.forName("UTF-8").newDecoder(); + } + }; + + public static ThreadLocalCache<CharsetEncoder> utf8Encoder = new ThreadLocalCache<CharsetEncoder>() { + protected CharsetEncoder initialValue() { + return Charset.forName("UTF-8").newEncoder(); + } + }; + + public static ThreadLocalCache<java.nio.ByteBuffer> byteBuffer = new ThreadLocalCache<java.nio.ByteBuffer>() { + protected java.nio.ByteBuffer initialValue() { + return java.nio.ByteBuffer.allocate(72); // >= + // Manifest.LINE_LENGTH_LIMIT + } + }; + + public static ThreadLocalCache<CharBuffer> charBuffer = new ThreadLocalCache<CharBuffer>() { + protected CharBuffer initialValue() { + return CharBuffer.allocate(72); // no specific requirement + } + }; + +} |