internal static void ReadFully(ITriflesByteReader source, byte[] buffer, int offset, int count) { Checker.NotNull(source, "source"); Checker.NotNull(buffer, "buffer"); Checker.SpanInRange(buffer.Length, offset, count, "offset", "count"); ReadFullyNoCheck(source, buffer, offset, count); }
internal static IEnumerable <int> ReadViaArray(ITriflesByteReader source, ulong maxCount, byte[] buffer, bool requireFullCount) { Checker.NotNull(buffer, "buffer"); if (maxCount > 0) { EnsureBufferNonZero(buffer); } return(ReadViaArrayNoCheck(source, maxCount, buffer, requireFullCount)); }
// Repeatedly calls a partial read function until a buffer is full. private static void ReadFullyNoCheck(ITriflesByteReader source, byte[] buffer, int index, int count) { int totalReadLength = ReadAsFullyAsPossibleNoCheck(source, buffer, index, count); if (totalReadLength < count) { // Stream ended before full read. throw new EndOfStreamException(); } }
internal static void ReadViaArray(ITriflesByteReader source, ulong maxCount, Action <byte[], int, int> fillAction, byte[] buffer = null, bool requireFullCount = false) { Checker.NotNull(fillAction, "fillAction"); if (buffer == null) { buffer = new byte[MathTrifles.Min(maxCount, DefaultReadBufferSize)]; } foreach (var countThisPass in ReadViaArray(source, maxCount, buffer, requireFullCount)) { fillAction(buffer, 0, countThisPass); } }
internal static void ReadViaArray(ITriflesByteReader source, Action <byte[], int, int> fillAction, byte[] buffer = null) { Checker.NotNull(fillAction, "fillAction"); if (buffer == null) { buffer = new byte[DefaultReadBufferSize]; } foreach (var countThisPass in ReadViaArray(source, buffer)) { fillAction(buffer, 0, countThisPass); } }
// Read to the end of the stream using the given byte array as a buffer. private static IEnumerable <int> ReadViaArrayNoCheck(ITriflesByteReader source, byte[] buffer) { var bufferSize = buffer.Length; while (true) { var countThisPass = ReadAsFullyAsPossibleNoCheck(source, buffer, 0, bufferSize); if (countThisPass > 0) { yield return(countThisPass); } if (countThisPass < bufferSize) { break; } } }
private static IEnumerable <int> ReadViaArrayNoCheck(ITriflesByteReader source, ulong maxCount, byte[] buffer, bool requireFullCount) { var bufferSize = buffer.Length; while (maxCount > 0) { var targetCountThisPass = MathTrifles.Min(bufferSize, maxCount); var countThisPass = ReadAsFullyAsPossibleNoCheck(source, buffer, 0, targetCountThisPass); if (countThisPass > 0) { yield return(countThisPass); maxCount -= (ulong)countThisPass; } } if (requireFullCount && maxCount > 0) { throw new EndOfStreamException(); } }
// Repeatedly calls a partial read function until a buffer is full or the stream ends. private static int ReadAsFullyAsPossibleNoCheck(ITriflesByteReader source, byte[] buffer, int index, int count) { int totalReadLength = 0; while (totalReadLength < count) { int readLengthThisPass = source.Read(buffer, index + totalReadLength, count - totalReadLength); if (readLengthThisPass > 0) { // At least one byte was read. totalReadLength += readLengthThisPass; } else { // Stream ended. break; } } return(totalReadLength); }
/// <summary> /// Returns this source as an <see cref="ITriflesByteReader"/>. /// </summary> /// <param name="source">A source to convert.</param> /// <returns><paramref name="source"/> as an <see cref="ITriflesByteReader"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> is <c>null</c>.</exception> public static ITriflesByteReader ToTriflesByteReader(this ITriflesByteReader source) { return(TriflesByteReaderImpl.ToTriflesByteReader(source)); }
internal static void ReadViaArray(ITriflesByteReader triflesByteReader, Action <byte[], int, int> fillAction, int maxCount, byte[] buffer, bool requireFullCount) { throw new NotImplementedException(); }
internal static ITriflesByteReader ToTriflesByteReader(ITriflesByteReader source) { Checker.NotNull(source, "source"); return(source); }
internal static void ReadViaArray(ITriflesByteReader source, long maxCount, Action <byte[], int, int> fillAction, byte[] buffer = null, bool requireFullCount = false) { ReadViaArray(source, EnsureMaxCountNotNegative(maxCount), fillAction, buffer, requireFullCount); }
internal static IEnumerable <int> ReadViaArray(ITriflesByteReader source, long maxCount, byte[] buffer, bool requireFullCount) { return(ReadViaArray(source, EnsureMaxCountNotNegative(maxCount), buffer, requireFullCount)); }
internal static IEnumerable <int> ReadViaArray(ITriflesByteReader source, byte[] buffer) { Checker.NotNull(buffer, "buffer"); EnsureBufferNonZero(buffer); return(ReadViaArrayNoCheck(source, buffer)); }