本文整理汇总了C#中System.IO.MemoryStream.WriteBytes方法的典型用法代码示例。如果您正苦于以下问题:C# MemoryStream.WriteBytes方法的具体用法?C# MemoryStream.WriteBytes怎么用?C# MemoryStream.WriteBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.MemoryStream
的用法示例。
在下文中一共展示了MemoryStream.WriteBytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static void Main(string[] args)
{
// Notes:
// 1. All referenced assemblies shall
// define [assembly:Obfuscation(feature = "script")]
// 2. Turn off "optimize code" option in release build
// 3. All used .net APIs must be defined by ScriptCoreLibJava
// 4. Generics are not supported.
// 5. Check post build event
// 6. Build in releas build configuration for java version
Console.WriteLine("This console application can run at .net and java virtual machine!");
var port = 18080;
Console.WriteLine("http://127.0.0.1:" + port);
port.ToListener(
s =>
{
var request = new byte[0x1000];
var requestc = s.Read(request, 0, request.Length);
// StreamReader would be nice huh
// ScriptCoreLibJava does not have it yet
Console.WriteLine("client request " + requestc);
// if we get the file name wrong we get a bad error message to stdout
var data = File.ReadAllBytes("HTMLPage1.htm");
Console.WriteLine("data length " + data.Length);
var m = new MemoryStream();
m.WriteLineASCII("HTTP/1.1 200 OK");
m.WriteLineASCII("Content-Type: text/html; charset=utf-8");
m.WriteLineASCII("Content-Length: " + data.Length);
m.WriteLineASCII("Connection: close");
m.WriteLineASCII("");
m.WriteLineASCII("");
Console.WriteLine("headers written");
m.WriteBytes(data);
Console.WriteLine("data written");
var Text = Encoding.ASCII.GetString(m.ToArray());
m.WriteTo(s);
Console.WriteLine("flush");
s.Flush();
s.Close();
}
);
Console.WriteLine("press enter to exit!");
Console.ReadLine();
}
示例2: SerializeCoinbase
public static byte[] SerializeCoinbase(IJob job, UInt32 extraNonce1, UInt32 extraNonce2)
{
var extraNonce1Buffer = BitConverter.GetBytes(extraNonce1.BigEndian());
var extraNonce2Buffer = BitConverter.GetBytes(extraNonce2.BigEndian());
byte[] result;
using (var stream = new MemoryStream())
{
stream.WriteBytes(job.CoinbaseInitial.HexToByteArray());
stream.WriteBytes(extraNonce1Buffer);
stream.WriteBytes(extraNonce2Buffer);
stream.WriteBytes(job.CoinbaseFinal.HexToByteArray());
result = stream.ToArray();
}
return result;
}
示例3: CompressChunk
public byte[] CompressChunk(Chunk chunk)
{
int numBlocks = (chunk.Uncompressed.Length + maxBlockSize - 1) / maxBlockSize;
if (numBlocks > 8)
throw new FormatException("Maximum block number exceeded");
ChunkHeader head = new ChunkHeader();
head.magic = -1641380927;
head.blocksize = maxBlockSize;
head.uncompressedsize = chunk.Uncompressed.Length;
int pos = 0;
MemoryStream mem = new MemoryStream();
List<Block> blockList = new List<Block>();
int startData = 16 + 8 * numBlocks;
mem.Seek(startData, SeekOrigin.Begin);
for (int i = 0; i < numBlocks; i++)
{
Block block = new Block();
byte[] result, temp;
if (i != numBlocks - 1)
{
block.uncompressedsize = maxBlockSize;
temp = new byte[maxBlockSize];
}
else
{
block.uncompressedsize = head.uncompressedsize - pos;
temp = new byte[block.uncompressedsize];
}
Buffer.BlockCopy(chunk.Uncompressed, pos, temp, 0, temp.Length);
result = LZO1X.Compress(temp);
if (result.Length == 0)
throw new Exception("LZO compression error!");
block.compressedsize = result.Length;
mem.WriteBytes(result);
blockList.Add(block);
pos += maxBlockSize;
}
head.compressedsize = (int)mem.Length;
mem.Seek(0, SeekOrigin.Begin);
mem.WriteValueS32(head.magic);
mem.WriteValueS32(head.blocksize);
mem.WriteValueS32(head.compressedsize);
mem.WriteValueS32(head.uncompressedsize);
foreach (Block block in blockList)
{
mem.WriteValueS32(block.compressedsize);
mem.WriteValueS32(block.uncompressedsize);
}
return mem.ToArray();
}
示例4: SignatureScript
public SignatureScript(int blockHeight, string coinbaseAuxFlags, Int64 unixTime, byte extraNoncePlaceholder, string signature)
{
// cook input signature script.
// The txin's prevout script is an arbitrary byte array (it doesn't have to be a valid script, though this is commonly
// done anyway) of 2 to 100 bytes. It has to start with a correct push of the block height (see BIP34).
var serializedBlockHeight = Serializers.SerializeNumber(blockHeight);
var serializedUnixTime = TransactionUtils.GetSerializedUnixDateTime(unixTime);
using (var stream = new MemoryStream())
{
stream.WriteBytes(serializedBlockHeight);
stream.WriteBytes(coinbaseAuxFlags.HexToByteArray());
stream.WriteBytes(serializedUnixTime);
stream.WriteByte(extraNoncePlaceholder);
Initial = stream.ToArray();
}
Final = Serializers.SerializeString(signature);
}
示例5: ToIDATChunk
public IDATChunk ToIDATChunk()
{
uint newCrc;
using (var msCrc = new MemoryStream())
{
msCrc.WriteBytes(new[] {(byte) 'I', (byte) 'D', (byte) 'A', (byte) 'T'});
msCrc.WriteBytes(FrameData);
newCrc = CrcHelper.Calculate(msCrc.ToArray());
}
using (var ms = new MemoryStream())
{
ms.WriteUInt32(Helper.ConvertEndian(Length - 4));
ms.WriteBytes(new[] {(byte) 'I', (byte) 'D', (byte) 'A', (byte) 'T'});
ms.WriteBytes(FrameData);
ms.WriteUInt32(Helper.ConvertEndian(newCrc));
ms.Position = 0;
return new IDATChunk(ms);
}
}
示例6: SerializeBlock
/// <summary>
/// Block data structure.
/// </summary>
/// <remarks>
/// https://en.bitcoin.it/wiki/Protocol_specification#block
/// </remarks>
/// <param name="job"></param>
/// <param name="header"></param>
/// <param name="coinbase"></param>
/// <returns></returns>
public static byte[] SerializeBlock(IJob job, byte[] header, byte[] coinbase)
{
byte[] result;
using (var stream = new MemoryStream())
{
stream.WriteBytes(header);
stream.WriteBytes(VarInt((UInt32)job.BlockTemplate.Transactions.Length + 1));
stream.WriteBytes(coinbase);
foreach (var transaction in job.BlockTemplate.Transactions)
{
stream.WriteBytes(transaction.Data.HexToByteArray());
}
// need to implement POS support too.
result = stream.ToArray();
}
return result;
}
示例7: GetStream
/// <summary>
/// Gets the frame as PNG FileStream.
/// </summary>
public MemoryStream GetStream()
{
var ihdrChunk = new IHDRChunk(IHDRChunk);
if (fcTLChunk != null)
{
// Fix frame size with fcTL data.
ihdrChunk.ModifyChunkData(0, Helper.ConvertEndian(fcTLChunk.Width));
ihdrChunk.ModifyChunkData(4, Helper.ConvertEndian(fcTLChunk.Height));
}
// Write image data
using (var ms = new MemoryStream())
{
ms.WriteBytes(Signature);
ms.WriteBytes(ihdrChunk.RawData);
otherChunks.ForEach(o => ms.WriteBytes(o.RawData));
idatChunks.ForEach(i => ms.WriteBytes(i.RawData));
ms.WriteBytes(IENDChunk.RawData);
ms.Position = 0;
return ms;
}
}
示例8: NpgsqlExecute
public NpgsqlExecute(String portalName, Int32 maxRows)
{
int messageLength = 4 + portalName.Length + 1 + 4;
_messageData = new byte[messageLength + 1];
MemoryStream messageBuilder = new MemoryStream(_messageData);
_portalName = portalName;
messageBuilder
.WriteBytes((byte)FrontEndMessageCode.Execute)
.WriteInt32(messageLength)
.WriteStringNullTerminated(_portalName)
.WriteInt32(maxRows);
}
示例9: SerializeBlock
/// <summary>
/// Block data structure.
/// </summary>
/// <remarks>
/// https://en.bitcoin.it/wiki/Protocol_specification#block
/// </remarks>
/// <param name="job"></param>
/// <param name="header"></param>
/// <param name="coinbase"></param>
/// <param name="isProofOfStakeHybrid">Are we serializing a proof-of-stake coin block</param>
/// <returns></returns>
public static byte[] SerializeBlock(IJob job, byte[] header, byte[] coinbase, bool isProofOfStakeHybrid = false)
{
byte[] result;
using (var stream = new MemoryStream())
{
stream.WriteBytes(header);
stream.WriteBytes(VarInt((UInt32)job.BlockTemplate.Transactions.Length + 1));
stream.WriteBytes(coinbase);
foreach (var transaction in job.BlockTemplate.Transactions)
{
stream.WriteBytes(transaction.Data.HexToByteArray());
}
if (isProofOfStakeHybrid) // check if we are serializing a block for proof-of-stake coin.
stream.WriteByte(0); // proof-of-stake coins require a zero byte appended to block which the daemon replaces with signature.
result = stream.ToArray();
}
return result;
}
示例10: NpgsqlQuery
public NpgsqlQuery(byte[] command)
{
// Message length: Inr32 + command length + null terminator.
int len = 4 + command.Length + 1;
// Length + command code ('Q').
pgCommandBytes = new byte[1 + len];
MemoryStream commandWriter = new MemoryStream(pgCommandBytes);
commandWriter
.WriteBytes((byte)FrontEndMessageCode.Query)
.WriteInt32(len)
.WriteBytesNullTerminated(command);
commandBytes = command;
}
示例11: Split
public static void Split(Stream InputStream, byte[] Separator, Action<byte[]> ChunkHandler)
{
byte[] Buffer = new byte[4096];
byte[] TempDoubleBuffer = new byte[Separator.Length * 2];
MemoryStream Chunk = new MemoryStream();
int StartIndex = Separator.Length;
int SkipChunkStart = 0;
while (!InputStream.Eof()) {
Array.Copy(TempDoubleBuffer, Separator.Length, TempDoubleBuffer, 0, Separator.Length);
int TempDoubleBufferReaded = InputStream.Read(TempDoubleBuffer, Separator.Length, Separator.Length);
int EndIndex = Separator.Length + TempDoubleBufferReaded;
Chunk.Write(TempDoubleBuffer, Separator.Length, TempDoubleBufferReaded);
int FoundIndex = FindSequence(TempDoubleBuffer, Separator, StartIndex, EndIndex);
if (FoundIndex != -1)
{
int BytesToRemoveFromChunk = EndIndex - FoundIndex;
int RealChunkSize = (int)(Chunk.Length - BytesToRemoveFromChunk);
MemoryStream NewChunk = new MemoryStream();
NewChunk.WriteBytes(Chunk.ReadChunk(RealChunkSize, BytesToRemoveFromChunk));
ChunkHandler(Chunk.ReadChunk(SkipChunkStart, RealChunkSize - SkipChunkStart));
SkipChunkStart = Separator.Length;
Chunk = NewChunk;
}
StartIndex = 0;
}
if (Chunk.Length > 0)
{
ChunkHandler(Chunk.ReadChunk(SkipChunkStart, (int)Chunk.Length - SkipChunkStart));
}
}
示例12: GetBuffer
public byte[] GetBuffer()
{
byte[] result;
using (var stream = new MemoryStream())
{
stream.WriteBytes(Serializers.VarInt((UInt32)List.Count).ToArray());
foreach (var transaction in List)
{
stream.WriteValueU64(transaction.Value);
stream.WriteBytes(transaction.PublicKeyScriptLenght);
stream.WriteBytes(transaction.PublicKeyScript);
}
result = stream.ToArray();
}
return result;
}
示例13: CoinAddressToScript
/// <summary>
/// For POW coins - used to format wallet address for use in generation transaction's output
/// </summary>
/// <param name="address"></param>
/// <example>
/// nodejs: https://github.com/zone117x/node-stratum-pool/blob/dfad9e58c661174894d4ab625455bb5b7428881c/lib/util.js#L264
/// nodejs: https://codio.com/raistlinthewiz/bitcoin-coinbase-serializer-wallet-address-to-script
/// </example>
/// <returns></returns>
public static byte[] CoinAddressToScript(string address)
{
byte[] decoded;
try
{
decoded = Base58.Decode(address);
}
catch (AddressFormatException)
{
Log.Error("Base58 decode failed for address {0:l}", address);
return null;
}
if (decoded.Length != 25)
{
Log.Error("invalid address length for {0:l}", address);
return null;
}
var pubkey = decoded.Slice(1, -4);
byte[] result;
using (var stream = new MemoryStream())
{
stream.WriteValueU8(0x76);
stream.WriteValueU8(0xa9);
stream.WriteValueU8(0x14);
stream.WriteBytes(pubkey);
stream.WriteValueU8(0x88);
stream.WriteValueU8(0xac);
result = stream.ToArray();
}
return result;
}
示例14: processFragments
private bool processFragments(WsFrame first)
{
using (var concatenated = new MemoryStream ())
{
concatenated.WriteBytes (first.PayloadData.ApplicationData);
if (!concatenateFragmentsInto (concatenated))
return false;
byte [] data;
if (_compression != CompressionMethod.NONE)
{
data = concatenated.DecompressToArray (_compression);
}
else
{
concatenated.Close ();
data = concatenated.ToArray ();
}
OnMessage.Emit (this, new MessageEventArgs (first.Opcode, data));
return true;
}
}
示例15: Create
public void Create()
{
// create the first part.
using (var stream = new MemoryStream())
{
stream.WriteValueU32(Version.LittleEndian()); // write version
// for proof-of-stake coins we need here timestamp - https://github.com/zone117x/node-stratum-pool/blob/b24151729d77e0439e092fe3a1cdbba71ca5d12e/lib/transactions.js#L210
// write transaction input.
stream.WriteBytes(Serializers.VarInt(InputsCount));
stream.WriteBytes(Inputs.First().PreviousOutput.Hash.Bytes);
stream.WriteValueU32(Inputs.First().PreviousOutput.Index.LittleEndian());
// write signature script lenght
var signatureScriptLenght = (UInt32)(Inputs.First().SignatureScript.Initial.Length + ExtraNonce.ExtraNoncePlaceholder.Length + Inputs.First().SignatureScript.Final.Length);
stream.WriteBytes(Serializers.VarInt(signatureScriptLenght).ToArray());
stream.WriteBytes(Inputs.First().SignatureScript.Initial);
Initial = stream.ToArray();
}
/* The generation transaction must be split at the extranonce (which located in the transaction input
scriptSig). Miners send us unique extranonces that we use to join the two parts in attempt to create
a valid share and/or block. */
// create the second part.
using (var stream = new MemoryStream())
{
// transaction input
stream.WriteBytes(Inputs.First().SignatureScript.Final);
stream.WriteValueU32(Inputs.First().Sequence);
// transaction inputs end here.
// transaction output
var outputBuffer = Outputs.GetBuffer();
stream.WriteBytes(outputBuffer);
// transaction output ends here.
stream.WriteValueU32(LockTime.LittleEndian());
if (SupportTxMessages)
stream.WriteBytes(Message);
Final = stream.ToArray();
}
}