public static byte[] ReadAllBytes(string path) { // bufferSize == 1 used to avoid unnecessary buffer in FileStream using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1)) { long fileLength = fs.Length; if (fileLength > int.MaxValue) { throw new IOException(SR.IO_FileTooLong2GB); } else if (fileLength == 0) { // Some file systems (e.g. procfs on Linux) return 0 for length even when there's content. // Thus we need to assume 0 doesn't mean empty. return(ReadAllBytesUnknownLength(fs)); } int index = 0; int count = (int)fileLength; byte[] bytes = new byte[count]; while (count > 0) { int n = fs.Read(bytes, index, count); if (n == 0) { throw Error.GetEndOfFile(); } index += n; count -= n; } return(bytes); } }
private ReadOnlySpan <byte> InternalRead(int numBytes) { Debug.Assert(numBytes >= 2 && numBytes <= 16, "value of 1 should use ReadByte. value > 16 requires to change the minimal _buffer size"); if (_isMemoryStream) { // read directly from MemoryStream buffer Debug.Assert(_stream is MemoryStream); return(((MemoryStream)_stream).InternalReadSpan(numBytes)); } else { ThrowIfDisposed(); int bytesRead = 0; int n = 0; do { n = _stream.Read(_buffer, bytesRead, numBytes - bytesRead); if (n == 0) { throw Error.GetEndOfFile(); } bytesRead += n; } while (bytesRead < numBytes); return(_buffer); } }
private static byte[] InternalReadAllBytes(string path) { // bufferSize == 1 used to avoid unnecessary buffer in FileStream using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1)) { long fileLength = fs.Length; if (fileLength > int.MaxValue) { throw new IOException(SR.IO_FileTooLong2GB); } int index = 0; int count = (int)fileLength; byte[] bytes = new byte[count]; while (count > 0) { int n = fs.Read(bytes, index, count); if (n == 0) { throw Error.GetEndOfFile(); } index += n; count -= n; } return(bytes); } }
private ReadOnlySpan <byte> InternalRead(int numBytes) { Debug.Assert(numBytes >= 2 && numBytes <= 16, "value of 1 should use ReadByte. value > 16 requires to change the minimal _buffer size"); if (_isMemoryStream) { // no need to check if _stream == null as we will never have null _stream when _isMemoryStream = true // read directly from MemoryStream buffer Debug.Assert(_stream is MemoryStream); return(((MemoryStream)_stream).InternalReadSpan(numBytes)); } else { if (_stream == null) { throw Error.GetFileNotOpen(); } int bytesRead = 0; int n = 0; do { n = _stream.Read(_buffer, bytesRead, numBytes - bytesRead); if (n == 0) { throw Error.GetEndOfFile(); } bytesRead += n; } while (bytesRead < numBytes); return(_buffer); } }
public static byte[] ReadAllBytes(String path) { byte[] bytes; using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, FileStream.DefaultBufferSize, FileOptions.None)) { // Do a blocking read int index = 0; long fileLength = fs.Length; if (fileLength > Int32.MaxValue) { throw new IOException(SR.IO_FileTooLong2GB); } int count = (int)fileLength; bytes = new byte[count]; while (count > 0) { int n = fs.Read(bytes, index, count); if (n == 0) { throw Error.GetEndOfFile(); } index += n; count -= n; } } return(bytes); }
public virtual char ReadChar() { int value = Read(); if (value == -1) { throw Error.GetEndOfFile(); } return((char)value); }
public virtual string ReadString() { ThrowIfDisposed(); int currPos = 0; int n; int stringLength; int readLength; int charsRead; // Length of the string in bytes, not chars stringLength = Read7BitEncodedInt(); if (stringLength < 0) { throw new IOException(SR.Format(SR.IO_InvalidStringLen_Len, stringLength)); } if (stringLength == 0) { return(string.Empty); } _charBytes ??= new byte[MaxCharBytesSize]; _charBuffer ??= new char[_maxCharsSize]; StringBuilder?sb = null; do { readLength = ((stringLength - currPos) > MaxCharBytesSize) ? MaxCharBytesSize : (stringLength - currPos); n = _stream.Read(_charBytes, 0, readLength); if (n == 0) { throw Error.GetEndOfFile(); } charsRead = _decoder.GetChars(_charBytes, 0, n, _charBuffer, 0); if (currPos == 0 && n == stringLength) { return(new string(_charBuffer, 0, charsRead)); } if (sb == null) { sb = StringBuilderCache.Acquire(stringLength); // Actual string length in chars may be smaller. } sb.Append(_charBuffer, 0, charsRead); currPos += n; } while (currPos < stringLength); return(StringBuilderCache.GetStringAndRelease(sb)); }
public virtual string ReadString() { ThrowIfDisposed(); int currPos = 0; int n; int stringLength; int readLength; int charsRead; // Length of the string in bytes, not chars stringLength = Read7BitEncodedInt(); if (stringLength < 0) { throw new IOException(SR.Format(SR.IO_InvalidStringLen_Len, stringLength)); } if (stringLength == 0) { return(string.Empty); } _charBytes ??= new byte[MaxCharBytesSize]; _charBuffer ??= new char[_maxCharsSize]; StringBuilder?sb = null; do { readLength = ((stringLength - currPos) > MaxCharBytesSize) ? MaxCharBytesSize : (stringLength - currPos); n = _stream.Read(_charBytes, 0, readLength); if (n == 0) { throw Error.GetEndOfFile(); } charsRead = _decoder.GetChars(_charBytes, 0, n, _charBuffer, 0); if (currPos == 0 && n == stringLength) { return(new string(_charBuffer, 0, charsRead)); } // Since we could be reading from an untrusted data source, limit the initial size of the // StringBuilder instance we're about to get or create. It'll expand automatically as needed. sb ??= StringBuilderCache.Acquire(Math.Min(stringLength, StringBuilderCache.MaxBuilderSize)); // Actual string length in chars may be smaller. sb.Append(_charBuffer, 0, charsRead); currPos += n; } while (currPos < stringLength); return(StringBuilderCache.GetStringAndRelease(sb)); }
[MethodImpl(MethodImplOptions.AggressiveInlining)] // Inlined to avoid some method call overhead with InternalRead. private byte InternalReadByte() { ThrowIfDisposed(); int b = _stream.ReadByte(); if (b == -1) { throw Error.GetEndOfFile(); } return((byte)b); }
// PERF: Takes out Int32 as fast as possible internal int InternalReadInt32() { EnsureNotClosed(); int pos = (_position += 4); // use temp to avoid a race condition if (pos > _length) { _position = _length; throw Error.GetEndOfFile(); } return((int)(_buffer[pos - 4] | _buffer[pos - 3] << 8 | _buffer[pos - 2] << 16 | _buffer[pos - 1] << 24)); }
public virtual byte ReadByte() { // Inlined to avoid some method call overhead with FillBuffer. if (_stream == null) { throw Error.GetFileNotOpen(); } int b = _stream.ReadByte(); if (b == -1) { throw Error.GetEndOfFile(); } return((byte)b); }
private byte InternalReadByte() { // Inlined to avoid some method call overhead with InternalRead. if (_stream == null) { throw Error.GetFileNotOpen(); } int b = _stream.ReadByte(); if (b == -1) { throw Error.GetEndOfFile(); } return((byte)b); }
internal ReadOnlySpan <byte> InternalReadSpan(int count) { EnsureNotClosed(); int origPos = _position; int newPos = origPos + count; if ((uint)newPos > (uint)_length) { _position = _length; throw Error.GetEndOfFile(); } var span = new ReadOnlySpan <byte>(_buffer, origPos, count); _position = newPos; return(span); }
private static async Task <byte[]> InternalReadAllBytesAsync(FileStream fs, int count, CancellationToken cancellationToken) { using (fs) { int index = 0; byte[] bytes = new byte[count]; do { int n = await fs.ReadAsync(bytes, index, count - index, cancellationToken).ConfigureAwait(false); if (n == 0) { throw Error.GetEndOfFile(); } index += n; } while (index < count); return(bytes); } }
protected virtual void FillBuffer(int numBytes) { if (_buffer != null && (numBytes < 0 || numBytes > _buffer.Length)) { throw new ArgumentOutOfRangeException(nameof(numBytes), SR.ArgumentOutOfRange_BinaryReaderFillBuffer); } int bytesRead = 0; int n = 0; if (_stream == null) { throw Error.GetFileNotOpen(); } // Need to find a good threshold for calling ReadByte() repeatedly // vs. calling Read(byte[], int, int) for both buffered & unbuffered // streams. if (numBytes == 1) { n = _stream.ReadByte(); if (n == -1) { throw Error.GetEndOfFile(); } _buffer[0] = (byte)n; return; } do { n = _stream.Read(_buffer, bytesRead, numBytes - bytesRead); if (n == 0) { throw Error.GetEndOfFile(); } bytesRead += n; } while (bytesRead < numBytes); }