Esempio n. 1
0
 public static Bytes a2b_uu(CodeContext /*!*/ context, [NotNone] IBufferProtocol data)
 {
     using var buffer = data.GetBufferNoThrow();
     if (buffer is null)
     {
         throw PythonOps.TypeError($"argument should be bytes, buffer or ASCII string, not '{PythonOps.GetPythonTypeName(data)}'");
     }
     return(a2b_uu_impl(context, buffer.AsReadOnlySpan()));
Esempio n. 2
0
            private void WriteBufferProtocol(IBufferProtocol b)
            {
                using var buffer = b.GetBufferNoThrow();
                if (buffer is null)
                {
                    throw PythonOps.ValueError("unmarshallable object");
                }
                var span = buffer.AsReadOnlySpan();

                _bytes.Add((byte)'s');
                WriteInt32(span.Length);
                _bytes.AddRange(span.ToArray());
            }
Esempio n. 3
0
            public BigInteger readinto([NotNull] IBufferProtocol buffer)
            {
                using var pythonBuffer = buffer.GetBufferNoThrow(BufferFlags.Writable)
                                         ?? throw PythonOps.TypeError("readinto() argument must be read-write bytes-like object, not {0}", PythonTypeOps.GetName(buffer));

                _checkClosed();

                var span = pythonBuffer.AsSpan();
                int len  = Math.Min(_length - _pos, span.Length);

                _data.AsSpan(_pos, len).CopyTo(span);
                _pos += len;
                return(len);
            }
Esempio n. 4
0
            public BigInteger readinto([NotNull] IBufferProtocol buffer)
            {
                EnsureReadable();

                using var pythonBuffer = buffer.GetBufferNoThrow(BufferFlags.Writable)
                                         ?? throw PythonOps.TypeError("readinto() argument must be read-write bytes-like object, not {0}", PythonTypeOps.GetName(buffer));

                _checkClosed();

                var span = pythonBuffer.AsSpan();

                for (int i = 0; i < span.Length; i++)
                {
                    int b = _readStream.ReadByte();
                    if (b == -1)
                    {
                        return(i);
                    }
                    span[i] = (byte)b;
                }

                return(span.Length);
            }