// TODO: use native memcpy public unsafe void CopyTo(BytePtr dest, UInt32 length) { for (uint i = 0; i < length; i++) { dest.ptr[i] = this.ptr[i]; } }
public void WriteLine(BytePtr ptr, UInt32 length) { Write(ptr, length); Write(defaultEncoderNewline); if (flushAtWriteLine) { Flush(); } }
public static void Write(BytePtr buffer, UInt32 length) { UInt32 written = 0; if (!NativeWindows.TryWriteFile(StdOutFileHandle, buffer, length, out written, IntPtr.Zero)) { throw new InvalidOperationException(String.Format("WriteFile failed (lastError={0})", NativeWindows.GetLastError())); } if (written != length) { throw new InvalidOperationException(String.Format("Attempted to write {0} bytes but only wrote {1}", length, written)); } }
public static unsafe void WriteFile(IntPtr file, BytePtr buffer, UInt32 length) { UInt32 written = 0; if (!TryWriteFile(file, buffer, length, out written, IntPtr.Zero)) { throw new InvalidOperationException(String.Format("WriteFile failed (lastError={0})", GetLastError())); } if (written != length) { throw new InvalidOperationException(String.Format("Attempted to write {0} bytes but only wrote {1}", length, written)); } }
public override void Flush() { try { if (sync) { Monitor.Enter(this); } FlushHandler(buffer, next.DiffWithSmallerNumber(buffer)); next = buffer; } finally { if (sync) { Monitor.Exit(this); } } }
public override void Write(BytePtr ptr, UInt32 length) { try { if (sync) { Monitor.Enter(this); } FlushHandler(buffer, next.DiffWithSmallerNumber(buffer)); next = buffer; FlushHandler(ptr, length); } finally { if (sync) { Monitor.Exit(this); } } }
protected BytePtr next; // Assumption: buffer <= next <= bufferLimit public BufferedSink(BytePtr buffer, BytePtr bufferLimit, Boolean flushAtWriteLine, Encoder defaultEncoder, FlushHandler flushHandler, Boolean sync) : base(flushAtWriteLine, defaultEncoder) { if (buffer >= bufferLimit) { throw new ArgumentException(String.Format("buffer {0} must be < bufferLimit {1}", buffer, bufferLimit)); } if (bufferLimit.DiffWithSmallerNumber(buffer) < MinimumBufferSize) { throw new ArgumentException(String.Format("buffer length {0} is too small (minimum is {1})", bufferLimit.DiffWithSmallerNumber(buffer), MinimumBufferSize)); } if (flushHandler == null) { throw new ArgumentNullException("flushHandler"); } this.buffer = buffer; this.bufferLimit = bufferLimit; this.sync = sync; this.FlushHandler = flushHandler; this.next = buffer; }
public static extern bool TryWriteFile( IntPtr hFile, BytePtr lpBuffer, UInt32 nNumberOfBytesToWrite, out UInt32 lpNumberOfBytesWritten, IntPtr lpOverlapped);
private BufferedWindowsConsoleSink(BytePtr buffer, BytePtr bufferLimit, Boolean flushAtWriteLine) : base(buffer, bufferLimit, flushAtWriteLine, Encoder.Utf8, (NativeWindowsConsole.StdOutFileHandle == IntPtr.Zero) ? new FlushHandler(TypedSink.DoNothingFlushHandler) : new FlushHandler(NativeWindowsConsole.Write), true) { }
public abstract void Write(BytePtr ptr, UInt32 length);
public static void DoNothingFlushHandler(BytePtr ptr, UInt32 length) { }
public BufferSlice(BytePtr ptr, UInt32 length) { this.ptr = ptr; this.length = length; }
public unsafe UInt32 DiffWithSmallerNumber(BytePtr smaller) { return((UInt32)(this.ptr - smaller.ptr)); }