2011-04-07 07:03:54 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using Ionic.Zlib;
|
2011-06-30 04:41:29 +00:00
|
|
|
|
using Substrate.Nbt;
|
2011-04-07 07:03:54 +00:00
|
|
|
|
|
2011-06-29 02:58:34 +00:00
|
|
|
|
namespace Substrate.Core
|
2011-04-07 07:03:54 +00:00
|
|
|
|
{
|
|
|
|
|
public class NBTFile
|
|
|
|
|
{
|
2011-07-24 18:47:52 +00:00
|
|
|
|
private string _filename;
|
2011-04-07 07:03:54 +00:00
|
|
|
|
|
|
|
|
|
public NBTFile (string path)
|
|
|
|
|
{
|
|
|
|
|
_filename = path;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-24 18:47:52 +00:00
|
|
|
|
public string FileName
|
|
|
|
|
{
|
|
|
|
|
get { return _filename; }
|
|
|
|
|
protected set { _filename = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-07 07:03:54 +00:00
|
|
|
|
public bool Exists ()
|
|
|
|
|
{
|
|
|
|
|
return File.Exists(_filename);
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-02 03:51:48 +00:00
|
|
|
|
public void Delete ()
|
2011-04-07 07:03:54 +00:00
|
|
|
|
{
|
|
|
|
|
File.Delete(_filename);
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-08 23:16:04 +00:00
|
|
|
|
public virtual Stream GetDataInputStream ()
|
2011-04-07 07:03:54 +00:00
|
|
|
|
{
|
2011-07-02 03:51:48 +00:00
|
|
|
|
try {
|
|
|
|
|
FileStream fstr = new FileStream(_filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
2011-04-07 07:03:54 +00:00
|
|
|
|
|
2011-07-02 03:51:48 +00:00
|
|
|
|
long length = fstr.Seek(0, SeekOrigin.End);
|
|
|
|
|
fstr.Seek(0, SeekOrigin.Begin);
|
2011-04-07 07:03:54 +00:00
|
|
|
|
|
2011-07-02 03:51:48 +00:00
|
|
|
|
byte[] data = new byte[length];
|
|
|
|
|
fstr.Read(data, 0, data.Length);
|
2011-04-07 07:03:54 +00:00
|
|
|
|
|
2011-07-02 03:51:48 +00:00
|
|
|
|
fstr.Close();
|
2011-04-07 07:03:54 +00:00
|
|
|
|
|
2011-07-02 03:51:48 +00:00
|
|
|
|
return new GZipStream(new MemoryStream(data), CompressionMode.Decompress);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
throw new NbtIOException("Failed to open compressed NBT data stream for input.", ex);
|
|
|
|
|
}
|
2011-04-07 07:03:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-08 23:16:04 +00:00
|
|
|
|
public virtual Stream GetDataOutputStream ()
|
2011-04-07 07:03:54 +00:00
|
|
|
|
{
|
2011-07-02 03:51:48 +00:00
|
|
|
|
try {
|
|
|
|
|
return new GZipStream(new NBTBuffer(this), CompressionMode.Compress);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
throw new NbtIOException("Failed to initialize compressed NBT data stream for output.", ex);
|
|
|
|
|
}
|
2011-04-07 07:03:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class NBTBuffer : MemoryStream
|
|
|
|
|
{
|
|
|
|
|
private NBTFile file;
|
|
|
|
|
|
|
|
|
|
public NBTBuffer (NBTFile c)
|
|
|
|
|
: base(8096)
|
|
|
|
|
{
|
|
|
|
|
this.file = c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Close ()
|
|
|
|
|
{
|
2011-07-02 03:51:48 +00:00
|
|
|
|
FileStream fstr;
|
|
|
|
|
try {
|
|
|
|
|
fstr = new FileStream(file._filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
throw new NbtIOException("Failed to open NBT data stream for output.", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
fstr.Write(this.GetBuffer(), 0, (int)this.Length);
|
|
|
|
|
fstr.Close();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
throw new NbtIOException("Failed to write out NBT data stream.", ex);
|
|
|
|
|
}
|
2011-04-07 07:03:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|