-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8042d3b
commit 6b3a182
Showing
3 changed files
with
63 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 62 additions & 15 deletions
77
java-api/src/java11/java/xyz/wagyourtail/jvmdg/j11/stub/java_base/J_I_FileReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,79 @@ | ||
package xyz.wagyourtail.jvmdg.j11.stub.java_base; | ||
|
||
|
||
import xyz.wagyourtail.jvmdg.version.Adapter; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.Type; | ||
import org.objectweb.asm.tree.InsnList; | ||
import org.objectweb.asm.tree.InsnNode; | ||
import org.objectweb.asm.tree.MethodInsnNode; | ||
import org.objectweb.asm.tree.MethodNode; | ||
import xyz.wagyourtail.jvmdg.util.Utils; | ||
import xyz.wagyourtail.jvmdg.version.Modify; | ||
import xyz.wagyourtail.jvmdg.version.Ref; | ||
|
||
import java.io.*; | ||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.CharsetDecoder; | ||
import java.nio.charset.CodingErrorAction; | ||
|
||
@Adapter("Ljava/io/FileReader;") | ||
public class J_I_FileReader extends InputStreamReader { | ||
|
||
public J_I_FileReader(String fileName) throws FileNotFoundException { | ||
super(new FileInputStream(fileName)); | ||
public class J_I_FileReader { | ||
private static final MethodHandles.Lookup IMPL_LOOKUP = Utils.getImplLookup(); | ||
private static final MethodHandle sdGetter; | ||
private static final MethodHandle decSetter; | ||
static { | ||
MethodHandle seGet = null; | ||
MethodHandle decSet = null; | ||
try { | ||
Class<?> sd = Class.forName("sun.nio.cs.StreamDecoder"); | ||
seGet = IMPL_LOOKUP.findGetter(FileReader.class, "sd", sd); | ||
decSet = IMPL_LOOKUP.findSetter(sd, "decoder", CharsetDecoder.class); | ||
} catch (Throwable t) { | ||
t.printStackTrace(); | ||
} | ||
sdGetter = seGet; | ||
decSetter = decSet; | ||
} | ||
|
||
public J_I_FileReader(File file) throws FileNotFoundException { | ||
super(new FileInputStream(file)); | ||
public static void setCharset(FileReader fw, Charset charset) { | ||
try { | ||
Object sd = sdGetter.invoke(fw); | ||
decSetter.invoke(sd, charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE)); | ||
} catch (Throwable t) { | ||
Utils.sneakyThrow(t); | ||
} | ||
} | ||
|
||
public J_I_FileReader(FileDescriptor fd) { | ||
super(new FileInputStream(fd)); | ||
} | ||
@Modify(ref = @Ref(value = "java/io/FileReader", member = "<init>", desc = "(Ljava/lang/String;Ljava/nio/charset/Charset;)V")) | ||
public static void init1(MethodNode mnode, int i) { | ||
MethodInsnNode node = (MethodInsnNode) mnode.instructions.get(i); | ||
Type firstArgType = Type.getArgumentTypes(node.desc)[0]; | ||
InsnList list = new InsnList(); | ||
// stack: (U) FileReader, String, Charset | ||
list.add(new InsnNode(Opcodes.DUP_X2)); | ||
// stack: Charset, (U) FileReader, String, Charset | ||
list.add(new InsnNode(Opcodes.POP)); | ||
// stack: Charset, (U) FileReader, String | ||
list.add(new InsnNode(Opcodes.DUP2)); | ||
// stack: Charset, (U) FileReader, String, (U) FileReader, String | ||
// call init | ||
list.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/io/FileReader", "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, firstArgType), false)); | ||
// stack: Charset, FileReader, String | ||
list.add(new InsnNode(Opcodes.POP)); | ||
// stack: Charset, FileReader | ||
list.add(new InsnNode(Opcodes.SWAP)); | ||
// stack: FileReader, Charset | ||
list.add(new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getType(J_I_FileReader.class).getInternalName(), "setCharset", "(Ljava/io/FileReader;Ljava/nio/charset/Charset;)V", false)); | ||
|
||
public J_I_FileReader(String fileName, Charset charset) throws IOException { | ||
super(new FileInputStream(fileName), charset); | ||
mnode.instructions.insert(node, list); | ||
mnode.instructions.remove(node); | ||
} | ||
|
||
public J_I_FileReader(File file, Charset charset) throws IOException { | ||
super(new FileInputStream(file), charset); | ||
|
||
@Modify(ref = @Ref(value = "java/io/FileReader", member = "<init>", desc = "(Ljava/io/File;Ljava/nio/charset/Charset;)V")) | ||
public static void init3(MethodNode mnode, int i) { | ||
init1(mnode, i); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters