fix: 去除RMIServer中的重复代码段

This commit is contained in:
qi4l
2026-04-29 08:52:00 +08:00
parent 32432ff2bf
commit f33de9d82c
2 changed files with 68 additions and 0 deletions
@@ -0,0 +1,25 @@
package com.qi4l.JYso.gadgets.utils;
import sun.rmi.transport.TransportConstants;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class JRMPUtils {
public static DataOutputStream handshake(DataInputStream in, Socket s) throws IOException {
int magic = in.readInt();
short version = in.readShort();
if (magic != TransportConstants.Magic || version != TransportConstants.Version) {
s.close();
return null;
}
OutputStream sockOut = s.getOutputStream();
BufferedOutputStream bufOut = new BufferedOutputStream(sockOut);
return new DataOutputStream(bufOut);
}
}
@@ -0,0 +1,43 @@
package com.qi4l.JYso.gadgets.utils;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLClassLoader;
public class MarshalOutputStream extends ObjectOutputStream {
private final URL sendUrl;
public MarshalOutputStream(OutputStream out, URL u) throws IOException {
super(out);
this.sendUrl = u;
}
public MarshalOutputStream(OutputStream out) throws IOException {
super(out);
this.sendUrl = null;
}
@Override
protected void annotateClass(Class<?> cl) throws IOException {
if (this.sendUrl != null) {
writeObject(this.sendUrl.toString());
} else if (!(cl.getClassLoader() instanceof URLClassLoader)) {
writeObject(null);
} else {
URL[] us = ((URLClassLoader) cl.getClassLoader()).getURLs();
StringBuilder cb = new StringBuilder();
for (URL u : us) {
cb.append(u.toString());
}
writeObject(cb.toString());
}
}
@Override
protected void annotateProxyClass(Class<?> cl) throws IOException {
annotateClass(cl);
}
}