]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/netstd/Thrift/Transport/Client/TMemoryBufferTransport.cs
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / netstd / Thrift / Transport / Client / TMemoryBufferTransport.cs
1 // Licensed to the Apache Software Foundation(ASF) under one
2 // or more contributor license agreements.See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17
18 using System;
19 using System.Diagnostics;
20 using System.IO;
21 using System.Threading;
22 using System.Threading.Tasks;
23
24 namespace Thrift.Transport.Client
25 {
26 // ReSharper disable once InconsistentNaming
27 public class TMemoryBufferTransport : TTransport
28 {
29 private bool IsDisposed;
30 private byte[] Bytes;
31 private int _bytesUsed;
32
33 public TMemoryBufferTransport()
34 {
35 Bytes = new byte[2048]; // default size
36 }
37
38 public TMemoryBufferTransport(int initialCapacity)
39 {
40 Bytes = new byte[initialCapacity]; // default size
41 }
42
43 public TMemoryBufferTransport(byte[] buf)
44 {
45 Bytes = (byte[])buf.Clone();
46 _bytesUsed = Bytes.Length;
47 }
48
49 public int Position { get; set; }
50
51 public int Capacity
52 {
53 get
54 {
55 Debug.Assert(_bytesUsed <= Bytes.Length);
56 return Bytes.Length;
57 }
58 set
59 {
60 Array.Resize(ref Bytes, value);
61 _bytesUsed = value;
62 }
63 }
64
65 public int Length
66 {
67 get {
68 Debug.Assert(_bytesUsed <= Bytes.Length);
69 return _bytesUsed;
70 }
71 set {
72 if ((Bytes.Length < value) || (Bytes.Length > (10 * value)))
73 Array.Resize(ref Bytes, Math.Max(2048, (int)(value * 1.25)));
74 _bytesUsed = value;
75 }
76 }
77
78 public void SetLength(int value)
79 {
80 Length = value;
81 Position = Math.Min(Position, value);
82 }
83
84 public override bool IsOpen => true;
85
86 public override async Task OpenAsync(CancellationToken cancellationToken)
87 {
88 if (cancellationToken.IsCancellationRequested)
89 {
90 await Task.FromCanceled(cancellationToken);
91 }
92 }
93
94 public override void Close()
95 {
96 /** do nothing **/
97 }
98
99 public void Seek(int delta, SeekOrigin origin)
100 {
101 int newPos;
102 switch (origin)
103 {
104 case SeekOrigin.Begin:
105 newPos = delta;
106 break;
107 case SeekOrigin.Current:
108 newPos = Position + delta;
109 break;
110 case SeekOrigin.End:
111 newPos = _bytesUsed + delta;
112 break;
113 default:
114 throw new ArgumentException(nameof(origin));
115 }
116
117 if ((0 > newPos) || (newPos > _bytesUsed))
118 throw new ArgumentException(nameof(origin));
119 Position = newPos;
120 }
121
122 public override ValueTask<int> ReadAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
123 {
124 var count = Math.Min(Length - Position, length);
125 Buffer.BlockCopy(Bytes, Position, buffer, offset, count);
126 Position += count;
127 return new ValueTask<int>(count);
128 }
129
130 public override Task WriteAsync(byte[] buffer, CancellationToken cancellationToken)
131 {
132 return WriteAsync(buffer, 0, buffer.Length, cancellationToken);
133 }
134
135 public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
136 {
137 var free = Length - Position;
138 Length = Length + count - free;
139 Buffer.BlockCopy(buffer, offset, Bytes, Position, count);
140 Position += count;
141 return Task.CompletedTask;
142 }
143
144 public override async Task FlushAsync(CancellationToken cancellationToken)
145 {
146 if (cancellationToken.IsCancellationRequested)
147 {
148 await Task.FromCanceled(cancellationToken);
149 }
150 }
151
152 public byte[] GetBuffer()
153 {
154 var retval = new byte[Length];
155 Buffer.BlockCopy(Bytes, 0, retval, 0, Length);
156 return retval;
157 }
158
159 internal bool TryGetBuffer(out ArraySegment<byte> bufSegment)
160 {
161 bufSegment = new ArraySegment<byte>(Bytes, 0, _bytesUsed);
162 return true;
163 }
164
165
166 // IDisposable
167 protected override void Dispose(bool disposing)
168 {
169 if (!IsDisposed)
170 {
171 if (disposing)
172 {
173 // nothing to do
174 }
175 }
176 IsDisposed = true;
177 }
178 }
179 }