LibAC-dart/lib/nbt/NbtIo.dart

48 lines
1.2 KiB
Dart

import 'package:libac_flutter/nbt/Stream.dart';
import 'package:libac_flutter/nbt/Tag.dart';
import 'package:libac_flutter/nbt/impl/CompoundTag.dart';
class NbtIo {
static ByteLayer _io = ByteLayer();
// Handle various helper functions here!
static Future<void> _read(String file) async {
_io = ByteLayer();
await _io.readFromFile(file);
}
// This function will read the file and check if it is infact gzipped
static Future<CompoundTag> read(String file) async {
await _read(file);
if (_io.readByte() == TagType.Compound.byte) {
_io.resetPosition();
return Tag.readNamedTag(_io) as CompoundTag;
} else {
// Is likely gzip compressed
return readCompressed(file);
}
}
static Future<CompoundTag> readCompressed(String file) async {
_io = ByteLayer();
await _io.readFromFile(file);
await _io.decompress();
_io.resetPosition();
return Tag.readNamedTag(_io) as CompoundTag;
}
static Future<void> write(String file) async {
await _io.writeToFile(file);
}
static Future<void> writeCompressed(String file) async {
await _io.compress();
await _io.writeToFile(file);
}
static ByteLayer getStream() {
return _io;
}
}