public GBXString(Stream s) { uint length = s.ReadUInt(); byte[] srcdata = s.SimpleRead((int)length); Value = UTF8.GetString(srcdata); }
internal async Task ProcessSmqAsync() { await Task.Yield(); var token = this.Token; while (!token.IsCancellationRequested && this.Socket.State == WebSocketState.Open) { if (this.SocketMessageQueue.IsEmpty) { break; } if (!this.SocketMessageQueue.TryDequeue(out var message)) { break; } var buff = UTF8.GetBytes(message); var msgc = buff.Length / BUFFER_SIZE; if (buff.Length % BUFFER_SIZE != 0) { msgc++; } for (var i = 0; i < msgc; i++) { var off = BUFFER_SIZE * i; var cnt = Math.Min(BUFFER_SIZE, buff.Length - off); var lm = i == msgc - 1; await this.Socket.SendAsync(new ArraySegment <byte>(buff, off, cnt), WebSocketMessageType.Text, lm, this.Token).ConfigureAwait(false); } } }
Task AwaitConnectionStart(IContext context) { switch (context.Message) { case (Inbound, ConnectionStart message): { if (!message.Mechanisms.Contains(_connectionConfiguration.AuthenticationStrategy.Mechanism)) { // TODO: Unable to agree on authentication mechanism... throw new Exception(); } _state.AuthenticationStage = 0; var authenticationResponse = _connectionConfiguration.AuthenticationStrategy.Respond((Byte)_state.AuthenticationStage, new Byte[0]); // TODO: Verify protocol version compatibility... context.Send(context.Parent, (Outbound, new ConnectionStartOk( peerProperties: _connectionConfiguration.PeerProperties.ToDictionary(), mechanism: _connectionConfiguration.AuthenticationStrategy.Mechanism, response: UTF8.GetString(authenticationResponse), locale: "en_US" ))); _behaviour.Become(AwaitConnectionTune); return(Done); } default: return(Done); } }
Task AwaitConnectionTune(IContext context) { switch (context.Message) { case (Inbound, ConnectionSecure message): { _state.AuthenticationStage++; var challenge = UTF8.GetBytes(message.Challenge); var authenticationResponse = _connectionConfiguration.AuthenticationStrategy.Respond(stage: _state.AuthenticationStage, challenge: challenge); context.Send(context.Parent, (Outbound, new ConnectionSecureOk(UTF8.GetString(authenticationResponse)))); return(Done); } case (Inbound, ConnectionTune message): { var heartbeatFrequency = Min(message.Heartbeat, _connectionConfiguration.HeartbeatFrequency); var maximumFrameSize = Min(message.FrameMax, _connectionConfiguration.MaximumFrameSize); var maximumChannelCount = Min(message.ChannelMax, _connectionConfiguration.MaximumChannelCount); context.Send(context.Parent, (Outbound, new ConnectionTuneOk( channelMax: maximumChannelCount, frameMax: maximumFrameSize, heartbeat: heartbeatFrequency ))); context.Send(context.Parent, (StartHeartbeatTransmission, frequency: heartbeatFrequency)); context.Send(context.Parent, (Outbound, new ConnectionOpen( virtualHost: _connectionConfiguration.VirtualHost ))); _behaviour.Become(AwaitConnectionOpenOk); return(Done); } default: return(Done); } }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldReturnUsersWithFlags() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldReturnUsersWithFlags() { _authManager.newUser("andres", UTF8.encode("123"), false); IDictionary <string, object> expected = map("neo4j", ListOf(_pwdChange), "andres", ListOf()); AssertSuccess(_admin, "CALL dbms.security.listUsers()", r => assertKeyIsMap(r, "username", "flags", expected)); }
private void ExceptionTest() { Assert.ThrowsException <UTF8Exception>( delegate { UTF8.Decode("\uFFFF"); }); Assert.ThrowsException <UTF8Exception>( delegate { UTF8.Decode("\xE9\x00\x00"); }); Assert.ThrowsException <UTF8Exception>( delegate { UTF8.Decode("\xC2\uFFFF"); }); Assert.ThrowsException <UTF8Exception>( delegate { UTF8.Decode("\xF0\x9D"); }); }
public void ClearStorageAndWriteNextRandomValue(long nextRandomValue) { var nextRandomValueBytes = UTF8.GetBytes($"{nextRandomValue}{StorageLinesSeparator}"); _storage.Write(nextRandomValueBytes); _storageBytes = nextRandomValueBytes; }
public void ToLowerInvariant() { if (this.Array != null) { UTF8.ToLowerInvariant(this.Array, this.Index, this.Length); } }
/// <summary> /// Creates a <see cref="WaveFileWriter"/> that writes to a <see cref="Stream"/>. /// </summary> public WaveFileWriter(Stream outStream, WaveFormat format) { _ofstream = outStream; _writer = new BinaryWriter(outStream, UTF8); _writer.Write(UTF8.GetBytes("RIFF")); _writer.Write(0); // placeholder _writer.Write(UTF8.GetBytes("WAVEfmt ")); _waveFormat = format; _writer.Write(18 + format.ExtraSize); // wave format Length format.Serialize(_writer); // CreateFactChunk if (format.Encoding != WaveFormatTag.Pcm) { _writer.Write(UTF8.GetBytes("fact")); _writer.Write(4); _factSampleCountPos = outStream.Position; _writer.Write(0); // number of samples } // WriteDataChunkHeader _writer.Write(UTF8.GetBytes("data")); _dataSizePos = outStream.Position; _writer.Write(0); // placeholder Length = 0; }
public ConsoleWithStorage(UserConsole console, Storage storage) { _console = console; _storage = storage; _storageBytes = _storage.Read(); var storageLines = UTF8.GetString(_storageBytes) .Split(new[] { StorageLinesSeparator }, StringSplitOptions.RemoveEmptyEntries); _unusedStorageLines = new Queue <string>(storageLines); var isStorageEmpty = _storageBytes == null || _storageBytes.Length == 0; if (isStorageEmpty) { const long firstRandomValue = 420L; AddToStorage(firstRandomValue); _nextRandomValue = firstRandomValue; } else { var nextRandomValue = _unusedStorageLines.Dequeue(); _nextRandomValue = System.Convert.ToInt64(nextRandomValue); } }
async Task ProcessAsyncIO() { using (var stream = new FileStream("test1.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None, BUFFER_SIZE)) { Console.WriteLine($"1. Use I/O Thread : {stream.IsAsync}"); byte[] buffer = UTF8.GetBytes(CreateFileContent()); var writeTask = Task.Factory.FromAsync(stream.BeginWrite, stream.EndWrite, buffer, 0, buffer.Length, null); await writeTask; } using (var stream = new FileStream("test2.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None, BUFFER_SIZE, FileOptions.Asynchronous)) { Console.WriteLine($"2. Uses I/O Thread : {stream.IsAsync}"); byte[] buffer = UTF8.GetBytes(CreateFileContent()); var writeTask = Task.Factory.FromAsync(stream.BeginWrite, stream.EndWrite, buffer, 0, buffer.Length, null); await writeTask; } using (var stream = File.Create("test3.txt", BUFFER_SIZE, FileOptions.Asynchronous)) using (var sw = new StreamWriter(stream)) { Console.WriteLine($"3. Uses I/O Thread : {stream.IsAsync}"); await sw.WriteAsync(CreateFileContent()); } using (var sw = new StreamWriter("test4.txt", true)) { Console.WriteLine($"4. Uses I/O Thread : {((FileStream)sw.BaseStream).IsAsync}"); await sw.WriteAsync(CreateFileContent()); } Console.WriteLine("Starting parsing files in parallel"); var readTasks = new Task <long> [4]; for (int i = 0; i < readTasks.Length; i++) { string fileName = $"test{i + 1}.txt"; readTasks[i] = SumFileContent(fileName); } long[] sums = await Task.WhenAll(readTasks); Console.WriteLine($"Sum in all files : {sums.Sum()}"); Console.WriteLine($"Deleting files"); Task[] deleteTasks = new Task[4]; for (int i = 0; i < deleteTasks.Length; i++) { string fileName = $"test{i + 1}.txt"; deleteTasks[i] = SimulateAsuncDelete(fileName); } await Task.WhenAll(deleteTasks); Console.WriteLine("Deleting complete."); }
private bool ValidateWsUsernameToken(WsUsernameToken wsUsernameToken) { if (wsUsernameToken.Username != _username) { return(false); } var isClearText = wsUsernameToken.Password?.Type == null || wsUsernameToken.Password.Type == _passwordTextType; if (isClearText) { return(wsUsernameToken.Password?.Value == _password); } var nonceArray = wsUsernameToken.Nonce != null ? wsUsernameToken.Nonce : Array.Empty <byte>(); var createdArray = wsUsernameToken.Created != null?UTF8.GetBytes(wsUsernameToken.Created) : Array.Empty <byte>(); var passwordArray = _password != null?UTF8.GetBytes(_password) : Array.Empty <byte>(); var hashArray = new byte[nonceArray.Length + createdArray.Length + passwordArray.Length]; Array.Copy(nonceArray, 0, hashArray, 0, nonceArray.Length); Array.Copy(createdArray, 0, hashArray, nonceArray.Length, createdArray.Length); Array.Copy(passwordArray, 0, hashArray, nonceArray.Length + createdArray.Length, passwordArray.Length); var hash = SHA1.Create().ComputeHash(hashArray); var serverPasswordDigest = ToBase64String(hash); var clientPasswordDigest = wsUsernameToken.Password?.Value; return(serverPasswordDigest == clientPasswordDigest); }
private void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt) { using (var hmac = new HMACSHA512()) { passwordSalt = hmac.Key; passwordHash = hmac.ComputeHash(UTF8.GetBytes(password)); } }
public bool SendFile(string path) { if (ConnectedSocket != null && ConnectedSocket.Connected) { try { FileInfo fi = new FileInfo(path); byte[] len = BitConverter.GetBytes(fi.Length); byte[] name = UTF8.GetBytes(fi.Name); byte[] nameLen = BitConverter.GetBytes(name.Length); byte[] head = new byte[1 + len.Length + nameLen.Length + name.Length]; head[0] = (byte)ChatType.File; Array.Copy(len, 0, head, 1, len.Length); Array.Copy(nameLen, 0, head, 1 + len.Length, nameLen.Length); Array.Copy(name, 0, head, 1 + len.Length + nameLen.Length, name.Length); ConnectedSocket.SendFile( path, head, null, TransmitFileOptions.UseDefaultWorkerThread ); return(true); } catch (Exception e) { // 连接断开了 Console.WriteLine("send file exception : " + e.Message); } } return(false); }
//------------------------------------------------------------------------- public override string GetString(byte[] bytes, int index, int count) { if (null == bytes || count > bytes.Length) { return(string.Empty); } byte[] pp = new byte[count]; Array.Clear(pp, 0, count); Array.Copy(bytes, index, pp, 0, count); string strAll = BitConverter.ToString(pp); string strHex = strAll.Replace(NativeiOS.m_strSplit, ""); int nIndex = strAll.IndexOf(NativeiOS.m_strSplit); if (-1 != nIndex) { strHex = strAll.Substring(0, nIndex); } string strNormal = UTF8.GetString(bytes, index, count).Trim('\0'); StringBuilder sb = new StringBuilder(); sb.Append("_GB2312ToUtf8 BitConverter = " + BitConverter.ToString(pp)); sb.Append(" / "); sb.Append("_GB2312ToUtf8 strHex = " + strHex); sb.Append(" / "); sb.Append("_GB2312ToUtf8 strNormal = " + strNormal); Debug.Log(sb.ToString()); return(strHex); }
/// <summary> /// Compare this ByteBlock to another one and return whether other /// starts with this value. /// </summary> /// <param name="other">ByteBlock to compare with</param> /// <returns>Positive if this is greater, negative if other greater, 0 if other starts with this</returns> public int CaseInsensitiveIsPrefixOf(ByteBlock other) { int commonLength = Math.Min(this.Length, other.Length); // Compare bytes and return the first difference, if any for (int i = 0; i < commonLength; ++i) { byte tC = this.Array[this.Index + i]; tC = UTF8.ToLowerInvariant(tC); byte oC = other.Array[other.Index + i]; oC = UTF8.ToLowerInvariant(oC); int cmp = tC.CompareTo(oC); if (cmp != 0) { return(cmp); } } // If identical, this is a prefix if other is at least as long if (other.Length >= this.Length) { return(0); } // Otherwise, this is a later value return(1); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test @SuppressWarnings("unchecked") public void shouldDeserializeMultipleStatements() public virtual void ShouldDeserializeMultipleStatements() { // Given string json = createJsonFrom(map("statements", asList(map("statement", "Blah blah", "parameters", map("one", 12)), map("statement", "Blah bluh", "parameters", map("asd", asList("one, two")))))); // When StatementDeserializer de = new StatementDeserializer(new MemoryStream(UTF8.encode(json))); // Then //JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops: assertThat(de.HasNext(), equalTo(true)); //JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops: Statement stmt = de.Next(); assertThat(stmt.StatementConflict(), equalTo("Blah blah")); assertThat(stmt.Parameters(), equalTo(map("one", 12))); //JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops: assertThat(de.HasNext(), equalTo(true)); //JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops: Statement stmt2 = de.Next(); assertThat(stmt2.StatementConflict(), equalTo("Blah bluh")); assertThat(stmt2.Parameters(), equalTo(map("asd", asList("one, two")))); //JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops: assertThat(de.HasNext(), equalTo(false)); }
internal static bool DecompressArchive(string ArchivePath, string DestinationDirectory) { using (FileStream Reader = OpenRead(ArchivePath)) { byte[] Buffer = new byte[4]; Reader.Read(Buffer, 0, 4); using (CRC32 CRC = new CRC32()) if (!CRC.ComputeHash(Reader).SequenceEqual(Buffer)) { return(false); } Reader.Position = 4L; using (MemoryStream Stream = new MemoryStream()) { DecompressSingleFile(Reader, Stream); Stream.Position = 0L; while (Stream.Position != Stream.Length) { int RPathSize = Stream.ReadByte(); byte[] RPath = new byte[RPathSize]; Stream.Read(RPath, 0, RPathSize); string APath = $@"{DestinationDirectory}\{UTF8.GetString(RPath)}"; Directory.CreateDirectory(Path.GetDirectoryName(APath)); Stream.Read(Buffer, 0, 4); using (FileStream Writer = Create(APath)) { byte[] FileBuffer = new byte[ToUInt32(Buffer, 0)]; Stream.Read(FileBuffer, 0, FileBuffer.Length); Writer.Write(FileBuffer, 0, FileBuffer.Length); } } } } return(true); }
private async Task <(int Status, string Body)> SendSocketRequestAsync(string path) { using (var connection = _fixture.CreateTestConnection()) { await connection.Send( "GET " + path + " HTTP/1.1", "Host: " + _fixture.Client.BaseAddress.Authority, "", ""); var headers = await connection.ReceiveHeaders(); var status = int.Parse(headers[0].Substring(9, 3), CultureInfo.InvariantCulture); if (headers.Contains("Transfer-Encoding: chunked")) { var bytes0 = await connection.ReceiveChunk(); Assert.False(bytes0.IsEmpty); return(status, Encoding.UTF8.GetString(bytes0.Span)); } var length = int.Parse(headers.Single(h => h.StartsWith("Content-Length: ", StringComparison.Ordinal)).Substring("Content-Length: ".Length), CultureInfo.InvariantCulture); var bytes1 = await connection.Receive(length); return(status, Encoding.ASCII.GetString(bytes1.Span)); } }
private static void WriteString(FileStream Stream, string String) { byte[] EncodedString = UTF8.GetBytes(String ?? string.Empty); Stream.Write(GetBytes(EncodedString.Length + 1), 0, 4); Stream.Write(EncodedString, 0, EncodedString.Length); Stream.WriteByte(0); }
public virtual string ReadString() { /* Inspection of BinaryWriter-written files * shows that the length is given in bytes, * not chars */ int len = Read7BitEncodedInt(); if (len < 0) { throw new IOException("Invalid binary file (string len < 0)"); } if (len == 0) { return(System.String.Empty); } if (charByteBuffer == null) { charBuffer = new char [512]; charByteBuffer = new byte [MaxBufferSize]; } // // We read the string here in small chunks. Also, we // Attempt to optimize the common case of short strings. // StringBuilder sb = null; do { int readLen = Math.Min(MaxBufferSize, len); int n = m_stream.Read(charByteBuffer, 0, readLen); if (n == 0) { throw new EndOfStreamException(); } int cch = UTF8.decode(charByteBuffer, 0, n, charBuffer, 0); if (sb == null && readLen == len) // ok, we got out the easy way, dont bother with the sb { return(new String(charBuffer, 0, cch)); } if (sb == null) { // Len is a fairly good estimate of the number of chars in a string // Most of the time 1 byte == 1 char sb = new StringBuilder(len); } sb.Append(charBuffer, 0, cch); len -= readLen; } while (len > 0); return(sb.ToString()); }
public async Task ThenIfFileHasMissingFieldsReturnError(string header) { var inputData = $"{header}" + @" Abba123,1113335559,Froberg,Chris,1998-12-08,SE123321C,25,,,2,2120-08,2125-08,1500,,Employer ref,Provider ref"; var textStream = new MemoryStream(UTF8.GetBytes(inputData)); _file.Setup(m => m.InputStream).Returns(textStream); BulkUploadApprenticeshipsCommand commandArgument = null; _mockMediator.Setup(x => x.Send(It.IsAny <BulkUploadApprenticeshipsCommand>(), new CancellationToken())) .ReturnsAsync(new Unit()) .Callback((object x) => commandArgument = x as BulkUploadApprenticeshipsCommand); _mockMediator.Setup(m => m.Send(It.IsAny <GetCommitmentQueryRequest>(), new CancellationToken())) .Returns(Task.FromResult(new GetCommitmentQueryResponse { Commitment = new CommitmentView { AgreementStatus = AgreementStatus.NotAgreed, EditStatus = EditStatus.ProviderOnly } })); _mockMediator.Setup(m => m.Send(It.IsAny <GetOverlappingApprenticeshipsQueryRequest>(), It.IsAny <CancellationToken>())) .Returns( Task.Run(() => new GetOverlappingApprenticeshipsQueryResponse { Overlaps = new List <ApprenticeshipOverlapValidationResult> { new ApprenticeshipOverlapValidationResult { OverlappingApprenticeships = new List <OverlappingApprenticeship> { new OverlappingApprenticeship { Apprenticeship = new Apprenticeship { ULN = "1113335559" }, ValidationFailReason = ValidationFailReason.DateEmbrace } } } } })); var model = new UploadApprenticeshipsViewModel { Attachment = _file.Object, HashedCommitmentId = "ABBA123", ProviderId = 111 }; var file = await _sut.UploadFile("user123", model, new SignInUserModel()); //Assert Assert.IsTrue(file.HasFileLevelErrors); _logger.Verify(x => x.Info(It.IsAny <string>(), It.IsAny <long?>(), It.IsAny <long?>(), It.IsAny <long?>()), Times.Once); _logger.Verify(x => x.Error(It.IsAny <Exception>(), It.IsAny <string>(), It.IsAny <long?>(), It.IsAny <long?>(), It.IsAny <long?>()), Times.Never); }
/// <summary> /// Merges the specified original json. /// </summary> /// <param name="originalJson">The original json.</param> /// <param name="newContent">The new content.</param> /// <returns></returns> public static string Merge(string originalJson, string newContent) { using JsonDocument jDoc1 = JsonDocument.Parse(originalJson); using JsonDocument jDoc2 = JsonDocument.Parse(newContent); ReadOnlySpan <byte> merged = Merge(jDoc1, jDoc2); return(UTF8.GetString(merged)); }
public uint GetSizeWithPadding() { int keySpanLength = UTF8.GetByteCount(Key); uint totalSize = (uint)(keySpanLength + 1 + Value.Length); int paddingBytes = (int)(3 - ((totalSize + 3) % 4)); return((uint)(totalSize + paddingBytes)); }
public static int WriteNullTerminatedString(this BinaryWriter writer, string value) { writer.Align(); var position = (int)writer.BaseStream.Position; writer.Write(UTF8.GetBytes(value + '\0')); return(position); }
/// <summary> /// Computes the FNV-1a 256-bit hash for the specified data. /// </summary> /// <param name="data">The data.</param> /// <returns>The FNV-1a 256-bit hash of the specified data.</returns> /// <exception cref="AssertFailedException">Thrown if expected is not equal to actual.</exception> // ReSharper disable once InconsistentNaming private string Fnv1a256(string data) { AreEqual(256, this._alg.HashSize); string value = new BigInteger(this._alg.ComputeHash(UTF8.GetBytes(data)).AddZero()).ToString("X64", InvariantCulture); return(value.Substring(value.Length - 64)); }
private protected override void ImplWriteString(ref State state, string value, int expectedBytes) { DemandSpace(expectedBytes, this, ref state); int actualBytes = UTF8.GetBytes(value, 0, value.Length, ioBuffer, ioIndex); ioIndex += actualBytes; Helpers.DebugAssert(expectedBytes == actualBytes); }
/// <summary> /// Computes the FNV-1a 64-bit hash for the specified data. /// </summary> /// <param name="data">The data.</param> /// <returns>The FNV-1a 64-bit hash of the specified data.</returns> // ReSharper disable once InconsistentNaming private static string Fnv1a64s(this string data) { using (HashAlgorithm alg = new Fnv1a64()) { return("0x" + ((ulong)ToInt64(alg.ComputeHash(UTF8.GetBytes(data)), 0)).ToString("X16", InvariantCulture)); } }
/// <summary> /// Computes the FNV-1a 32-bit hash for the specified data. /// </summary> /// <param name="data">The data.</param> /// <returns>The FNV-1a 32-bit hash of the specified data.</returns> // ReSharper disable once InconsistentNaming private static string Fnv1a32s(this string data) { using (HashAlgorithm alg = new Fnv1a32()) { return("0x" + ((uint)ToInt32(alg.ComputeHash(UTF8.GetBytes(data)), 0)).ToString("X8", InvariantCulture)); } }
public static (byte[] passwordHash, byte[] passwordSalt) CreatePasswordHash(string password) { using var hmac = new HMACSHA512(); // Generates a random key (salt) var passwordSalt = hmac.Key; var passwordHash = hmac.ComputeHash(UTF8.GetBytes(password)); return(passwordHash, passwordSalt); }