|
1 | 1 | /*
|
2 | 2 |
|
3 | 3 | This file is part of the iText (R) project.
|
4 |
| - Copyright (c) 1998-2017 iText Group NV |
| 4 | + Copyright (c) 1998-2020 iText Group NV |
5 | 5 | Authors: Bruno Lowagie, Paulo Soares, et al.
|
6 | 6 |
|
7 | 7 | This program is free software; you can redistribute it and/or modify
|
@@ -43,24 +43,18 @@ This file is part of the iText (R) project.
|
43 | 43 | */
|
44 | 44 | package com.itextpdf.io.source;
|
45 | 45 |
|
46 |
| -import org.slf4j.Logger; |
47 |
| -import org.slf4j.LoggerFactory; |
48 |
| - |
49 | 46 | import java.io.IOException;
|
50 | 47 | import java.io.NotSerializableException;
|
51 | 48 | import java.io.ObjectInputStream;
|
52 | 49 | import java.io.ObjectOutputStream;
|
53 | 50 | import java.io.Serializable;
|
54 |
| -import java.lang.reflect.InvocationTargetException; |
55 | 51 | import java.lang.reflect.Method;
|
56 | 52 | import java.nio.Buffer;
|
57 | 53 | import java.nio.BufferUnderflowException;
|
58 | 54 | import java.security.AccessController;
|
59 | 55 | import java.security.PrivilegedAction;
|
60 |
| - |
61 |
| -import java.nio.ByteBuffer; |
62 |
| -import java.util.Objects; |
63 |
| -import java.lang.reflect.Field; |
| 56 | +import org.slf4j.Logger; |
| 57 | +import org.slf4j.LoggerFactory; |
64 | 58 |
|
65 | 59 |
|
66 | 60 | /**
|
@@ -159,7 +153,7 @@ public void close() throws java.io.IOException {
|
159 | 153 | static {
|
160 | 154 | final Object hack = AccessController.doPrivileged(new PrivilegedAction<Object>() {
|
161 | 155 | public Object run() {
|
162 |
| - return unmapHackImpl(); |
| 156 | + return BufferCleaner.unmapHackImpl(); |
163 | 157 | }
|
164 | 158 | });
|
165 | 159 | if (hack instanceof BufferCleaner) {
|
@@ -226,74 +220,4 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
|
226 | 220 | }
|
227 | 221 | }
|
228 | 222 |
|
229 |
| - /* |
230 |
| - * Licensed to the Apache Software Foundation (ASF) under one or more |
231 |
| - * contributor license agreements. See the NOTICE file distributed with |
232 |
| - * this work for additional information regarding copyright ownership. |
233 |
| - * The ASF licenses this file to You under the Apache License, Version 2.0 |
234 |
| - * (the "License"); you may not use this file except in compliance with |
235 |
| - * the License. You may obtain a copy of the License at |
236 |
| - * |
237 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
238 |
| - * |
239 |
| - * Unless required by applicable law or agreed to in writing, software |
240 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
241 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
242 |
| - * See the License for the specific language governing permissions and |
243 |
| - * limitations under the License. |
244 |
| - * |
245 |
| - * NOTE: that this code was edited since original code is compatible with android sdk not lower than 26. |
246 |
| - * This edited code has been verified to be compatible with android sdk 19. |
247 |
| - */ |
248 |
| - |
249 |
| - private static class BufferCleaner { |
250 |
| - Class<?> unmappableBufferClass; |
251 |
| - final Method method; |
252 |
| - final Object theUnsafe; |
253 |
| - |
254 |
| - BufferCleaner(final Class<?> unmappableBufferClass, final Method method, final Object theUnsafe) { |
255 |
| - this.unmappableBufferClass = unmappableBufferClass; |
256 |
| - this.method = method; |
257 |
| - this.theUnsafe = theUnsafe; |
258 |
| - } |
259 |
| - |
260 |
| - void freeBuffer(String resourceDescription, final ByteBuffer buffer) throws IOException { |
261 |
| - assert Objects.equals(void.class, method.getReturnType()); |
262 |
| - assert method.getParameterTypes().length == 1; |
263 |
| - assert Objects.equals(ByteBuffer.class, method.getParameterTypes()[0]); |
264 |
| - if (!buffer.isDirect()) { |
265 |
| - throw new IllegalArgumentException("unmapping only works with direct buffers"); |
266 |
| - } |
267 |
| - if (!unmappableBufferClass.isInstance(buffer)) { |
268 |
| - throw new IllegalArgumentException("buffer is not an instance of " + unmappableBufferClass.getName()); |
269 |
| - } |
270 |
| - final Throwable error = AccessController.doPrivileged(new PrivilegedAction<Throwable>() { |
271 |
| - public Throwable run() { |
272 |
| - try { |
273 |
| - method.invoke(theUnsafe, buffer); |
274 |
| - return null; |
275 |
| - } catch (IllegalAccessException | InvocationTargetException e) { |
276 |
| - return e; |
277 |
| - } |
278 |
| - } |
279 |
| - }); |
280 |
| - if (error != null) { |
281 |
| - throw new IOException("Unable to unmap the mapped buffer: " + resourceDescription, error); |
282 |
| - } |
283 |
| - } |
284 |
| - } |
285 |
| - |
286 |
| - private static Object unmapHackImpl() { |
287 |
| - try { |
288 |
| - // *** sun.misc.Unsafe unmapping (Java 9+) *** |
289 |
| - final Class<?> unsafeClass = Class.forName("sun.misc.Unsafe"); |
290 |
| - final Method method = unsafeClass.getDeclaredMethod("invokeCleaner", ByteBuffer.class); |
291 |
| - final Field f = unsafeClass.getDeclaredField("theUnsafe"); |
292 |
| - f.setAccessible(true); |
293 |
| - final Object theUnsafe = f.get(null); |
294 |
| - return new BufferCleaner(ByteBuffer.class, method, theUnsafe); |
295 |
| - } catch (Exception e) { |
296 |
| - return e.getMessage(); |
297 |
| - } |
298 |
| - } |
299 | 223 | }
|
0 commit comments