Class: NBTUtils::File
- Inherits:
-
Object
- Object
- NBTUtils::File
- Defined in:
- lib/nbt_utils/file.rb
Instance Attribute Summary collapse
-
#compressed ⇒ Object
readonly
Returns the value of attribute compressed.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
Instance Method Summary collapse
-
#initialize(path = nil) ⇒ File
constructor
A new instance of File.
- #read(read_path = path) ⇒ Object
- #write(write_path = path, write_tag = tag, write_compressed = compressed) ⇒ Object
Constructor Details
#initialize(path = nil) ⇒ File
Returns a new instance of File.
7 8 9 |
# File 'lib/nbt_utils/file.rb', line 7 def initialize(path = nil) @path = path end |
Instance Attribute Details
#compressed ⇒ Object (readonly)
Returns the value of attribute compressed.
5 6 7 |
# File 'lib/nbt_utils/file.rb', line 5 def compressed @compressed end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
5 6 7 |
# File 'lib/nbt_utils/file.rb', line 5 def content @content end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/nbt_utils/file.rb', line 5 def path @path end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
5 6 7 |
# File 'lib/nbt_utils/file.rb', line 5 def tag @tag end |
Instance Method Details
#read(read_path = path) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/nbt_utils/file.rb', line 11 def read(read_path = path) # Zlib does not provide a way to test a file for compressed, and I'm # not going to tool around with magic numbers, so I guess we use # exceptions for flow control. begin Zlib::GzipReader.open(read_path) do |f| @content = StringIO.new(f.read) end @compressed = true rescue Zlib::GzipFile::Error @content = StringIO.new(::File.read(read_path)) @compressed = false end last_byte = content.read(1).bytes.first klass = NBTUtils::Tag.tag_type_to_class(last_byte) @tag = klass.new(content, named: true) end |
#write(write_path = path, write_tag = tag, write_compressed = compressed) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/nbt_utils/file.rb', line 31 def write(write_path = path, write_tag = tag, write_compressed = compressed) if write_compressed Zlib::GzipWriter.open(write_path) do |gz| gz.write write_tag.to_nbt_string end else ::File.write(write_path, write_tag.to_nbt_string) end end |