有没有一种方法可以在不使用 ObjectInputStream 的情况下将 FileInputStream 转换为 Object?
Is there a way I can convert a FileInputStream to an Object without using ObjectInputStream?
我想知道是否可以在不使用 ObjectInputStream 的情况下从 FileInputStream 获取 object。
我为什么要这样做?我最近一直在做一个项目,它从游戏中读取 .DAT 文件并将它们转换为 .OBJ - 然而这些 .DAT 文件有一个问题:它们的流 header 总是 0xFACEAF0E。有没有办法绕过 ObjectInputStream 在流 headers 上的限制并从其中一个文件中获取 Object?
这是我需要帮助的代码。
package xan_code;
/*
* Purpose: Determine file extension and run it through the code to move it to an OBJ.
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import main.BeginConversion; //Import the conversion code. Returns a string based on the file
import xan_code.dathandler.ReadBinary; //Get the .DAT reading functions
@SuppressWarnings("serial")
public class HandleFiles extends Main { //Extend main to get the log from the opener UI.
static BeginConversion converter = new BeginConversion(); //Get the converter for XML files since this will also read XMLs derived from the .DAT
static String DatText = ""; //The "text" to return for the .DAT (To pack into the decoded file)
static Object _object; //THIS IS THE VARIABLE OF THE OBJECT I NEED IN ORDER TO CONVERT THE .DAT TO A MODEL
@SuppressWarnings("static-access")
public static String convert(File file, boolean isXML, FileInputStream FIS) { //Convert. Passes in the .DAT or .XML file, a boolean to whether or not its extension is .XML, and the FileInputStream from file
if (isXML) { //If it's an XML
String xml = ""; //This is the text to store the XML as a string
String obj = ""; //This is the text to store the WaveFront OBJ (Model format) as a string
try {
xml = new Scanner(file).useDelimiter("\Z").next(); //use the scanner to get the string of the XML
obj = converter.BeginConvert(xml); //Pass the XML into the java files required to read from the XML and convert it to an OBJ. They return the text from an OBJ file.
} catch (Exception e) {
//Exceptions are handled before, though to be safe...
e.printStackTrace();
}
return obj; //Return that text to Main so I can create the file.
} else { //We have a .DAT
try {
//HELP REQUIRED HERE. NEED TO GET _object FROM THE FILE WITHOUT USING ObjectInputStream
DatText = ReadBinary.Read(file, _object); //Right now this actually returns the text of an XML, but that doesn't matter much at the moment.
} catch (IOException e) {
DatText = "Unexpected error while reading .DAT file!";
e.printStackTrace();
}
return DatText;
}
}
}
您可以使用 FileInputStream 删除 ObjectInputStream 不应该首先看到的额外字节。
static class MyObject implements Serializable{
int i;
}
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
MyObject obj = new MyObject();
obj.i = 77;
File testFile = new File("test.dat");
try (FileOutputStream fos = new FileOutputStream(testFile)) {
fos.write(new byte[]{(byte) 0xFA, (byte) 0xCE, (byte) 0xAF, (byte) 0x0E});
try (ObjectOutputStream oos = new ObjectOutputStream(fos)) {
oos.writeObject(obj);
}
}
try (FileInputStream fis = new FileInputStream(testFile)) {
byte b4[] = new byte[4];
fis.read(b4);
try (ObjectInputStream ois = new ObjectInputStream(fis)) {
MyObject newObj = (MyObject) ois.readObject();
System.out.println("newObj.i = " + newObj.i);
}
}
}
我想知道是否可以在不使用 ObjectInputStream 的情况下从 FileInputStream 获取 object。
我为什么要这样做?我最近一直在做一个项目,它从游戏中读取 .DAT 文件并将它们转换为 .OBJ - 然而这些 .DAT 文件有一个问题:它们的流 header 总是 0xFACEAF0E。有没有办法绕过 ObjectInputStream 在流 headers 上的限制并从其中一个文件中获取 Object?
这是我需要帮助的代码。
package xan_code;
/*
* Purpose: Determine file extension and run it through the code to move it to an OBJ.
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import main.BeginConversion; //Import the conversion code. Returns a string based on the file
import xan_code.dathandler.ReadBinary; //Get the .DAT reading functions
@SuppressWarnings("serial")
public class HandleFiles extends Main { //Extend main to get the log from the opener UI.
static BeginConversion converter = new BeginConversion(); //Get the converter for XML files since this will also read XMLs derived from the .DAT
static String DatText = ""; //The "text" to return for the .DAT (To pack into the decoded file)
static Object _object; //THIS IS THE VARIABLE OF THE OBJECT I NEED IN ORDER TO CONVERT THE .DAT TO A MODEL
@SuppressWarnings("static-access")
public static String convert(File file, boolean isXML, FileInputStream FIS) { //Convert. Passes in the .DAT or .XML file, a boolean to whether or not its extension is .XML, and the FileInputStream from file
if (isXML) { //If it's an XML
String xml = ""; //This is the text to store the XML as a string
String obj = ""; //This is the text to store the WaveFront OBJ (Model format) as a string
try {
xml = new Scanner(file).useDelimiter("\Z").next(); //use the scanner to get the string of the XML
obj = converter.BeginConvert(xml); //Pass the XML into the java files required to read from the XML and convert it to an OBJ. They return the text from an OBJ file.
} catch (Exception e) {
//Exceptions are handled before, though to be safe...
e.printStackTrace();
}
return obj; //Return that text to Main so I can create the file.
} else { //We have a .DAT
try {
//HELP REQUIRED HERE. NEED TO GET _object FROM THE FILE WITHOUT USING ObjectInputStream
DatText = ReadBinary.Read(file, _object); //Right now this actually returns the text of an XML, but that doesn't matter much at the moment.
} catch (IOException e) {
DatText = "Unexpected error while reading .DAT file!";
e.printStackTrace();
}
return DatText;
}
}
}
您可以使用 FileInputStream 删除 ObjectInputStream 不应该首先看到的额外字节。
static class MyObject implements Serializable{
int i;
}
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
MyObject obj = new MyObject();
obj.i = 77;
File testFile = new File("test.dat");
try (FileOutputStream fos = new FileOutputStream(testFile)) {
fos.write(new byte[]{(byte) 0xFA, (byte) 0xCE, (byte) 0xAF, (byte) 0x0E});
try (ObjectOutputStream oos = new ObjectOutputStream(fos)) {
oos.writeObject(obj);
}
}
try (FileInputStream fis = new FileInputStream(testFile)) {
byte b4[] = new byte[4];
fis.read(b4);
try (ObjectInputStream ois = new ObjectInputStream(fis)) {
MyObject newObj = (MyObject) ois.readObject();
System.out.println("newObj.i = " + newObj.i);
}
}
}