Skip to content

Commit ae44901

Browse files
committed
Separate code licensed under Apache License into separate file
This is more transparent and also makes copyright update script work DEVSIX-3611
1 parent e01f58e commit ae44901

File tree

2 files changed

+86
-80
lines changed

2 files changed

+86
-80
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
* NOTE: that this code was edited since original code is compatible with android sdk not lower than 26.
18+
* This edited code has been verified to be compatible with android sdk 19.
19+
*/
20+
21+
package com.itextpdf.io.source;
22+
23+
import java.io.IOException;
24+
import java.lang.reflect.Field;
25+
import java.lang.reflect.InvocationTargetException;
26+
import java.lang.reflect.Method;
27+
import java.nio.ByteBuffer;
28+
import java.security.AccessController;
29+
import java.security.PrivilegedAction;
30+
import java.util.Objects;
31+
32+
33+
class BufferCleaner {
34+
Class<?> unmappableBufferClass;
35+
final Method method;
36+
final Object theUnsafe;
37+
38+
BufferCleaner(final Class<?> unmappableBufferClass, final Method method, final Object theUnsafe) {
39+
this.unmappableBufferClass = unmappableBufferClass;
40+
this.method = method;
41+
this.theUnsafe = theUnsafe;
42+
}
43+
44+
void freeBuffer(String resourceDescription, final java.nio.ByteBuffer buffer) throws IOException {
45+
assert Objects.equals(void.class, method.getReturnType());
46+
assert method.getParameterTypes().length == 1;
47+
assert Objects.equals(ByteBuffer.class, method.getParameterTypes()[0]);
48+
if (!buffer.isDirect()) {
49+
throw new IllegalArgumentException("unmapping only works with direct buffers");
50+
}
51+
if (!unmappableBufferClass.isInstance(buffer)) {
52+
throw new IllegalArgumentException("buffer is not an instance of " + unmappableBufferClass.getName());
53+
}
54+
final Throwable error = AccessController.doPrivileged(new PrivilegedAction<Throwable>() {
55+
public Throwable run() {
56+
try {
57+
method.invoke(theUnsafe, buffer);
58+
return null;
59+
} catch (IllegalAccessException | InvocationTargetException e) {
60+
return e;
61+
}
62+
}
63+
});
64+
if (error != null) {
65+
throw new IOException("Unable to unmap the mapped buffer: " + resourceDescription, error);
66+
}
67+
}
68+
69+
static Object unmapHackImpl() {
70+
try {
71+
// *** sun.misc.Unsafe unmapping (Java 9+) ***
72+
final Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
73+
final Method method = unsafeClass.getDeclaredMethod("invokeCleaner", ByteBuffer.class);
74+
final Field f = unsafeClass.getDeclaredField("theUnsafe");
75+
f.setAccessible(true);
76+
final Object theUnsafe = f.get(null);
77+
return new BufferCleaner(ByteBuffer.class, method, theUnsafe);
78+
} catch (Exception e) {
79+
return e.getMessage();
80+
}
81+
}
82+
}

io/src/main/java/com/itextpdf/io/source/ByteBufferRandomAccessSource.java

Lines changed: 4 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
33
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
55
Authors: Bruno Lowagie, Paulo Soares, et al.
66
77
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.
4343
*/
4444
package com.itextpdf.io.source;
4545

46-
import org.slf4j.Logger;
47-
import org.slf4j.LoggerFactory;
48-
4946
import java.io.IOException;
5047
import java.io.NotSerializableException;
5148
import java.io.ObjectInputStream;
5249
import java.io.ObjectOutputStream;
5350
import java.io.Serializable;
54-
import java.lang.reflect.InvocationTargetException;
5551
import java.lang.reflect.Method;
5652
import java.nio.Buffer;
5753
import java.nio.BufferUnderflowException;
5854
import java.security.AccessController;
5955
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;
6458

6559

6660
/**
@@ -159,7 +153,7 @@ public void close() throws java.io.IOException {
159153
static {
160154
final Object hack = AccessController.doPrivileged(new PrivilegedAction<Object>() {
161155
public Object run() {
162-
return unmapHackImpl();
156+
return BufferCleaner.unmapHackImpl();
163157
}
164158
});
165159
if (hack instanceof BufferCleaner) {
@@ -226,74 +220,4 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
226220
}
227221
}
228222

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-
}
299223
}

0 commit comments

Comments
 (0)