Skip to content

Commit

Permalink
restore changes that removes java 8 support in comit 5a81749
Browse files Browse the repository at this point in the history
  • Loading branch information
xexes authored and asturio committed Feb 22, 2024
1 parent 4e9da4e commit 289347a
Showing 1 changed file with 88 additions and 39 deletions.
127 changes: 88 additions & 39 deletions openpdf/src/main/java/com/lowagie/text/pdf/MappedRandomAccessFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.AccessController;
import java.security.PrivilegedAction;

/**
* A {@link java.nio.MappedByteBuffer} wrapped as a {@link java.io.RandomAccessFile}
Expand All @@ -66,10 +70,10 @@
* Created on 6.9.2006
*/
public class MappedRandomAccessFile implements AutoCloseable {

private MappedByteBuffer mappedByteBuffer = null;
private FileChannel channel = null;

/**
* Constructs a new MappedRandomAccessFile instance
* @param filename String
Expand All @@ -79,7 +83,7 @@ public class MappedRandomAccessFile implements AutoCloseable {
*/
public MappedRandomAccessFile(String filename, String mode)
throws IOException {

if (mode.equals("rw"))
init(
new java.io.RandomAccessFile(filename, mode).getChannel(),
Expand All @@ -88,9 +92,9 @@ public MappedRandomAccessFile(String filename, String mode)
init(
new FileInputStream(filename).getChannel(),
FileChannel.MapMode.READ_ONLY);

}

/**
* initializes the channel and mapped bytebuffer
* @param channel FileChannel
Expand All @@ -114,7 +118,7 @@ private void init(FileChannel channel, FileChannel.MapMode mapMode)
public FileChannel getChannel() {
return channel;
}

/**
* @see java.io.RandomAccessFile#read()
* @return int next integer or -1 on EOF
Expand All @@ -123,13 +127,13 @@ public int read() {
try {
byte b = mappedByteBuffer.get();
int n = b & 0xff;

return n;
} catch (BufferUnderflowException e) {
return -1; // EOF
}
}

/**
* @see java.io.RandomAccessFile#read(byte[], int, int)
* @param bytes byte[]
Expand All @@ -149,31 +153,31 @@ public int read(byte[] bytes, int off, int len) {
mappedByteBuffer.get(bytes, off, len);
return len;
}

/**
* @see java.io.RandomAccessFile#getFilePointer()
* @return long
*/
public long getFilePointer() {
return mappedByteBuffer.position();
}

/**
* @see java.io.RandomAccessFile#seek(long)
* @param pos long position
*/
public void seek(long pos) {
mappedByteBuffer.position((int) pos);
}

/**
* @see java.io.RandomAccessFile#length()
* @return long length
*/
public long length() {
return mappedByteBuffer.limit();
}

/**
* Cleans the mapped bytebuffer and closes the channel
*
Expand All @@ -187,7 +191,7 @@ public void close() throws IOException {
channel.close();
channel = null;
}

/**
* invokes the close method
* @see java.lang.Object#finalize()
Expand All @@ -196,34 +200,79 @@ protected void finalize() throws Throwable {
close();
super.finalize();
}



/**
* invokes the clean method on the ByteBuffer's cleaner
* @param buffer ByteBuffer
* @return boolean true on success
*/
public static boolean clean(final java.nio.ByteBuffer buffer) {
if (buffer == null || !buffer.isDirect()) {
return false;
}
return cleanJava11(buffer);
* invokes the clean method on the ByteBuffer's cleaner
*
* @param buffer ByteBuffer
* @return boolean true on success
*/
public static boolean clean(final java.nio.ByteBuffer buffer) {
if (buffer == null || !buffer.isDirect()) {
return false;
}



if (cleanJava9(buffer)) {
return true;

}
return cleanOldsJDK(buffer);
}






private static boolean cleanOldsJDK(final java.nio.ByteBuffer buffer) {
Boolean b = AccessController.doPrivileged((PrivilegedAction<Boolean>) () -> {
Boolean success = Boolean.FALSE;
try {
Method getCleanerMethod = buffer.getClass()
.getMethod("cleaner", (Class[]) null);
if (!getCleanerMethod.isAccessible()) {
getCleanerMethod.setAccessible(true);
}
Object cleaner = getCleanerMethod.invoke(buffer, (Object[])null);
Method clean = cleaner.getClass().getMethod("clean", (Class[])null);
clean.invoke(cleaner, (Object[])null);
success = Boolean.TRUE;
} catch (Exception e) {
// Ignore
}
return success;
});

return b;
}



private static boolean cleanJava9(final java.nio.ByteBuffer buffer) {
Boolean b = AccessController.doPrivileged((PrivilegedAction<Boolean>) () -> {
Boolean success = Boolean.FALSE;
try {
final Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
final Field theUnsafeField = unsafeClass.getDeclaredField("theUnsafe");
theUnsafeField.setAccessible(true);
final Object theUnsafe = theUnsafeField.get(null);
final Method invokeCleanerMethod = unsafeClass
.getMethod("invokeCleaner", ByteBuffer.class);
invokeCleanerMethod.invoke(theUnsafe, buffer);
success = Boolean.TRUE;
} catch (Exception ignore) {
// Ignore
}
return success;
});

return b;
}


}

private static boolean cleanJava11(final ByteBuffer buffer) {
Boolean success = Boolean.FALSE;
try {
MethodHandles.Lookup lookup = MethodHandles.lookup();
Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
MethodHandle methodHandle = lookup.findStatic(unsafeClass, "getUnsafe", MethodType.methodType(unsafeClass));
Object theUnsafe = methodHandle.invoke();
MethodHandle invokeCleanerMethod = lookup.findVirtual(unsafeClass, "invokeCleaner", MethodType.methodType(void.class, ByteBuffer.class));
invokeCleanerMethod.invoke(theUnsafe, buffer);
success = Boolean.TRUE;
} catch (Throwable ignore) {
// Ignore
}
return success;
}

}

0 comments on commit 289347a

Please sign in to comment.