public byte WriteBytes(IBytes bytes) { var buffer = bytes.ToArray(); _port?.Write(buffer, 0, buffer.Length); return((byte)(_port?.ReadByte() ?? 0)); }
public static byte[] CopyTo(this IBytes bytes, byte[] array) { if (bytes.ByteCount > 0) { Marshal.Copy(bytes.Ptr, array, 0, Math.Min(bytes.ByteCount, array.Length)); } return(array); }
public static byte[] CopyTo(this IBytes bytes, byte[] array, int offset, int size) { if (bytes.ByteCount > 0) { Marshal.Copy(bytes.Ptr, array, offset, Math.Min(bytes.ByteCount, size)); } return(array); }
/// <summary> /// Writes all data from the buffer to the file. /// </summary> public static void WriteAllBytes(string filePath, IBytes bytes) { using (var hFile = OpenForWriting(filePath)) { hFile.Write(bytes); hFile.Flush(); } }
public static IBytes CopyTo(this byte[] array, IBytes bytes) { if (bytes.ByteCount > 0) { Marshal.Copy(array, 0, bytes.Ptr, Math.Min(bytes.ByteCount, array.Length)); } return(bytes); }
/// <summary> /// A cell which exists in memory. /// Note: This memory cell lives only as long as this object lives. /// If you create two cells with the same path using this ctor, /// they will not have the same content. /// </summary> public RamCell(string path, IBytes content) : this( new Live <string>(() => path), new Solid <IMnemonic>(() => { var mem = new RamMnemonic(); mem.Contents().UpdateBytes(path, content.AsBytes()); return(mem); }) ) { }
/// <summary> /// Returns true if the two buffers are the same. /// </summary> public static bool SequenceEqual(this IBytes self, IBytes other) { // TODO: consider casting to a Vector<T>() https://docs.microsoft.com/en-us/dotnet/api/system.numerics.vector-1.-ctor?view=netcore-2.2#System_Numerics_Vector_1__ctor_System_Span__0__ // Look at https://stackoverflow.com/questions/43289/comparing-two-byte-arrays-in-net/1445405#1445405 // Compare to PInvoke if (self.ByteCount != other.ByteCount) { return(false); } return(self.ToSpan <byte>().SequenceEqual(other.ToSpan <byte>())); }
/// <summary> /// A Response containing the given status, reason and a body from the given bytes. /// </summary> public Of(int status, string reason, IBytes body, params IMapInput[] extraParts) : base(() => new Response.Of( new Status(status), new Reason(reason), new Body(body), new Parts.Joined(extraParts) ), live: false ) { }
/// <summary> /// Adds a body to a request. /// </summary> public Body(IBytes content) : this( new MapInputOf( new KvpOf(KEY, () => new TextOf( new BytesBase64(content) ).AsString() ) ) ) { }
public AttributeBytes(IBytes bytes, AttributeDescriptor descriptor) { Bytes = bytes; Descriptor = descriptor; // Should never happen, but just in case if (Bytes.ByteCount % Descriptor.ItemSize != 0) { throw new Exception("Number of items does not divide by item size properly"); } Count = Bytes.ByteCount / Descriptor.ItemSize; }
public static IBytes CopyTo(this IBytes self, IBytes other) { Util.MemoryCopy(self.Ptr, other.Ptr, Math.Min(self.ByteCount, other.ByteCount)); return(other); }
public static bool Overlapping(this IBytes self, IBytes other) { return(self.Contains(other.Ptr) || self.Contains(other.End()) || other.Contains(self.Ptr) || self.Contains(self.End())); }
public static IntPtr End(this IBytes self) { return(self.Ptr + self.ByteCount); }
public static bool Contains(this IBytes self, IntPtr ptr) { var dist = self.Ptr.Distance(ptr); return(dist >= 0 && dist < self.ByteCount); }
/// <summary> /// Provides access to raw memory as an unmanaged memory stream. /// https://docs.microsoft.com/en-us/dotnet/api/system.io.unmanagedmemorystream?view=netframework-4.7.2 /// </summary> public static UnmanagedMemoryStream ToMemoryStream(this IBytes self) { return(new UnmanagedMemoryStream(self.Ptr.ToBytePtr(), self.ByteCount)); }
/// <summary> /// Initializes a new instance of <see cref="BytesAsOutput"/>. /// </summary> /// <param name="source">The source bytes.</param> public BytesAsOutput(IBytes source) : this(source.Bytes()) { }
public BytesText(IBytes source, Encoding encoding) { _source = source; _encoding = encoding; }
/// <summary> /// Initializes a new instance of <see cref="AsyncOutput"/>. /// </summary> /// <param name="from">Bytes to read from.</param> /// <param name="encoding">The input encoding.</param> /// <param name="to">AsyncOutput to write to.</param> public AsyncOutput(IBytes from, Encoding encoding, IOutput to) : this(new BytesText(from, encoding), to) { }
/// <summary> /// Read data from the file to the buffer. Assumes enough space in the buffer. /// </summary> public static SafeFileHandle Read(this SafeFileHandle hFile, IBytes bytes) { return(Read(hFile, bytes.Ptr, bytes.ByteCount)); }
/// <summary> /// Writes all data from the buffer to the file. /// </summary> public static SafeFileHandle Write(this SafeFileHandle hFile, IBytes bytes) { return(Write(hFile, bytes.Ptr, bytes.ByteCount)); }
/// <summary> /// Writes raw bytes to the stream by creating a memory stream around it. /// </summary> public static void Write(this BinaryWriter self, IBytes bytes) { self.Write(bytes.ToBytes()); }
/// <summary> /// Initializes a new instance of <see cref="AsyncOutput"/>. /// </summary> /// <param name="from">Bytes to read from.</param> /// <param name="to">AsyncOutput to write to.</param> public AsyncOutput(IBytes from, IOutput to) : this(new ByteInput(from), to) { }
public static IAttribute ToAttribute(this IBytes bytes, AttributeDescriptor desc) => new AttributeBytes(bytes, desc);
// TODO: all of these copies make me die a little bit inside public static G3D Create(IBytes bytes) => Create(bytes.ToBytes());
/// <summary> /// A 200/OK Response containing a body from the given bytes. /// </summary> public Of(IBytes body, params IMapInput[] extraParts) : this(200, "OK", body, extraParts) { }
public static T[] ToStructs <T>(this IBytes bytes) where T : struct { return(bytes.ToSpan <T>().ToArray()); }
/// <summary> /// Initializes a new instance of <see cref="ByteInput"/>. /// </summary> /// <param name="source">Source <see cref="IBytes"/>.</param> public ByteInput(IBytes source) { _source = source; }
public static byte[] ToBytes(this IBytes bytes) { return(bytes.CopyTo(new byte[bytes.ByteCount])); }
public static unsafe Span <T> ToSpan <T>(this IBytes span) where T : struct { return(new Span <T>((void *)span.Ptr, span.ByteCount / typeof(T).SizeOf())); }
public UnsafeFloatArray(IBytes bytes) : this(bytes.Ptr.ToPointer(), bytes.ByteCount) { }