Here are the examples of the csharp api System.IO.Stream.ReadBytes(int) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
25 Examples
19
Source : extensions.cs
with Apache License 2.0
from AntonioDePau
with Apache License 2.0
from AntonioDePau
public static byte[] ReadBytes(this Stream stream) =>
stream.ReadBytes((int)(stream.Length - stream.Position));
19
Source : extensions.cs
with Apache License 2.0
from AntonioDePau
with Apache License 2.0
from AntonioDePau
public static string ReadString(this Stream stream, int maxLength, Encoding encoding)
{
var data = stream.ReadBytes(maxLength);
var terminatorIndex = Array.FindIndex(data, x => x == 0);
return encoding.GetString(data, 0, terminatorIndex < 0 ? maxLength : terminatorIndex);
}
19
Source : Extensions.cs
with GNU General Public License v3.0
from askeladdk
with GNU General Public License v3.0
from askeladdk
public static T ReadStruct<T>(this Stream s) where T : struct
{
return s.ReadBytes(Helpers.SizeOf<T>()).TypeCastByteBuffer<T>();
}
19
Source : StreamExtensions.cs
with MIT License
from Donkie
with MIT License
from Donkie
public static void Align(this Stream s, byte wordSize)
{
var mod = s.Position % wordSize;
if (mod == 0) return;
var bytesToRead = (int)(4 - mod);
s.ReadBytes(bytesToRead);
}
19
Source : StreamExt.cs
with GNU General Public License v3.0
from Fe7n
with GNU General Public License v3.0
from Fe7n
public static byte[] ReadBytesWithLength(this Stream s)
{
int count = s.ReadInt32();
return s.ReadBytes(count);
}
19
Source : StreamExt.cs
with GNU General Public License v3.0
from Fe7n
with GNU General Public License v3.0
from Fe7n
public static string ReadString(this Stream s)
{
int num = s.ReadEncodedInt();
if (num > 0)
{
return Encoding.UTF8.GetString(s.ReadBytes(num));
}
return string.Empty;
}
19
Source : StreamExtensions.cs
with Apache License 2.0
from jlucansky
with Apache License 2.0
from jlucansky
public static SslClientHello ReadSslClientHello(this Stream stream)
{
int recordType = stream.ReadByte();
if (recordType == -1)
{
return null;
}
if ((recordType & 0x80) == 0x80) //SSL 2.0
{
int recordLength = ((recordType & 0x7f) << 8) + stream.ReadByte();
if (recordLength < 9)
{
// Message body too short.
return null;
}
if (stream.ReadByte() != 0x01)
{
// should be ClientHello
return null;
}
int majorVersion = stream.ReadByte();
int minorVersion = stream.ReadByte();
int ciphersCount = stream.ReadInt16BE() / 3;
int sessionIdLength = stream.ReadInt16BE();
int randomLength = stream.ReadInt16BE();
int[] ciphers = new int[ciphersCount];
for (int i = 0; i < ciphers.Length; i++)
{
ciphers[i] = (stream.ReadByte() << 16) + (stream.ReadByte() << 8) + stream.ReadByte();
}
byte[] sessionId = stream.ReadBytes(sessionIdLength);
byte[] random = stream.ReadBytes(randomLength);
var clientHelloInfo = new SslClientHello
{
HandshakeVersion = 2,
MajorVersion = majorVersion,
MinorVersion = minorVersion,
Random = random,
SessionId = sessionId,
Ciphers = ciphers,
};
return clientHelloInfo;
}
else if (recordType == 0x16) //SSL 3.0 or TLS 1.0, 1.1 and 1.2
{
int majorVersion = stream.ReadByte();
int minorVersion = stream.ReadByte();
int recordLength = stream.ReadInt16BE();
if (stream.ReadByte() != 0x01)
{
// should be ClientHello
return null;
}
var length = stream.ReadInt24BE();
majorVersion = stream.ReadByte();
minorVersion = stream.ReadByte();
byte[] random = stream.ReadBytes(32);
if (-1 == (length = stream.ReadByte())) return null;
byte[] sessionId = stream.ReadBytes(length);
length = stream.ReadInt16BE();
byte[] ciphersData = stream.ReadBytes(length);
int[] ciphers = new int[ciphersData.Length / 2];
for (int i = 0; i < ciphers.Length; i++)
ciphers[i] = (ciphersData[2 * i] << 8) + ciphersData[2 * i + 1];
length = stream.ReadByte();
if (length < 1)
return null;
byte[] compressionData = stream.ReadBytes(length);
Dictionary<string, SslExtension> extensions;
extensions = ReadSslExtensions(stream, majorVersion, minorVersion);
var clientHelloInfo = new SslClientHello
{
HandshakeVersion = 3,
MajorVersion = majorVersion,
MinorVersion = minorVersion,
Random = random,
SessionId = sessionId,
Ciphers = ciphers,
CompressionData = compressionData,
Extensions = extensions,
};
return clientHelloInfo;
}
return null;
}
19
Source : StreamExtensions.cs
with Apache License 2.0
from jlucansky
with Apache License 2.0
from jlucansky
public static SslServerHello ReadSslServerHello(this Stream stream)
{
//detects the HTTPS ClientHello message as it is described in the following url:
//https://stackoverflow.com/questions/3897883/how-to-detect-an-incoming-ssl-https-handshake-ssl-wire-format
int recordType = stream.ReadByte();
if (recordType == -1)
{
return null;
}
if ((recordType & 0x80) == 0x80) //SSL 2.0
{
// not tested. SSL2 is deprecated
int recordLength = ((recordType & 0x7f) << 8) + stream.ReadByte();
if (recordLength < 38)
{
// Message body too short.
return null;
}
if (stream.ReadByte() != 0x04)
{
// should be ServerHello
return null;
}
int majorVersion = stream.ReadByte();
int minorVersion = stream.ReadByte();
byte[] random = stream.ReadBytes(32);
byte[] sessionId = stream.ReadBytes(1);
int cipherSuite = stream.ReadInt16BE();
var serverHelloInfo = new SslServerHello
{
HandshakeVersion = 2,
MajorVersion = majorVersion,
MinorVersion = minorVersion,
Random = random,
SessionId = sessionId,
CipherSuite = cipherSuite,
};
return serverHelloInfo;
}
else if (recordType == 0x16) //SSL 3.0 or TLS 1.0, 1.1 and 1.2
{
int majorVersion = stream.ReadByte();
int minorVersion = stream.ReadByte();
int recordLength = stream.ReadInt16BE();
if (stream.ReadByte() != 0x02)
{
// should be ServerHello
return null;
}
var length = stream.ReadInt24BE();
majorVersion = stream.ReadByte();
minorVersion = stream.ReadByte();
byte[] random = stream.ReadBytes(32);
length = stream.ReadByte();
byte[] sessionId = stream.ReadBytes(length);
int cipherSuite = stream.ReadInt16BE();
int compressionMethod = stream.ReadByte();
if (compressionMethod == -1) return null;
Dictionary<string, SslExtension> extensions = null;
extensions = ReadSslExtensions(stream, majorVersion, minorVersion);
if (extensions.TryGetValue("supported_versions", out var supported_versions) && supported_versions.RawData.Length == 2)
{
// override for TLS 1.3
majorVersion = supported_versions.RawData[0];
minorVersion = supported_versions.RawData[1];
}
var serverHelloInfo = new SslServerHello
{
HandshakeVersion = 3,
MajorVersion = majorVersion,
MinorVersion = minorVersion,
Random = random,
SessionId = sessionId,
CipherSuite = cipherSuite,
CompressionMethod = (byte)compressionMethod,
Extensions = extensions,
};
return serverHelloInfo;
}
else if (recordType == 0x15) // alert
{
int majorVersion = stream.ReadByte();
int minorVersion = stream.ReadByte();
int recordLength = stream.ReadInt16BE();
if (recordLength == 2)
{
int level = stream.ReadByte(); // warning(1), fatal(2)
int description = stream.ReadByte();
if (description != -1)
{
throw new SslAlertException()
{
MajorVersion = majorVersion,
MinorVersion = minorVersion,
Level = level,
Description = (SslAlertDescription)description,
};
}
}
}
return null;
}
19
Source : StreamExtensions.cs
with Apache License 2.0
from jlucansky
with Apache License 2.0
from jlucansky
private static Dictionary<string, SslExtension> ReadSslExtensions(this Stream stream, int majorVersion, int minorVersion)
{
Dictionary<string, SslExtension> extensions = null;
if (majorVersion > 3 || majorVersion == 3 && minorVersion >= 1)
{
int extensionsLength = stream.ReadInt16BE();
extensions = new Dictionary<string, SslExtension>();
int idx = 0;
while (extensionsLength > 3)
{
int id = stream.ReadInt16BE();
int length = stream.ReadInt16BE();
byte[] data = stream.ReadBytes(length);
var extension = new SslExtension(id, data, idx++);
extensions[extension.Name] = extension;
extensionsLength -= 4 + length;
}
}
return extensions;
}
19
Source : StreamExtensions.cs
with The Unlicense
from marcussacana
with The Unlicense
from marcussacana
public static long ReadMultibyteBE(this Stream s, byte bytes)
{
if (bytes > 8) return 0;
long ret = 0;
var b = s.ReadBytes(bytes);
for (uint i = 0; i < b.Length; i++)
{
ret <<= 8;
ret |= b[i];
}
return ret;
}
19
Source : StreamExtensions.cs
with GNU Lesser General Public License v3.0
from maxton
with GNU Lesser General Public License v3.0
from maxton
public static string ReadFixedLengthNullTerminatedString(this Stream s, int length)
{
var stringBytes = s.ReadBytes(length);
var endIdx = 0;
for (int i = 0; i < stringBytes.Length; i++)
{
endIdx = i;
if (stringBytes[i] == 0)
break;
}
return Encoding.UTF8.GetString(stringBytes, 0, endIdx);
}
19
Source : StreamExtensions.cs
with GNU Lesser General Public License v3.0
from maxton
with GNU Lesser General Public License v3.0
from maxton
public static string ReadFixedLengthString(this Stream s, int length)
{
var stringBytes = s.ReadBytes(length);
return Encoding.UTF8.GetString(stringBytes, 0, stringBytes.Length);
}
19
Source : StreamExtensions.cs
with GNU General Public License v3.0
from ME3Tweaks
with GNU General Public License v3.0
from ME3Tweaks
public static double ReadDouble(this Stream stream, ByteOrder byteOrder = ByteOrder.LittleEndian)
{
var data = stream.ReadBytes(8);
if (ShouldSwap(byteOrder))
{
return BitConverter.Int64BitsToDouble(BitConverter.ToInt64(data, 0).Swap());
}
return BitConverter.ToDouble(data, 0);
}
19
Source : StreamExtensions.cs
with GNU General Public License v3.0
from ME3Tweaks
with GNU General Public License v3.0
from ME3Tweaks
public static Guid ReadGuid(this Stream stream, ByteOrder byteOrder = ByteOrder.LittleEndian)
{
var a = stream.ReadInt32(byteOrder);
var b = stream.ReadInt16(byteOrder);
var c = stream.ReadInt16(byteOrder);
var d = stream.ReadBytes(8);
return new Guid(a, b, c, d);
}
19
Source : StreamExtensions.cs
with GNU General Public License v3.0
from ME3Tweaks
with GNU General Public License v3.0
from ME3Tweaks
public static short ReadInt16(this Stream stream, ByteOrder byteOrder = ByteOrder.LittleEndian)
{
var data = stream.ReadBytes(2);
var value = BitConverter.ToInt16(data, 0);
if (ShouldSwap(byteOrder))
{
value = value.Swap();
}
return value;
}
19
Source : StreamExtensions.cs
with GNU General Public License v3.0
from ME3Tweaks
with GNU General Public License v3.0
from ME3Tweaks
public static int ReadInt32(this Stream stream, ByteOrder byteOrder = ByteOrder.LittleEndian)
{
var data = stream.ReadBytes(4);
var value = BitConverter.ToInt32(data, 0);
if (ShouldSwap(byteOrder))
{
value = value.Swap();
}
return value;
}
19
Source : StreamExtensions.cs
with GNU General Public License v3.0
from ME3Tweaks
with GNU General Public License v3.0
from ME3Tweaks
public static long ReadInt64(this Stream stream, ByteOrder byteOrder = ByteOrder.LittleEndian)
{
var data = stream.ReadBytes(8);
var value = BitConverter.ToInt64(data, 0);
if (ShouldSwap(byteOrder))
{
value = value.Swap();
}
return value;
}
19
Source : StreamExtensions.cs
with GNU General Public License v3.0
from ME3Tweaks
with GNU General Public License v3.0
from ME3Tweaks
public static float ReadSingle(this Stream stream, ByteOrder byteOrder = ByteOrder.LittleEndian)
{
var data = stream.ReadBytes(4);
if (ShouldSwap(byteOrder))
{
return BitConverter.ToSingle(BitConverter.GetBytes(BitConverter.ToInt32(data, 0).Swap()), 0);
}
return BitConverter.ToSingle(data, 0);
}
19
Source : StreamExtensions.cs
with GNU General Public License v3.0
from ME3Tweaks
with GNU General Public License v3.0
from ME3Tweaks
public static ushort ReadUInt16(this Stream stream, ByteOrder byteOrder = ByteOrder.LittleEndian)
{
var data = stream.ReadBytes(2);
var value = BitConverter.ToUInt16(data, 0);
if (ShouldSwap(byteOrder))
{
value = value.Swap();
}
return value;
}
19
Source : StreamExtensions.cs
with GNU General Public License v3.0
from ME3Tweaks
with GNU General Public License v3.0
from ME3Tweaks
public static uint ReadUInt32(this Stream stream, ByteOrder byteOrder = ByteOrder.LittleEndian)
{
var data = stream.ReadBytes(4);
var value = BitConverter.ToUInt32(data, 0);
if (ShouldSwap(byteOrder))
{
value = value.Swap();
}
return value;
}
19
Source : StreamExtensions.cs
with GNU General Public License v3.0
from ME3Tweaks
with GNU General Public License v3.0
from ME3Tweaks
public static ulong ReadUInt64(this Stream stream, ByteOrder byteOrder = ByteOrder.LittleEndian)
{
var data = stream.ReadBytes(8);
var value = BitConverter.ToUInt64(data, 0);
if (ShouldSwap(byteOrder))
{
value = value.Swap();
}
return value;
}
19
Source : StreamExtensions.cs
with GNU General Public License v3.0
from ME3Tweaks
with GNU General Public License v3.0
from ME3Tweaks
public static byte[] ReadBytes(this Stream stream, uint length)
{
return stream.ReadBytes((int) length);
}
19
Source : StreamExtensions.cs
with Apache License 2.0
from Noxalus
with Apache License 2.0
from Noxalus
public static byte[] ReadBytes(this Stream stream) => stream.ReadBytes((int)(stream.Length - stream.Position));
19
Source : StreamHelpers.cs
with GNU General Public License v3.0
from Xeeynamo
with GNU General Public License v3.0
from Xeeynamo
public static byte[] ReadBytes(this Stream stream) =>
stream.ReadBytes((int)(stream.Length - stream.Position));
19
Source : StreamExtensions.cs
with Apache License 2.0
from Xeeynamo
with Apache License 2.0
from Xeeynamo
public static string ReadString(this Stream stream, int maxLength, Encoding encoding)
{
var data = stream.ReadBytes(maxLength);
var terminatorIndex = Array.FindIndex(data, x => x == 0);
return encoding.GetString(data, 0, terminatorIndex < 0 ? maxLength : terminatorIndex);
}