// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System;
using System.IO;
namespace FlaxEngine.Json
{
///
/// Implements a that reads from unmanaged UTF8 string buffer (provided as raw pointer and length).
///
internal class UnmanagedStringReader : TextReader
{
private unsafe byte* _ptr;
private int _pos;
private int _length;
///
/// Initializes a new instance of the class.
///
public unsafe UnmanagedStringReader()
{
_ptr = null;
_pos = 0;
_length = 0;
}
///
/// Initializes a new instance of the class.
///
/// The text buffer pointer (raw, fixed memory).
/// The text length (characters count).
public unsafe UnmanagedStringReader(IntPtr buffer, int length)
{
if (buffer == IntPtr.Zero)
throw new ArgumentNullException(nameof(buffer));
_ptr = (byte*)buffer.ToPointer();
_pos = 0;
_length = length;
}
///
/// Initializes the reader with the specified text buffer.
///
/// The text buffer pointer (raw, fixed memory).
/// The text length (characters count).
public unsafe void Initialize(void* buffer, int length)
{
if (buffer == null)
throw new ArgumentNullException(nameof(buffer));
_ptr = (byte*)buffer;
_pos = 0;
_length = length;
}
///
/// Initializes the reader with the specified text buffer.
///
/// The text buffer pointer (raw, fixed memory).
/// The text length (characters count).
public unsafe void Initialize(IntPtr buffer, int length)
{
if (buffer == IntPtr.Zero)
throw new ArgumentNullException(nameof(buffer));
_ptr = (byte*)buffer.ToPointer();
_pos = 0;
_length = length;
}
///
protected override unsafe void Dispose(bool disposing)
{
_ptr = null;
_pos = 0;
_length = 0;
base.Dispose(disposing);
}
///
public override unsafe int Peek()
{
if (_pos == _length)
return -1;
return _ptr[_pos];
}
///
public override unsafe int Read()
{
if (_pos == _length)
return -1;
return _ptr[_pos++];
}
}
}