public void ReadDouble() { const double expected = 8.7; var bytes = BitConverter.GetBytes(expected); Array.Reverse(bytes); Underlying.Write(bytes, 0, bytes.Length); Underlying.WriteByte(8); Underlying.Seek(0, SeekOrigin.Begin); ReadBuffer.Ensure(9); Assert.That(ReadBuffer.ReadDouble(), Is.EqualTo(expected)); Assert.That(ReadBuffer.ReadByte(), Is.EqualTo(8)); }
public override async ValueTask <NpgsqlTsVector> Read(ReadBuffer buf, int len, bool async, FieldDescription fieldDescription = null) { await buf.Ensure(4, async); var numLexemes = buf.ReadInt32(); len -= 4; var lexemes = new List <NpgsqlTsVector.Lexeme>(); for (var lexemePos = 0; lexemePos < numLexemes; lexemePos++) { await buf.Ensure(Math.Min(len, MaxSingleLexemeBytes), async); var posBefore = buf.ReadPosition; List <NpgsqlTsVector.Lexeme.WordEntryPos> positions = null; var lexemeString = buf.ReadNullTerminatedString(); int numPositions = buf.ReadInt16(); for (var i = 0; i < numPositions; i++) { var wordEntryPos = buf.ReadInt16(); if (positions == null) { positions = new List <NpgsqlTsVector.Lexeme.WordEntryPos>(); } positions.Add(new NpgsqlTsVector.Lexeme.WordEntryPos(wordEntryPos)); } lexemes.Add(new NpgsqlTsVector.Lexeme(lexemeString, positions, true)); len -= buf.ReadPosition - posBefore; } return(new NpgsqlTsVector(lexemes, true)); }
async ValueTask <BitVector32> IChunkingTypeHandler <BitVector32> .Read(ReadBuffer buf, int len, bool async, FieldDescription fieldDescription) { if (len > 4 + 4) { buf.Skip(len); throw new SafeReadException("Can't read PostgreSQL bitstring with more than 32 bits into BitVector32"); } await buf.Ensure(4 + 4, async); var numBits = buf.ReadInt32(); return(numBits == 0 ? new BitVector32(0) : new BitVector32(buf.ReadInt32())); }
public void Skip() { for (byte i = 0; i < 50; i++) { Writer.WriteByte(i); } ReadBuffer.Ensure(10); ReadBuffer.Skip(7); Assert.That(ReadBuffer.ReadByte(), Is.EqualTo(7)); ReadBuffer.Skip(10); ReadBuffer.Ensure(1); Assert.That(ReadBuffer.ReadByte(), Is.EqualTo(18)); ReadBuffer.Skip(20); ReadBuffer.Ensure(1); Assert.That(ReadBuffer.ReadByte(), Is.EqualTo(39)); }
async ValueTask <bool> IChunkingTypeHandler <bool> .Read(ReadBuffer buf, int len, bool async, FieldDescription fieldDescription) { await buf.Ensure(5, async); var bitLen = buf.ReadInt32(); if (bitLen != 1) { // This isn't a single bit - error. // Consume the rest of the value first so the connection is left in a good state. buf.Skip(len - 4); throw new SafeReadException(new InvalidCastException("Can't convert a BIT(N) type to bool, only BIT(1)")); } var b = buf.ReadByte(); return((b & 128) != 0); }
public async Task ReadNullTerminatedString_with_io() { Writer.Write(PGUtil.UTF8Encoding.GetBytes(new string("Chunked "))); ReadBuffer.Ensure(1); var task = ReadBuffer.ReadNullTerminatedString(async: true); Assert.That(!task.IsCompleted); Writer .Write(PGUtil.UTF8Encoding.GetBytes(new string("string"))) .WriteByte(0) .Write(PGUtil.UTF8Encoding.GetBytes(new string("bar"))) .WriteByte(0); Assert.That(task.IsCompleted); Assert.That(await task, Is.EqualTo("Chunked string")); Assert.That(ReadBuffer.ReadNullTerminatedString(), Is.EqualTo("bar")); }
public void Skip() { for (byte i = 0; i < 50; i++) { Underlying.WriteByte(i); } Underlying.Seek(0, SeekOrigin.Begin); ReadBuffer.Ensure(10); ReadBuffer.Skip(7); Assert.That(ReadBuffer.ReadByte(), Is.EqualTo(7)); ReadBuffer.Skip(10); ReadBuffer.Ensure(1); Assert.That(ReadBuffer.ReadByte(), Is.EqualTo(18)); ReadBuffer.Skip(20); ReadBuffer.Ensure(1); Assert.That(ReadBuffer.ReadByte(), Is.EqualTo(39)); }
public override async ValueTask <NpgsqlRange <TElement> > Read(ReadBuffer buf, int len, bool async, FieldDescription fieldDescription = null) { await buf.Ensure(1, async); var flags = (RangeFlags)buf.ReadByte(); if ((flags & RangeFlags.Empty) != 0) { return(NpgsqlRange <TElement> .Empty); } TElement lowerBound = default(TElement), upperBound = default(TElement); if ((flags & RangeFlags.LowerBoundInfinite) == 0) { lowerBound = await ElementHandler.ReadWithLength <TElement>(buf, async); } if ((flags & RangeFlags.UpperBoundInfinite) == 0) { upperBound = await ElementHandler.ReadWithLength <TElement>(buf, async); } return(new NpgsqlRange <TElement>(lowerBound, upperBound, flags)); }
async ValueTask <PostgisGeometry> DoRead(ReadBuffer buf, WkbIdentifier id, ByteOrder bo, bool async) { switch (id) { case WkbIdentifier.Point: await buf.Ensure(16, async); return(new PostgisPoint(buf.ReadDouble(bo), buf.ReadDouble(bo))); case WkbIdentifier.LineString: { await buf.Ensure(4, async); var points = new Coordinate2D[buf.ReadInt32(bo)]; for (var ipts = 0; ipts < points.Length; ipts++) { await buf.Ensure(16, async); points[ipts] = new Coordinate2D(buf.ReadDouble(bo), buf.ReadDouble(bo)); } return(new PostgisLineString(points)); } case WkbIdentifier.Polygon: { await buf.Ensure(4, async); var rings = new Coordinate2D[buf.ReadInt32(bo)][]; for (var irng = 0; irng < rings.Length; irng++) { await buf.Ensure(4, async); rings[irng] = new Coordinate2D[buf.ReadInt32(bo)]; for (var ipts = 0; ipts < rings[irng].Length; ipts++) { await buf.Ensure(16, async); rings[irng][ipts] = new Coordinate2D(buf.ReadDouble(bo), buf.ReadDouble(bo)); } } return(new PostgisPolygon(rings)); } case WkbIdentifier.MultiPoint: { await buf.Ensure(4, async); var points = new Coordinate2D[buf.ReadInt32(bo)]; for (var ipts = 0; ipts < points.Length; ipts++) { await buf.Ensure(21, async); await buf.Skip(5, async); points[ipts] = new Coordinate2D(buf.ReadDouble(bo), buf.ReadDouble(bo)); } return(new PostgisMultiPoint(points)); } case WkbIdentifier.MultiLineString: { await buf.Ensure(4, async); var rings = new Coordinate2D[buf.ReadInt32(bo)][]; for (var irng = 0; irng < rings.Length; irng++) { await buf.Ensure(9, async); await buf.Skip(5, async); rings[irng] = new Coordinate2D[buf.ReadInt32(bo)]; for (var ipts = 0; ipts < rings[irng].Length; ipts++) { await buf.Ensure(16, async); rings[irng][ipts] = new Coordinate2D(buf.ReadDouble(bo), buf.ReadDouble(bo)); } } return(new PostgisMultiLineString(rings)); } case WkbIdentifier.MultiPolygon: { await buf.Ensure(4, async); var pols = new Coordinate2D[buf.ReadInt32(bo)][][]; for (var ipol = 0; ipol < pols.Length; ipol++) { await buf.Ensure(9, async); await buf.Skip(5, async); pols[ipol] = new Coordinate2D[buf.ReadInt32(bo)][]; for (var irng = 0; irng < pols[ipol].Length; irng++) { await buf.Ensure(4, async); pols[ipol][irng] = new Coordinate2D[buf.ReadInt32(bo)]; for (var ipts = 0; ipts < pols[ipol][irng].Length; ipts++) { await buf.Ensure(16, async); pols[ipol][irng][ipts] = new Coordinate2D(buf.ReadDouble(bo), buf.ReadDouble(bo)); } } } return(new PostgisMultiPolygon(pols)); } case WkbIdentifier.GeometryCollection: { await buf.Ensure(4, async); var g = new PostgisGeometry[buf.ReadInt32(bo)]; for (var i = 0; i < g.Length; i++) { await buf.Ensure(5, async); var elemBo = (ByteOrder)buf.ReadByte(); var elemId = (WkbIdentifier)(buf.ReadUInt32(bo) & 7); g[i] = await DoRead(buf, elemId, elemBo, async); } return(new PostgisGeometryCollection(g)); } default: throw new InvalidOperationException("Unknown Postgis identifier."); } }
public override async ValueTask <NpgsqlTsQuery> Read(ReadBuffer buf, int len, bool async, FieldDescription fieldDescription = null) { await buf.Ensure(4, async); var numTokens = buf.ReadInt32(); if (numTokens == 0) { return(new NpgsqlTsQueryEmpty()); } _nodes = new Stack <Tuple <NpgsqlTsQuery, int> >(); len -= 4; for (var tokenPos = 0; tokenPos < numTokens; tokenPos++) { await buf.Ensure(Math.Min(len, MaxSingleTokenBytes), async); var readPos = buf.ReadPosition; var isOper = buf.ReadByte() == 2; if (isOper) { var operKind = (NpgsqlTsQuery.NodeKind)buf.ReadByte(); if (operKind == NpgsqlTsQuery.NodeKind.Not) { var node = new NpgsqlTsQueryNot(null); InsertInTree(node); _nodes.Push(new Tuple <NpgsqlTsQuery, int>(node, 0)); } else { NpgsqlTsQuery node = null; switch (operKind) { case NpgsqlTsQuery.NodeKind.And: node = new NpgsqlTsQueryAnd(null, null); break; case NpgsqlTsQuery.NodeKind.Or: node = new NpgsqlTsQueryOr(null, null); break; default: throw new InvalidOperationException($"Internal Npgsql bug: unexpected value {operKind} of enum {nameof(NpgsqlTsQuery.NodeKind)}. Please file a bug."); } InsertInTree(node); _nodes.Push(new Tuple <NpgsqlTsQuery, int>(node, 2)); _nodes.Push(new Tuple <NpgsqlTsQuery, int>(node, 1)); } } else { var weight = (NpgsqlTsQueryLexeme.Weight)buf.ReadByte(); var prefix = buf.ReadByte() != 0; var str = buf.ReadNullTerminatedString(); InsertInTree(new NpgsqlTsQueryLexeme(str, weight, prefix)); } len -= buf.ReadPosition - readPos; } if (_nodes.Count != 0) { throw new InvalidOperationException("Internal Npgsql bug, please report."); } return(_value); }