pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject [1/2] pdfbox-jbig2 git commit: PDFBOX-4290: reformat
Date Sun, 19 Aug 2018 08:56:34 GMT
Repository: pdfbox-jbig2
Updated Branches:
  refs/heads/master 52c02bdcc -> 36b81329e


PDFBOX-4290: reformat


Project: http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/repo
Commit: http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/commit/2f8c2f2f
Tree: http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/tree/2f8c2f2f
Diff: http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/diff/2f8c2f2f

Branch: refs/heads/master
Commit: 2f8c2f2f3f316f1d7c8bd30a9d650161994c8996
Parents: e36da15
Author: Tilman Hausherr <tilman@snafu.de>
Authored: Sun Aug 19 10:50:15 2018 +0200
Committer: Tilman Hausherr <tilman@snafu.de>
Committed: Sun Aug 19 10:53:18 2018 +0200

----------------------------------------------------------------------
 .gitignore                                      |  15 +--
 .../jbig2/util/cache/SoftReferenceCache.java    | 117 ++++++++++---------
 .../util/cache/SoftReferenceCacheTest.java      |  57 +++++++++
 3 files changed, 126 insertions(+), 63 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/2f8c2f2f/.gitignore
----------------------------------------------------------------------
diff --git a/.gitignore b/.gitignore
index 79c51d9..9318c34 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,7 +1,8 @@
-.idea/
-.settings/
-.project
-.classpath
-atlassian-ide-plugin.xml
-levigo-jbig2-imageio.iml
-target/
+.idea/
+.settings/
+.project
+.classpath
+atlassian-ide-plugin.xml
+levigo-jbig2-imageio.iml
+target/
+*.iml

http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/2f8c2f2f/src/main/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCache.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCache.java b/src/main/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCache.java
index 8374848..9ad7027 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCache.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCache.java
@@ -1,56 +1,61 @@
-/**
- * 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.pdfbox.jbig2.util.cache;
-
-import java.lang.ref.SoftReference;
-import java.util.HashMap;
-
-public class SoftReferenceCache implements Cache
-{
-
-    private HashMap<Object, SoftReference<?>> cache = new HashMap<Object,
SoftReference<?>>();
-
-    public Object put(Object key, Object value, int sizeEstimate)
-    {
-        SoftReference<Object> softReference = new SoftReference<Object>(value);
-        SoftReference<?> oldValue = cache.put(key, softReference);
-        return getValueNullSafe(oldValue);
-    }
-
-    public Object get(Object key)
-    {
-        SoftReference<?> softReference = cache.get(key);
-        return getValueNullSafe(softReference);
-    }
-
-    public void clear()
-    {
-        cache.clear();
-    }
-
-    public Object remove(Object key)
-    {
-        SoftReference<?> removedObj = cache.remove(key);
-        return getValueNullSafe(removedObj);
-    }
-
-    private Object getValueNullSafe(SoftReference<?> softReference)
-    {
-        return softReference == null ? null : softReference.get();
-    }
-}
+/**
+ * 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.pdfbox.jbig2.util.cache;
+
+import java.lang.ref.SoftReference;
+import java.util.Map;
+import java.util.WeakHashMap;
+
+public class SoftReferenceCache implements Cache
+{
+
+    private final Map<Object, SoftReference<?>> cache = new WeakHashMap<Object,
SoftReference<?>>();
+
+    @Override
+    public Object put(Object key, Object value, int sizeEstimate)
+    {
+        SoftReference<Object> softReference = new SoftReference<Object>(value);
+        SoftReference<?> oldValue = cache.put(key, softReference);
+        return getValueNullSafe(oldValue);
+    }
+
+    @Override
+    public Object get(Object key)
+    {
+        SoftReference<?> softReference = cache.get(key);
+        return getValueNullSafe(softReference);
+    }
+
+    @Override
+    public void clear()
+    {
+        cache.clear();
+    }
+
+    @Override
+    public Object remove(Object key)
+    {
+        SoftReference<?> removedObj = cache.remove(key);
+        return getValueNullSafe(removedObj);
+    }
+
+    private Object getValueNullSafe(SoftReference<?> softReference)
+    {
+        return softReference == null ? null : softReference.get();
+    }
+}

http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/2f8c2f2f/src/test/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCacheTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCacheTest.java
b/src/test/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCacheTest.java
new file mode 100644
index 0000000..878de5b
--- /dev/null
+++ b/src/test/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCacheTest.java
@@ -0,0 +1,57 @@
+/**
+ * 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.pdfbox.jbig2.util.cache;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class SoftReferenceCacheTest
+{
+    private static final int KB = 1024;
+    private static final int MB = KB * KB;
+
+    /**
+     * Should not throw an OutOfMemoryError
+     */
+    @Test
+    public void putDoesNotLeakMemory()
+    {
+        Cache cache = CacheFactory.getCache();
+        cache.clear();
+        long maxHeapBytes = Runtime.getRuntime().maxMemory();
+        int halfEntrySizeEstimate = 8 * MB;
+
+        for (int i = 0; i < (maxHeapBytes / halfEntrySizeEstimate) * 2; i++)
+        {
+            cache.put(new Long[KB][KB], new Long[KB][KB], halfEntrySizeEstimate);
+        }
+    }
+
+    @Test
+    public void putAndGet()
+    {
+        Cache cache = CacheFactory.getCache();
+        cache.clear();
+        Object key = new Object();
+        Object value = new Object();
+
+        cache.put(key, value, 0);
+
+        Assert.assertEquals(value, cache.get(key));
+    }
+}


Mime
View raw message