[英]Storing Byte Array inside an Object, and then converting it to ObjectOutputStream? (Stuck)
我有點不解決這個問題。
我有這個FileDetails類,它存儲文件的詳細信息/元數據以及字節數組中的完整文件。 我想在ObjectOutputStream中跨網絡發送FileDetails對象,接收器將簡單地讀取文件並將其強制轉換回FileDetails。
這是代碼:
class FileDetails {
private String fileName;
private long fileSize;
private byte[] fileData;
public FileDetails(String fileName, long fileSize, byte[] fileData) {
this.fileName = fileName;
this.fileSize = fileSize;
this.fileData = fileData;
}
public String getFileName() {
return fileName;
}
public long getFileSize() {
return fileSize;
}
public byte[] getFileData() {
return fileData;
}
}
File file = new File("C://test.dat");
RandomAccessFile randFileAccess = new RandomAccessFile(file, "r");
byte[] buff = new byte[(int) file.length()];
randFileAccess.readFully(buff);
FileDetails fd = new FileDetails(file.getname(), file.length(); buff);
FileOutputStream fos = = new FileOutputStream(C://oos.dat);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(fd);
oos.write(buff);
問題是文件“test.dat”非常大,並且一次性將其完全讀入緩沖區(非常大)並不是最佳選擇。 我本可以將文件讀入塊中的緩沖區,但這需要我創建文件並將數據保存到磁盤中,這是我無法做到的,因為FileDetails對象需要字節數組。
我怎么解決這個問題? 我只想要這種方法,即在FileDetails對象中將數據存儲為字節數組,然后將其轉換為ObjectOutputStream,因為我將附加一個附加了ObjectOutStream文件的mp3文件並通過互聯網發送它。
任何建議? 或者替代方法?
編輯: 其實我正在開發一個Android應用程序。 它將文件的元數據與字節數組中的文件數據一起存儲在FileDetails對象中。 此FileDetails對象將轉換為ObjectOutputStream文件。 現在,在此ObjectOutputStream文件前面附加了一個特定的mp3文件,該文件用於識別該文件已由我的應用程序發送。 這個組合的mp3文件(包含“隱藏的”ObjectOutputStream文件)通過“流行的”消息應用程序發送到接收器。 接收器通過他的“流行”消息應用程序下載mp3文件。 現在我的應用程序開始運作了。 它識別mp3文件。 並從mp3文件中提取ObjectOutputStream文件。 然后將其轉換回FileDetails並使用它的元數據檢索原始文件。 我的方法是否正確? 有沒有其他方法來識別我的附加/隱藏文件?
非常感謝提前。
是否可以將類添加到接收器? 然后你可以嘗試這樣的事情:
File file = new File("C://test.dat")
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(file));
-> send over the network
finally {
if (in != null) {
in.close();
}
}
}
接收器可以將字節寫入臨時文件(而不是將它們保存在內存中)
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("C://test.dat");
BufferedOutputStream bos = new BufferedOutputStream(fos);
//something like:
byte[] buffer = new byte[1024];
int len = is.read(buffer);
while (len != -1) {
bos.write(buffer, 0, len);
len = is.read(buffer);
}
如果操作完成,則實例化對象FileDetails fd = new FileDetails(the file you just created,....)
如果必須,您還可以通過網絡發送類定義。
這里我添加了read / writeObject方法:
class FileDetails implements Serializable {
private static final int CHUNK_LEN = 0x10000; // 64k
private String fileName;
private long fileSize;
private File file;
// Note: everything can be deduced from a File object
public FileDetails(File file) {
this.fileName = file.getName();
this.fileSize = file.length();
this.file = file;
}
public String getFileName() {
return fileName;
}
public long getFileSize() {
return fileSize;
}
// explicit coding for reading a FileDetails object
private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException {
fileName = stream.readUTF(); // file name
fileSize = stream.readLong(); // file size
// file data as a series of byte[], length CHUNK_LEN
long toRead = fileSize;
// write file data to a File object, same path name
file = new File( fileName );
OutputStream os = new FileOutputStream( file );
while( toRead > 0 ){
// last byte arrays may be shorter than CHUNK_LEN
int chunkLen = toRead > CHUNK_LEN ? CHUNK_LEN : (int)toRead;
byte[] bytes = new byte[chunkLen];
int nread = stream.read( bytes );
// write data to file
os.write( bytes, 0, nread );
toRead -= nread;
}
os.close();
}
// explicit coding for writing a FileDetails object
private void writeObject(ObjectOutputStream stream)
throws IOException {
stream.writeUTF( fileName ); // file name as an "UTF string"
stream.writeLong( fileSize ); // file size
// file data as a series of byte[], length CHUNK_LEN
long toWrite = fileSize;
// read file data from the File object passed to the constructor
InputStream is = new FileInputStream( file );
while( toWrite > 0 ){
// last byte[] may be shorter than CHUNK_LEN
int chunkLen = toWrite > CHUNK_LEN ? CHUNK_LEN : (int)toWrite;
byte[] bytes = new byte[chunkLen];
int nread = is.read( bytes );
stream.write( bytes );
toWrite -= nread;
}
is.close();
}
private void readObjectNoData()
throws ObjectStreamException {
}
}
我用短文測試了這個:
File file = new File( "test.dat" );
FileDetails fd = new FileDetails( file );
FileOutputStream fos = new FileOutputStream("oos.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject( fd );
oos.close();
// test on a local system: rename test.dat to avoid overwriting
file.renameTo( new File( "test.dat.sav" ) );
FileInputStream fis = new FileInputStream("oos.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
FileDetails fd1 = (FileDetails)ois.readObject();
ois.close();
// now the file test.dat has been rewritten under the same path,
// i.e., test.dat exists again and test.dat.sav == test.dat
我不確定接收器是否滿意根據消息中發送的路徑名寫入某個文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.