本文整理汇总了C#中System.IO.MemoryStream.WriteString方法的典型用法代码示例。如果您正苦于以下问题:C# MemoryStream.WriteString方法的具体用法?C# MemoryStream.WriteString怎么用?C# MemoryStream.WriteString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.MemoryStream
的用法示例。
在下文中一共展示了MemoryStream.WriteString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteNullString
public void WriteNullString()
{
using (var stream = new MemoryStream())
{
stream.WriteString(null);
stream.Seek(0, SeekOrigin.Begin);
new StreamReader(stream).ReadToEnd().Should().BeEmpty();
}
}
示例2: WriteString
public void WriteString()
{
using (var stream = new MemoryStream())
{
stream.WriteString("test");
stream.Seek(0, SeekOrigin.Begin);
new StreamReader(stream).ReadToEnd().Should().Be("test");
}
}
示例3: AddFile
public void AddFile(Site site, string absoluteFileName, string content)
{
var storage = site.GetIsolatedStorage(_baseDir);
using (var memoryStream = new MemoryStream())
{
memoryStream.WriteString(content);
storage.CreateFile(absoluteFileName, memoryStream);
}
}
示例4: Encode
public override void Encode(Stream stream)
{
using (var body = new MemoryStream())
{
body.WriteShort(MessageIdentifier);
foreach (var item in topics)
{
body.WriteString(item);
}
FixedHeader.RemaingLength = (int)body.Length;
FixedHeader.WriteTo(stream);
body.WriteTo(stream);
}
}
示例5: Serialize
public void Serialize(Stream output)
{
var endian = this.Endian;
const uint headerSize = 32;
output.WriteValueU32(0x42424947, endian);
output.WriteValueU32(this.Version, endian);
var keys = new List<string>() {""};
int maxValueLength = 0;
var blob = new StringBuilder();
foreach (var file in this.Files)
{
keys.Add(file.Name);
foreach (var section in file.Sections)
{
keys.Add(section.Key);
foreach (var value in section.Value)
{
keys.Add(value.Key);
foreach (var item in value.Value)
{
if (item.Value != null)
{
blob.Append(item.Value + '\0');
maxValueLength = Math.Max(maxValueLength, item.Value.Length);
}
}
}
}
}
var huffmanEncoder = new Huffman.Encoder();
huffmanEncoder.Build(blob.ToString());
keys = keys.Distinct().OrderBy(k => k.HashCrc32()).ToList();
int maxKeyLength = keys.Max(k => k.Length);
uint stringTableSize;
using (var data = new MemoryStream())
{
data.Position = 4;
data.WriteValueS32(keys.Count, endian);
data.Position = 4 + 4 + (8 * keys.Count);
var offsets = new List<KeyValuePair<uint, uint>>();
foreach (var key in keys)
{
var offset = (uint)data.Position;
data.WriteValueU16((ushort)key.Length, endian);
data.WriteString(key, Encoding.UTF8);
offsets.Add(new KeyValuePair<uint, uint>(key.HashCrc32(), offset));
}
data.Position = 8;
foreach (var kv in offsets)
{
data.WriteValueU32(kv.Key, endian);
data.WriteValueU32(kv.Value - 8, endian);
}
data.Position = 0;
data.WriteValueU32((uint)data.Length, endian);
data.Position = 0;
stringTableSize = (uint)data.Length;
output.Seek(headerSize, SeekOrigin.Begin);
output.WriteFromStream(data, data.Length);
}
uint huffmanSize;
using (var data = new MemoryStream())
{
var pairs = huffmanEncoder.GetPairs();
data.WriteValueU16((ushort)pairs.Length, endian);
foreach (var pair in pairs)
{
data.WriteValueS32(pair.Left, endian);
data.WriteValueS32(pair.Right, endian);
}
data.Position = 0;
huffmanSize = (uint)data.Length;
output.Seek(headerSize + stringTableSize, SeekOrigin.Begin);
output.WriteFromStream(data, data.Length);
}
var bits = new BitArray(huffmanEncoder.TotalBits);
var bitOffset = 0;
uint indexSize;
using (var index = new MemoryStream())
{
var fileDataOffset = 2 + (this.Files.Count * 6);
var files = new List<KeyValuePair<ushort, int>>();
foreach (var file in this.Files.OrderBy(f => keys.IndexOf(f.Name)))
//.........这里部分代码省略.........
示例6: GetCommandText
/// <summary>
/// Process this.commandText, trimming each distinct command and substituting paramater
/// tokens.
/// </summary>
/// <param name="prepare"></param>
/// <param name="forExtendQuery"></param>
/// <returns>UTF8 encoded command ready to be sent to the backend.</returns>
private byte[] GetCommandText(bool prepare, bool forExtendQuery)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetCommandText");
MemoryStream commandBuilder = new MemoryStream();
StringChunk[] chunks;
chunks = GetDistinctTrimmedCommands(commandText);
if (chunks.Length > 1)
{
if (prepare || commandType == CommandType.StoredProcedure)
{
throw new NpgsqlException("Multiple queries not supported for this command type");
}
}
foreach (StringChunk chunk in chunks)
{
if (commandBuilder.Length > 0)
{
commandBuilder
.WriteBytes((byte)ASCIIBytes.SemiColon)
.WriteBytes(ASCIIByteArrays.LineTerminator);
}
if (prepare && ! forExtendQuery)
{
commandBuilder
.WriteString("PREPARE ")
.WriteString(planName)
.WriteString(" AS ");
}
if (commandType == CommandType.StoredProcedure)
{
if (! prepare && ! functionChecksDone)
{
functionNeedsColumnListDefinition = Parameters.Count != 0 && CheckFunctionNeedsColumnDefinitionList();
functionChecksDone = true;
}
commandBuilder.WriteString("SELECT * FROM ");
if (commandText[chunk.Begin + chunk.Length - 1] == ')')
{
AppendCommandReplacingParameterValues(commandBuilder, commandText, chunk.Begin, chunk.Length, prepare, forExtendQuery);
}
else
{
commandBuilder
.WriteString(commandText.Substring(chunk.Begin, chunk.Length))
.WriteBytes((byte)ASCIIBytes.ParenLeft);
if (prepare)
{
AppendParameterPlaceHolders(commandBuilder);
}
else
{
AppendParameterValues(commandBuilder);
}
commandBuilder.WriteBytes((byte)ASCIIBytes.ParenRight);
}
if (! prepare && functionNeedsColumnListDefinition)
{
AddFunctionColumnListSupport(commandBuilder);
}
}
else if (commandType == CommandType.TableDirect)
{
commandBuilder
.WriteString("SELECT * FROM ")
.WriteString(commandText.Substring(chunk.Begin, chunk.Length));
}
else
{
AppendCommandReplacingParameterValues(commandBuilder, commandText, chunk.Begin, chunk.Length, prepare, forExtendQuery);
}
}
return commandBuilder.ToArray();
}
示例7: SerializeString
/// <summary>
/// Creates a serialized string used in script signature.
/// </summary>
/// <remarks>
/// </remarks>
/// <example>
/// python: http://runnable.com/U3Mya-5oZntF5Ira/bitcoin-coinbase-serialize-string-python
/// nodejs: https://github.com/zone117x/node-stratum-pool/blob/dfad9e58c661174894d4ab625455bb5b7428881c/lib/util.js#L153
/// </example>
/// <param name="input"></param>
/// <returns></returns>
public static byte[] SerializeString(string input)
{
if (input.Length < 253)
return ArrayHelpers.Combine(new[] { (byte)input.Length }, Encoding.UTF8.GetBytes(input));
// if input string is >=253, we need need a special format.
byte[] result;
using (var stream = new MemoryStream())
{
if (input.Length < 0x10000)
{
stream.WriteValueU8(253);
stream.WriteValueU16(((UInt16)input.Length).LittleEndian()); // write packed length.
}
else if ((long)input.Length < 0x100000000)
{
stream.WriteValueU8(254);
stream.WriteValueU32(((UInt32)input.Length).LittleEndian()); // write packed length.
}
else
{
stream.WriteValueU8(255);
stream.WriteValueU16(((UInt16)input.Length).LittleEndian()); // write packed length.
}
stream.WriteString(input);
result = stream.ToArray();
}
return result;
}
示例8: GetClearCommandText
private byte[] GetClearCommandText()
{
if (NpgsqlEventLog.Level == LogLevel.Debug)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetClearCommandText");
}
MemoryStream result = new MemoryStream();
switch(type)
{
case CommandType.TableDirect:
result
.WriteString("SELECT * FROM ")
.WriteString(text.Trim());
return result.ToArray();
case CommandType.StoredProcedure:
if (!functionChecksDone)
{
functionNeedsColumnListDefinition = Parameters.Count != 0 && CheckFunctionNeedsColumnDefinitionList();
// Check if just procedure name was passed. If so, does not replace parameter names and just pass parameter values in order they were added in parameters collection. Also check if command text finishes in a ";" which would make Npgsql incorrectly append a "()" when executing this command text.
switch(text[text.Length - 1])
{
case ')' : case ';':
addProcedureParenthesis = false;
break;
default:
addProcedureParenthesis = true;
break;
}
functionChecksDone = true;
}
PGUtil.WriteString(
result,
Connector.SupportsPrepare
? "SELECT * FROM " // This syntax is only available in 7.3+ as well SupportsPrepare.
: "SELECT " //Only a single result return supported. 7.2 and earlier.
);
break;
}
if (parameters.Count == 0)
{
PGUtil.WriteString(result, text.Trim());
if (addProcedureParenthesis)
{
result
.WriteBytes((byte)ASCIIBytes.ParenLeft)
.WriteBytes((byte)ASCIIBytes.ParenRight);
}
if (functionNeedsColumnListDefinition)
{
AddFunctionColumnListSupport(result);
}
return result.ToArray();
}
// Get parameters in query string to translate them to their actual values.
// This regular expression gets all the parameters in format :param or @param
// and everythingelse.
// This is only needed if query string has parameters. Else, just append the
// parameter values in order they were put in parameter collection.
// If parenthesis don't need to be added, they were added by user with parameter names. Replace them.
if (!addProcedureParenthesis)
{
Dictionary<string, NpgsqlParameter> parameterIndex = new Dictionary<string, NpgsqlParameter>(parameters.Count, StringComparer.InvariantCultureIgnoreCase);
foreach (NpgsqlParameter parameter in parameters)
parameterIndex[parameter.CleanName] = parameter;
foreach (String s in parameterReplace.Split(text))
if (s.Length != 0)
{
NpgsqlParameter p = null;
string parameterName = s;
if ((parameterName[0] == ':') || (parameterName[0] == '@'))
{
parameterName = parameterName.Remove(0, 1);
parameterIndex.TryGetValue(parameterName, out p);
}
if (p != null)
{
switch(p.Direction)
{
case ParameterDirection.Input: case ParameterDirection.InputOutput:
//Wrap in probably-redundant parentheses. Queries should operate much as if they were in the a parameter or
//variable in a postgres function. Generally this is the case without the parentheses (hence "probably redundant")
//but there are exceptions to this rule. E.g. consider the postgres function:
//
//.........这里部分代码省略.........
示例9: TestStreamString
public void TestStreamString()
{
String obj = "Hallo Welt!";
using (MemoryStream ms = new MemoryStream())
{
ms.WriteString(obj);
ms.Seek(0, SeekOrigin.Begin);
String res = ms.ReadString();
Assert.IsTrue(obj.Equals(res), "Streaming failed!");
}
}
示例10: Serialize
public void Serialize(Stream output)
{
var saveGame = this.SaveGame;
byte[] innerUncompressedBytes;
using (var innerUncompressedData = new MemoryStream())
{
saveGame.Compose();
try
{
ProtoBuf.Serializer.Serialize(innerUncompressedData, saveGame);
}
finally
{
saveGame.Decompose();
}
innerUncompressedData.Position = 0;
innerUncompressedBytes = innerUncompressedData.ReadBytes((uint)innerUncompressedData.Length);
}
byte[] innerCompressedBytes;
using (var innerCompressedData = new MemoryStream())
{
var endian = this.Endian;
innerCompressedData.WriteValueS32(0, Endian.Big);
innerCompressedData.WriteString("WSG");
innerCompressedData.WriteValueU32(2, endian);
innerCompressedData.WriteValueU32(CRC32.Hash(innerUncompressedBytes, 0, innerUncompressedBytes.Length),
endian); // crc32
innerCompressedData.WriteValueS32(innerUncompressedBytes.Length, endian);
var encoder = new Huffman.Encoder();
encoder.Build(innerUncompressedBytes);
innerCompressedData.WriteBytes(encoder.Encode(innerUncompressedBytes));
innerCompressedData.Position = 0;
innerCompressedData.WriteValueU32((uint)(innerCompressedData.Length - 4), Endian.Big);
innerCompressedData.Position = 0;
innerCompressedBytes = innerCompressedData.ReadBytes((uint)innerCompressedData.Length);
}
byte[] compressedBytes;
if (innerCompressedBytes.Length <= BlockSize)
{
compressedBytes = new byte[innerCompressedBytes.Length +
(innerCompressedBytes.Length / 16) + 64 + 3];
var actualCompressedSize = compressedBytes.Length;
var result = LZO.Compress(innerCompressedBytes,
0,
innerCompressedBytes.Length,
compressedBytes,
0,
ref actualCompressedSize);
if (result != LZO.ErrorCode.Success)
{
throw new SaveCorruptionException(string.Format("LZO compression failure ({0})", result));
}
Array.Resize(ref compressedBytes, actualCompressedSize);
}
else
{
int innerCompressedOffset = 0;
int innerCompressedSizeLeft = innerCompressedBytes.Length;
using (var blockData = new MemoryStream())
{
var blockCount = (innerCompressedSizeLeft + BlockSize) / BlockSize;
blockData.WriteValueS32(blockCount, Endian.Big);
blockData.Position = 4 + (blockCount * 8);
var blockInfos = new List<Tuple<uint, uint>>();
while (innerCompressedSizeLeft > 0)
{
var blockUncompressedSize = Math.Min(BlockSize, innerCompressedSizeLeft);
compressedBytes = new byte[blockUncompressedSize +
(blockUncompressedSize / 16) + 64 + 3];
var actualCompressedSize = compressedBytes.Length;
var result = LZO.Compress(innerCompressedBytes,
innerCompressedOffset,
blockUncompressedSize,
compressedBytes,
0,
ref actualCompressedSize);
if (result != LZO.ErrorCode.Success)
{
throw new SaveCorruptionException(string.Format("LZO compression failure ({0})", result));
}
blockData.Write(compressedBytes, 0, actualCompressedSize);
blockInfos.Add(new Tuple<uint, uint>((uint)actualCompressedSize, BlockSize));
innerCompressedOffset += blockUncompressedSize;
//.........这里部分代码省略.........
示例11: saveToCprFile
/// <summary>
/// save PCCObject to file.
/// </summary>
/// <param name="newFileName">set full path + file name.</param>
/// <param name="saveCompress">set true if you want a zlib compressed pcc file.</param>
public void saveToCprFile(string newFileName = null, bool saveCompress = false)
{
bool bOverwriteFile = false;
if (string.IsNullOrWhiteSpace(newFileName) || newFileName == pccFileName)
{
bOverwriteFile = true;
newFileName = Path.GetFullPath(pccFileName) + ".tmp";
}
if (bDLCStored)
saveCompress = false;
using (MemoryStream newPccStream = new MemoryStream())
{
//ME3Explorer.DebugOutput.Clear();
DebugOutput.PrintLn("Saving file...");
DebugOutput.PrintLn("writing names list...");
// this condition needs a deeper check. todo...
if (bExtraNamesList)
{
//MessageBox.Show("sono dentro, dimensione extranamelist: " + extraNamesList.Length + " bytes");
newPccStream.Seek(headerSize, SeekOrigin.Begin);
newPccStream.Write(extraNamesList, 0, extraNamesList.Length);
}
//writing names list
newPccStream.Seek(NameOffset, SeekOrigin.Begin);
NameCount = Names.Count;
foreach (string name in Names)
{
newPccStream.WriteValueS32(-(name.Length + 1));
newPccStream.WriteString(name + "\0", (uint)(name.Length + 1) * 2, Encoding.Unicode);
}
DebugOutput.PrintLn("writing imports list...");
//writing import infos
ImportOffset = (int)newPccStream.Position;
ImportCount = Imports.Count;
foreach (ME3ImportEntry import in Imports)
newPccStream.Write(import.data, 0, import.data.Length);
//updating general export infos
ExportOffset = (int)newPccStream.Position;
ExportCount = Exports.Count;
expInfoEndOffset = ExportOffset + Exports.Sum(export => export.info.Length);
expDataBegOffset = expInfoEndOffset;
// WV code stuff...
DebugOutput.PrintLn("writing export data...");
int counter = 0;
int breaker = Exports.Count / 100;
if (breaker == 0)
breaker = 1;
//updating general export infos
ExportOffset = (int)newPccStream.Position;
ExportCount = Exports.Count;
expInfoEndOffset = ExportOffset + Exports.Sum(export => export.info.Length);
if (expDataBegOffset < expInfoEndOffset)
expDataBegOffset = expInfoEndOffset;
//writing export data
/*newPccStream.Seek(expDataBegOffset, SeekOrigin.Begin);
foreach (ExportEntry export in Exports)
{
//updating info values
export.DataSize = export.Data.Length;
export.DataOffset = (int)newPccStream.Position;
//writing data
newPccStream.Write(export.Data, 0, export.Data.Length);
}*/
//writing export data
List<ME3ExportEntry> unchangedExports = Exports.Where(export => !export.hasChanged || (export.hasChanged && export.Data.Length <= export.DataSize)).ToList();
List<ME3ExportEntry> changedExports = Exports.Where(export => export.hasChanged && export.Data.Length > export.DataSize).ToList();
foreach (ME3ExportEntry export in unchangedExports)
{
newPccStream.Seek(export.DataOffset, SeekOrigin.Begin);
//updating info values
export.DataSize = export.Data.Length;
//export.DataOffset = (int)newPccStream.Position;
export.DataOffset = (uint)newPccStream.Position;
//writing data
newPccStream.Write(export.Data, 0, export.Data.Length);
}
ME3ExportEntry lastExport = unchangedExports.Find(export => export.DataOffset == unchangedExports.Max(maxExport => maxExport.DataOffset));
int lastDataOffset = (int)(lastExport.DataOffset + lastExport.DataSize);
//.........这里部分代码省略.........
示例12: Encode
public override void Encode(Stream stream)
{
using (var body = new MemoryStream())
{
body.WriteString(TopicName);
//body.WriteShort(MessageIdentifier);
body.Write(Payload, 0, Payload.Length);
FixedHeader.RemaingLength = (int)body.Length;
FixedHeader.WriteTo(stream);
body.WriteTo(stream);
}
}
示例13: SezializeSaveFile
private void SezializeSaveFile(Stream stream, SaveFile save)
{
var saveDataStream = new MemoryStream();
var nameTableStream = new MemoryStream();
var newSave = new SaveFile
{
Header =
{
UnkInt1 = save.Header.UnkInt1
},
SaveDataHeader =
{
HeaderSize = save.SaveDataHeader.HeaderSize,
UnkInt2 = save.SaveDataHeader.UnkInt2
}
};
newSave.SaveData = FillSaveData();
saveDataStream.SerializeObject(newSave.SaveData, newSave.NameTable.Names);
nameTableStream.WriteInt32(newSave.NameTable.Names.Count);
for (var i = 0; i < newSave.NameTable.Names.Count; i++)
{
nameTableStream.WriteString(newSave.NameTable.Names[i]);
}
newSave.SaveDataHeader.SaveDataSize = (int) saveDataStream.Length + 12; // 12 = SaveDataHeader size
newSave.Header.FileSize = (int) saveDataStream.Length + 12 + (int) nameTableStream.Length;
stream.Seek(0, SeekOrigin.Begin);
// File header
stream.WriteInt32(newSave.Header.UnkInt1);
stream.WriteInt32(newSave.Header.FileSize);
// Save data header
stream.WriteInt32(newSave.SaveDataHeader.HeaderSize);
stream.WriteInt32(newSave.SaveDataHeader.SaveDataSize);
stream.WriteInt32(newSave.SaveDataHeader.UnkInt2);
// Save data
saveDataStream.WriteTo(stream);
//stream.WriteInt32(255);
// Name table
nameTableStream.WriteTo(stream);
}
示例14: GetCommandText
/// <summary>
/// Process this.commandText, trimming each distinct command and substituting paramater
/// tokens.
/// </summary>
/// <param name="prepare"></param>
/// <returns>UTF8 encoded command ready to be sent to the backend.</returns>
private byte[] GetCommandText(bool prepare)
{
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetCommandText");
MemoryStream commandBuilder = new MemoryStream();
if (commandType == CommandType.TableDirect)
{
foreach (var table in commandText.Split(';'))
{
if (table.Trim().Length == 0)
{
continue;
}
commandBuilder
.WriteString("SELECT * FROM ")
.WriteString(table)
.WriteString(";");
}
}
else if (commandType == CommandType.StoredProcedure)
{
if (!prepare && !functionChecksDone)
{
functionNeedsColumnListDefinition = Parameters.Count != 0 && CheckFunctionNeedsColumnDefinitionList();
functionChecksDone = true;
}
commandBuilder.WriteString("SELECT * FROM ");
if (commandText.TrimEnd().EndsWith(")"))
{
if (!AppendCommandReplacingParameterValues(commandBuilder, commandText, prepare, false))
{
throw new NpgsqlException("Multiple queries not supported for stored procedures");
}
}
else
{
commandBuilder
.WriteString(commandText)
.WriteBytes((byte)ASCIIBytes.ParenLeft);
if (prepare)
{
AppendParameterPlaceHolders(commandBuilder);
}
else
{
AppendParameterValues(commandBuilder);
}
commandBuilder.WriteBytes((byte)ASCIIBytes.ParenRight);
}
if (!prepare && functionNeedsColumnListDefinition)
{
AddFunctionColumnListSupport(commandBuilder);
}
}
else
{
if (!AppendCommandReplacingParameterValues(commandBuilder, commandText, prepare, !prepare))
{
throw new NpgsqlException("Multiple queries not supported for prepared statements");
}
}
return commandBuilder.ToArray();
}
示例15: GetExecuteCommandText
byte[] GetExecuteCommandText()
{
var result = new MemoryStream();
result.WriteString("EXECUTE {0}", _planName);
if (_parameters.Count != 0)
{
result.WriteByte((byte)ASCIIBytes.ParenLeft);
for (var i = 0; i < Parameters.Count; i++)
{
var p = Parameters[i];
if (i > 0)
{
result.WriteByte((byte)ASCIIBytes.Comma);
}
// Add parentheses wrapping parameter value before the type cast to avoid problems with Int16.MinValue, Int32.MinValue and Int64.MinValue
// See bug #1010543
result.WriteByte((byte)ASCIIBytes.ParenLeft);
byte[] serialization;
serialization = p.TypeInfo.ConvertToBackend(p.NpgsqlValue, false, Connector.NativeToBackendTypeConverterOptions);
result.WriteBytes(serialization);
result.WriteByte((byte)ASCIIBytes.ParenRight);
if (p.UseCast)
{
result.WriteString(string.Format("::{0}", p.TypeInfo.GetCastName(p.Size)));
}
}
result.WriteByte((byte)ASCIIBytes.ParenRight);
}
return result.ToArray();
}