Skip to content

Commit

Permalink
FileReader
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed Jun 3, 2024
1 parent 8042d3b commit 6b3a182
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void main(String[] args) throws IOException {
writer2.write("Goodbye World!");
writer2.close();

FileReader reader = new FileReader(new File("build/test/test.txt"), StandardCharsets.UTF_8);
FileReader reader = new FileReader("build/test/test.txt", StandardCharsets.UTF_8);
StringBuilder sb = new StringBuilder();
int c;
while ((c = reader.read()) != -1) {
Expand Down
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;

//@Adapter("Ljava/io/FileWriter;")
public class J_I_FileWriter {
private static final MethodHandles.Lookup IMPL_LOOKUP = Utils.getImplLookup();
private static final MethodHandle seGetter;
Expand Down Expand Up @@ -46,15 +45,6 @@ public static void setCharset(FileWriter fw, Charset charset) {
}
}

// public J_I_FileWriter(File file, Charset charset) throws IOException {
// super(new FileOutputStream(file), charset);
// }
//
// public J_I_FileWriter(File file, Charset charset, boolean append) throws IOException {
// super(new FileOutputStream(file, append), charset);
// }


@Modify(ref = @Ref(value = "java/io/FileWriter", 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);
Expand Down

0 comments on commit 6b3a182

Please sign in to comment.