EndOfStreamException:无法读取流的结尾(Unity 3d)
EndOfStreamException: Failed to Read past end of stream (Unity 3d)
我正在尝试使用 GZipStream 解压缩内存中的文件,将解压缩的数据复制到 MemoryStream,然后使用 BinaryReader(来自 Unity 3d)读取 MemoryStream。但是,当我尝试 运行 时出现这些错误:
EndOfStreamException:无法读取流的末尾。
System.IO.BinaryReader.FillBuffer (Int32 numBytes)(位于 /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/BinaryReader.cs:119)
System.IO.BinaryReader.ReadInt32 () (位于 /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/BinaryReader.cs:432)
LoadNii.LoadNifti(System.String fullPath,布尔值 loadFromResources)(位于 Assets/scripts/LoadNii.cs:240)
opengl_main.PrepareNewAnatomy(System.String fullPath,布尔值 loadFromResources)(位于 Assets/scripts/opengl_main.cs:193)
opengl_main.LoadFileUsingPath () (位于 Assets/scripts/opengl_main.cs:656)
UnityEngine.Component:发送消息(字符串,对象)
SimpleFileBrowser.Scripts.GracesGames.FileBrowser:SendCallbackMessage(String)(位于 Assets/Resources/SimpleFileBrowser/Scripts/GracesGames/FileBrowser.cs:274)
SimpleFileBrowser.Scripts.GracesGames.FileBrowser:SelectFile()(位于 Assets/Resources/SimpleFileBrowser/Scripts/GracesGames/FileBrowser.cs:267)
UnityEngine.EventSystems.EventSystem:更新()
有人知道问题出在哪里吗?还有另一种方法可以将文件解压缩到内存中,并将其传输到 binaryReader 吗?谢谢
代码:
Stream stream = null;
if (loadFromResources == true)
{
TextAsset textAsset = Resources.Load(fullPath) as TextAsset;
Debug.Log(textAsset);
stream = new MemoryStream(textAsset.bytes);
}
else
{
FileInfo fi1 = new FileInfo(fullPath);
if (fi1.Extension.Equals(".gz"))
{
stream = new MemoryStream();
byte[] buffer = new byte[4096];
using (Stream inGzipStream = new GZipStream(File.Open(fullPath,FileMode.Open), CompressionMode.Decompress))
{
int bytesRead;
while ((bytesRead = inGzipStream.Read(buffer, 0, buffer.Length)) > 0)
{
stream.Write(buffer, 0, bytesRead);
}
}
}
else
stream = File.Open(fullPath, FileMode.Open);
}
using (BinaryReader reader = new BinaryReader(stream))
{
//header headerKey substruct:
headerKey.sizeof_hdr = reader.ReadInt32(); //ERROR
}
每次写入流时,它的 Position
都会增加。
写入后设置stream.Position = 0
即可,之后再从第一个字节开始读取。
这只是一个反序列化并取回结果的函数。谢谢@C.Evenhuis.
/// <summary>
///Get data from a binary file.
/// </summary>
/// <param name="filename"></param>
/// <param name="path"></param>
/// <returns>Null if no object is found </returns>
public static T GetData<T>(string filename, string path)
{
//Path is empty.
if (string.IsNullOrEmpty(path)) throw new NullReferenceException("Path can not be null or empty");
//file is empty.
if (string.IsNullOrEmpty(filename)) throw new NullReferenceException("File name can not be null or empty.");
//Storage path
var storagePath = Path.Combine(path, filename);
//check file not exist
if (!File.Exists(storagePath))
throw new NullReferenceException("No file with the name " + filename + "at location" + storagePath);
else
try
{
//create new binary formatter .
BinaryFormatter binaryFormatter = new BinaryFormatter();
//Read the file.
using (FileStream fileStream = File.Open(storagePath, FileMode.Open))
{
//result .
var result = binaryFormatter.Deserialize(fileStream);
//Set file stream to zero .
//to avoid Exception: FailedSystem.IO.EndOfStreamException:
//Failed to read past end of stream.
fileStream.Position = 0;
//Check if casting is possible and raise error accordin to that .
return (T)Convert.ChangeType(binaryFormatter.Deserialize(fileStream), typeof(T));
}
}
catch (Exception ex)
{
throw new Exception("Failed" + ex);
}
}
我正在尝试使用 GZipStream 解压缩内存中的文件,将解压缩的数据复制到 MemoryStream,然后使用 BinaryReader(来自 Unity 3d)读取 MemoryStream。但是,当我尝试 运行 时出现这些错误:
EndOfStreamException:无法读取流的末尾。 System.IO.BinaryReader.FillBuffer (Int32 numBytes)(位于 /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/BinaryReader.cs:119) System.IO.BinaryReader.ReadInt32 () (位于 /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/BinaryReader.cs:432) LoadNii.LoadNifti(System.String fullPath,布尔值 loadFromResources)(位于 Assets/scripts/LoadNii.cs:240) opengl_main.PrepareNewAnatomy(System.String fullPath,布尔值 loadFromResources)(位于 Assets/scripts/opengl_main.cs:193) opengl_main.LoadFileUsingPath () (位于 Assets/scripts/opengl_main.cs:656) UnityEngine.Component:发送消息(字符串,对象) SimpleFileBrowser.Scripts.GracesGames.FileBrowser:SendCallbackMessage(String)(位于 Assets/Resources/SimpleFileBrowser/Scripts/GracesGames/FileBrowser.cs:274) SimpleFileBrowser.Scripts.GracesGames.FileBrowser:SelectFile()(位于 Assets/Resources/SimpleFileBrowser/Scripts/GracesGames/FileBrowser.cs:267) UnityEngine.EventSystems.EventSystem:更新()
有人知道问题出在哪里吗?还有另一种方法可以将文件解压缩到内存中,并将其传输到 binaryReader 吗?谢谢
代码:
Stream stream = null;
if (loadFromResources == true)
{
TextAsset textAsset = Resources.Load(fullPath) as TextAsset;
Debug.Log(textAsset);
stream = new MemoryStream(textAsset.bytes);
}
else
{
FileInfo fi1 = new FileInfo(fullPath);
if (fi1.Extension.Equals(".gz"))
{
stream = new MemoryStream();
byte[] buffer = new byte[4096];
using (Stream inGzipStream = new GZipStream(File.Open(fullPath,FileMode.Open), CompressionMode.Decompress))
{
int bytesRead;
while ((bytesRead = inGzipStream.Read(buffer, 0, buffer.Length)) > 0)
{
stream.Write(buffer, 0, bytesRead);
}
}
}
else
stream = File.Open(fullPath, FileMode.Open);
}
using (BinaryReader reader = new BinaryReader(stream))
{
//header headerKey substruct:
headerKey.sizeof_hdr = reader.ReadInt32(); //ERROR
}
每次写入流时,它的 Position
都会增加。
写入后设置stream.Position = 0
即可,之后再从第一个字节开始读取。
这只是一个反序列化并取回结果的函数。谢谢@C.Evenhuis.
/// <summary>
///Get data from a binary file.
/// </summary>
/// <param name="filename"></param>
/// <param name="path"></param>
/// <returns>Null if no object is found </returns>
public static T GetData<T>(string filename, string path)
{
//Path is empty.
if (string.IsNullOrEmpty(path)) throw new NullReferenceException("Path can not be null or empty");
//file is empty.
if (string.IsNullOrEmpty(filename)) throw new NullReferenceException("File name can not be null or empty.");
//Storage path
var storagePath = Path.Combine(path, filename);
//check file not exist
if (!File.Exists(storagePath))
throw new NullReferenceException("No file with the name " + filename + "at location" + storagePath);
else
try
{
//create new binary formatter .
BinaryFormatter binaryFormatter = new BinaryFormatter();
//Read the file.
using (FileStream fileStream = File.Open(storagePath, FileMode.Open))
{
//result .
var result = binaryFormatter.Deserialize(fileStream);
//Set file stream to zero .
//to avoid Exception: FailedSystem.IO.EndOfStreamException:
//Failed to read past end of stream.
fileStream.Position = 0;
//Check if casting is possible and raise error accordin to that .
return (T)Convert.ChangeType(binaryFormatter.Deserialize(fileStream), typeof(T));
}
}
catch (Exception ex)
{
throw new Exception("Failed" + ex);
}
}