Write() публичный Метод

public Write ( byte bytes ) : void
bytes byte
Результат void
Пример #1
0
 public int WriteGamePackHeader(ByteStream readStream, GamePackHeader header, bool isHostToNet = true)
 {
     readStream.Write((int)header.type);
     readStream.Write(header.dataSize);
     readStream.WriteAlignBytes();
     return(readStream.GetNumberOfWriteBytes());
 }
Пример #2
0
            public byte[] ToArray()
            {
                using (ByteStream bs = new ByteStream())
                {
                    bs.Write('[');
                    bs.Write(RawCode.GetRawCode());
                    bs.Write("]\r\n", false);
                    foreach (var item in this)
                    {
                        bs.Write(item.Key, false);
                        bs.Write('=');
                        switch (item.Value.GetType().ToString())
                        {
                        case "System.Int32":
                            bs.Write(item.Value.ToString(), false);
                            break;

                        case "System.String":
                            bs.Write(item.Value as string, false);
                            break;

                        case "System.byte[]":
                            bs.Write(item.Value as byte[]);
                            break;

                        case "System.Int32[]":
                            bs.Write((item.Value as int[]).GetRawCodeList());
                            break;
                        }
                        bs.Write("\r\n", false);
                    }
                    return(bs.ToArray());
                }
            }
Пример #3
0
 public byte[] ToArray()
 {
     using (ByteStream bs = new ByteStream())
     {
         bs.Write(Length);
         bs.Write(value);
         return(bs.ToArray());
     }
 }
Пример #4
0
 public byte[] ToArray()
 {
     using (ByteStream bs = new ByteStream())
     {
         bs.Write(Header);
         bs.Write(VersionChunk);
         bs.Write(ModelChunk);
         return(bs.ToArray());
     }
 }
Пример #5
0
 public new byte[] ToArray()
 {
     using (ByteStream bs = new ByteStream())
     {
         bs.Write(0);
         bs.Write(Count);
         foreach (var item in this)
         {
             bs.Write(item);
         }
         return(bs.ToArray());
     }
 }
Пример #6
0
 public override byte[] ToArray(bool TryHashTableNormalize = true)
 {
     using (ByteStream ms = new ByteStream())
     {
         Header.UpdateSize();
         byte[] buffer;
         ms.Write(buffer = MapHeader.ToArray());
         ms.SetLength((int)Math.Ceiling(buffer.Length / (double)0x200) * 0x200);
         ms.Seek(0, SeekOrigin.End);
         ms.Write(buffer = base.ToArray(TryHashTableNormalize));
         return(ms.ToArray());
     }
 }
Пример #7
0
 public new byte[] ToArray()
 {
     using (ByteStream bs = new ByteStream())
     {
         bs.Write(Version);
         bs.Write(Count);
         foreach (var item in this)
         {
             bs.WriteByte((byte)(item.UseCustomPath ? 0xD : 0x8));
             bs.Write(item.Path);
         }
         return(bs.ToArray());
     }
 }
        private LedgerMerkleRootHash GetHash()
        {
            using (var stream = new ByteStream())
            {
                stream.Write(accounts.Hash);
                stream.Write(declarations.Hash);

                var message = stream.GetBytes();

                var hasher = HashFactory.Crypto.SHA3.CreateKeccak256();
                var hash   = hasher.ComputeBytes(message).GetBytes();
                return(new LedgerMerkleRootHash(hash));
            }
        }
Пример #9
0
 public static byte[] GetRawCodeList(this int[] hashs)
 {
     using (ByteStream bs = new ByteStream())
     {
         for (int i = 0; i < hashs.Length; i++)
         {
             if (i != 0)
             {
                 bs.Write(',');
             }
             bs.Write(hashs[i].GetRawCode());
         }
         return(bs.ToArray());
     }
 }
 private byte[] GetRaw(Account account)
 {
     using (var stream = new ByteStream())
     {
         stream.Write(account);
         return(stream.GetBytes());
     }
 }
Пример #11
0
 public static byte[] ZipSignedLedger(SignedLedger signedLedger)
 {
     using (var stream = new ByteStream())
     {
         stream.Write(signedLedger);
         return(Zipper.Zip(stream.GetBytes()));
     }
 }
Пример #12
0
 private byte[] GetLedgerStateChangeBytes(LedgerStateChange change)
 {
     using (var stream = new ByteStream())
     {
         stream.Write(change);
         return(stream.GetBytes());
     }
 }
Пример #13
0
 public byte[] ToBytes()
 {
     using (var stream = new ByteStream())
     {
         stream.Write(this);
         return(stream.GetBytes());
     }
 }
Пример #14
0
 private byte[] GetSignedLedgerBytes(SignedLedger signedLedger)
 {
     using (var stream = new ByteStream())
     {
         stream.Write(signedLedger);
         return(Zipper.Zip(stream.GetBytes()));
     }
 }
Пример #15
0
 public virtual byte[] ToArray(bool TryHashTableNormalize = false)
 {
     using (ByteStream ms = new ByteStream())
     {
         if (TryHashTableNormalize)
         {
             HashTable.Fill(BlockTable.Count.BitUpper());
         }
         Header.HashTableSize  = (uint)HashTable.Count;
         Header.BlockTableSize = (uint)BlockTable.Count;
         ms.WriteEmpty(0x20);
         foreach (var item in Files)
         {
             if ((item.Block.Flags & MPQ_FILE_FIX_KEY) != 0)
             {
                 item.Decrypt();
                 item.Block.FilePos = (int)ms.Length;
                 item.Encrypt(true);
             }
             else
             {
                 item.Block.FilePos = (int)ms.Length;
             }
             ms.Write(item.RawFile);
         }
         Header.HashTablePos = (int)ms.Length;
         byte[] Buffer = HashTable.ToArray();
         if (Buffer != null)
         {
             Buffer.EncryptBlock(MPQ_HASH_KEY);
             ms.Write(Buffer);
         }
         Header.BlockTablePos = (int)ms.Length;
         Buffer = BlockTable.ToArray();
         if (Buffer != null)
         {
             Buffer.EncryptBlock(MPQ_BLOCK_KEY);
             ms.Write(Buffer);
         }
         Header.ArchiveSize = (uint)ms.Length;
         ms.Seek(0, SeekOrigin.Begin);
         ms.Write(Header.ToArray());
         return(ms.ToArray());
     }
 }
Пример #16
0
    public static void Decode(ByteStream data, Bitmap dest)
    {
        PNGHeader header = data.Struct <PNGHeader>(true);

        Debug.Assert(header.Signature == PNGSignature, "Specified file was not a PNG file.");

        ByteStream idatStream = new ByteStream();

        while (true)
        {
            PNGChunkHeader chunkHeader = data.Struct <PNGChunkHeader>(true);
            string         chunkType   = Encoding.ASCII.GetString(chunkHeader.Type, 4);
            byte[]         chunkBytes  = data.Read(chunkHeader.Length);
            ByteStream     chunkData   = new ByteStream(chunkBytes);
            PNGChunkFooter chunkFooter = data.Struct <PNGChunkFooter>(true);

            Debug.Assert(Crc32(chunkHeader.ToBytes().Concat(chunkBytes).ToArray(), 4) == chunkFooter.CRC, chunkType + " chunk's CRC mismatched!");

            switch (chunkType)
            {
            case "IHDR":
                PNGIHDR ihdr = chunkData.Struct <PNGIHDR>(true);
                Debug.Assert(ihdr.BitDepth == 8 && ihdr.ColorType == 6 && ihdr.CompressionMethod == 0 &&
                             ihdr.FilterMethod == 0 && ihdr.InterlaceMethod == 0, "The specified PNG file uses an unsupported format.");
                dest.Width  = ihdr.Width;
                dest.Height = ihdr.Height;
                dest.Pixels = new byte[ihdr.Width * ihdr.Height * 4];
                break;

            case "IDAT":
                idatStream.Write(chunkData.Read(chunkHeader.Length));
                break;

            case "IEND":
                idatStream.Seek(0, SeekOrigin.Begin);
                PNGIDATHeader idatHeader = idatStream.Struct <PNGIDATHeader>(true);
                byte[]        idatData;
                PNGIDATFooter idatFooter;
                using (MemoryStream target = new MemoryStream())
                    using (DeflateStream decompressionStream = new DeflateStream(idatStream, CompressionMode.Decompress)) {
                        decompressionStream.CopyTo(target);
                        idatData = target.ToArray();
                        idatStream.Seek(-4);
                        idatFooter = idatStream.Struct <PNGIDATFooter>(true);
                    }

                Debug.Assert(idatFooter.CheckValue == Alder32(idatData), "IDAT chunk compression check value mismatch!");
                int scanlineSize = dest.Width * 4;
                for (int scanline = 0; scanline < dest.Height; scanline++)
                {
                    int offset = scanline * scanlineSize;
                    Array.Copy(idatData, offset + scanline + 1, dest.Pixels, offset, scanlineSize);
                }
                return;
            }
        }
    }
Пример #17
0
 public static void SaveLedger(SignedLedger signed, string path)
 {
     using (var stream = new ByteStream())
     {
         stream.Write(signed);
         var bytes = stream.GetBytes();
         File.WriteAllBytes(path, bytes);
     }
 }
 public override void WriteObject(object _Object, ByteStream _TargetStream)
 {
     string[] strings = (string[])_Object;
     Converter.Converters[typeof(ushort)].WriteObject(strings.Length, _TargetStream);
     foreach (string s in strings)
     {
         Converter.Write(s, _TargetStream);
     }
     foreach (char c in (string)_Object) _TargetStream.Write((byte)c);
 }
Пример #19
0
        void SetDataLengthToPackHead(byte[] pack, int dataSize)
        {
            int   headerType = 0;
            short size       = (short)dataSize;

            ByteStream readStream = new ByteStream(pack);

            readStream.Read(out headerType);
            readStream.Write(size);
        }
Пример #20
0
        private VendingMachineHash ComputeHash()
        {
            using (var stream = new ByteStream())
            {
                stream.Write(this);
                var message = stream.GetBytes();

                var hasher = HashFactory.Crypto.SHA3.CreateKeccak256();
                return(new VendingMachineHash(hasher.ComputeBytes(message).GetBytes()));
            }
        }
Пример #21
0
    /// <summary>
    /// 用protobuf将结构转二进制
    /// </summary>
    /// <param name="p"></param>
    /// <returns></returns>
    public byte[] Write(SendPacket p)
    {
        MemoryStream stream = new MemoryStream();

        try
        {
            Serializer.Serialize(stream, p.protoObj);
        }
        catch (Exception)
        {
            Debug.LogError("protobuf序列化失败");
        }


        //msg len占2位 uid占8位
        // len+cmd+[uid]+body
        byte[] msgbody = stream.ToArray();
        int    msgLen  = 0;

        if (p.hasUid)
        {
            msgLen = msgbody.Length + 2 + 8;// body+cmd+uid
            ByteStream writer = new ByteStream(msgLen + 2);

            writer.Write((ushort)msgLen);
            writer.Write((ushort)p.cmd);
            writer.Write((ulong)p.uid);
            writer.Write(msgbody);
            return(writer.GetUsedBytes());
        }
        else
        {
            msgLen = msgbody.Length + 2; // body+cmd
            ByteStream writer = new ByteStream(msgLen + 2);

            writer.Write((ushort)msgLen);
            writer.Write((ushort)p.cmd);
            writer.Write(msgbody);
            return(writer.GetUsedBytes());
        }
    }
Пример #22
0
        public LedgerHash GetHash(Ledger ledger)
        {
            using (var stream = new ByteStream())
            {
                stream.Write(ledger);
                var message = stream.GetBytes();

                var hasher = HashFactory.Crypto.SHA3.CreateKeccak256();
                var hash   = hasher.ComputeBytes(message).GetBytes();
                return(new LedgerHash(hash));
            }
        }
Пример #23
0
    /// <summary>
    /// 登录成功需要保存session和uid
    /// 之后的http请求用
    /// </summary>
    /// <param name="key"></param>
    /// <param name="uid"></param>
    /// <returns></returns>
    public static bool SetSession(byte[] key, ulong uid)
    {
        if (key.Length != 32)
        {
            return(false);
        }

        byte[] session = new byte[16];
        byte[] enKey   = new byte[16];

        Buffer.BlockCopy(key, 0, session, 0, 16);
        Buffer.BlockCopy(key, 16, enKey, 0, 16);

        var buf = new ByteStream(24);

        buf.Write(session);
        buf.Write(uid);
        sessionKey   = buf.GetUsedBytes();
        sessionEnKey = enKey;
        return(true);
    }
Пример #24
0
 /// <summary>
 /// <see cref="W3MHeader"/>를 바이트 배열로 반환합니다.
 /// </summary>
 /// <returns><see cref="W3MHeader"/>의 바이트 배열입니다.</returns>
 public byte[] ToArray()
 {
     using (ByteStream bs = new ByteStream())
     {
         bs.Write(W3M_HEADER_SIGNATURE);
         bs.WriteEmpty(4);
         bs.Write(Name);
         bs.Write(Flags);
         bs.Write(Players);
         return(bs.ToArray());
     }
     //List<byte> buffer = new List<byte>();
     //buffer.AddRange(BitConverter.GetBytes(W3M_HEADER_SIGNATURE));
     //for (int i = 0; i < 4; i++)
     //    buffer.Add(0x00);
     //buffer.AddRange(Encoding.UTF8.GetBytes(Name));
     //buffer.Add(0x00);
     //buffer.AddRange(BitConverter.GetBytes(Flags));
     //buffer.AddRange(BitConverter.GetBytes(Players));
     //return buffer.ToArray();
 }
Пример #25
0
        // used only for merkle tree
        internal virtual AccountHash GetHash(Account account)
        {
            using (var stream = new ByteStream())
            {
                stream.Write(account);

                var message = stream.GetBytes();

                var hasher = HashFactory.Crypto.SHA3.CreateKeccak256();
                var hash   = hasher.ComputeBytes(message).GetBytes();
                return(new AccountHash(hash));
            }
        }
Пример #26
0
            public byte[] ToArray()
            {
                switch (value)
                {
                case enums.None: return(new byte[1]);

                default:
                    using (ByteStream bs = new ByteStream())
                    {
                        bs.Write(value.ToString());
                        return(bs.ToArray());
                    }
                }
            }
Пример #27
0
        public void write(ByteStream queue)
        {
            queue.WriteByte(version);
            queue.WriteByte(control.Value);

            if (control.IsDestinationSpecific)
            {
                queue.pushU2B(destinationNetworkAddress);
                queue.WriteByte(destinationMacLyerAddressLength);
                if (destinationAddress != null)
                {
                    queue.Write(destinationAddress);
                }
            }

            if (control.IsSourceSpecific)
            {
                queue.pushU2B(sourceNetworkAddress);
                queue.WriteByte(sourceMacLyerAddressLength);
                queue.Write(sourceAddress);
            }

            if (control.IsDestinationSpecific)
            {
                queue.WriteByte(hopCount);
            }

            if (control.IsNetworkLayerMessage)
            {
                queue.WriteByte(messageType);
                if (messageType >= 80)
                {
                    queue.pushU2B(vendorId);
                }
            }
        }
Пример #28
0
        public byte[] ToArray()
        {
            using (ByteStream bs = new ByteStream())
            {
                bs.Write(Header);
                bs.Write(ChunkSize);

                bs.Write(Name, 80);
                bs.Write(AnimationFileName, 260);

                bs.Write(BoundsRadius);
                bs.Write(MinimumExtent);
                bs.Write(MaximumExtent);
                bs.Write(BlendTime);

                return(bs.ToArray());
            }
        }
Пример #29
0
 protected override void WriteImpl(ByteStream queue)
 {
     if (Value.Length == 0)
     {
         queue.WriteByte((byte)0);
     }
     else
     {
         int remainder = Value.Length % 8;
         if (remainder > 0)
         {
             remainder = 8 - remainder;
         }
         queue.WriteByte((byte)remainder);
         queue.Write(BACnetUtils.convertToBytes(Value));
     }
 }
Пример #30
0
        public void ZipTest()
        {
            using (CreateContext())
            {
                var signed = CreateSignedLedger();

                byte[] bytes;
                using (var stream = new ByteStream())
                {
                    stream.Write(signed);
                    bytes = stream.GetBytes();
                }

                var unzipped = Zipper.Unzip(Zipper.Zip(bytes));

                Debug.Assert(bytes.IsEqual(unzipped));
            }
        }
Пример #31
0
        public void sendAPDU(Address recipient, OctetString link, IAPDU apdu, bool broadcast)
        {
            ByteStream queue = new ByteStream();

            // BACnet virtual link layer detail

            // BACnet/IP
            queue.WriteByte(0x81);

            // Original-Unicast-NPDU, or Original-Broadcast-NPDU
            queue.WriteByte((byte)(broadcast ? 0xb : 0xa));

            // NPCI
            ByteStream apduStream = new ByteStream();

            writeNpci(apduStream, recipient, link, apdu);

            // APDU
            apdu.write(apduStream);

            // Length
            queue.WriteShort((ushort)(queue.Position + apduStream.Length + 2));

            // Combine the queues
            queue.Write(apduStream);

            IPEndPoint isa;

            if (recipient.IsGlobal)
            {
                isa = LocalBroadcastAddress.MacAddress.InetSocketAddress;
            }
            else if (link != null)
            {
                isa = link.InetSocketAddress;
            }
            else
            {
                isa = recipient.MacAddress.InetSocketAddress;
            }

            this.link.SendPacket(isa, queue.ReadToEnd());
        }
Пример #32
0
 public override void WriteObject(object _Object, ByteStream _TargetStream)
 {
     ushort obj = (ushort)_Object;
     _TargetStream.Write(BitConverter.GetBytes((ushort)_Object));
 }
 public override void WriteObject(object _Object, ByteStream _TargetStream)
 {
     byte[] obj = (byte[])_Object;
     Converter.Converters[typeof(ushort)].WriteObject((ushort)obj.Length, _TargetStream);
     _TargetStream.Write((byte[])_Object);
 }
Пример #34
0
 public override void WriteObject(object _Object, ByteStream _TargetStream)
 {
     string obj = (string)_Object;
     Converter.Converters[typeof(ushort)].WriteObject((ushort)obj.Length, _TargetStream);
     foreach (char c in (string)_Object) _TargetStream.Write((byte)c);
 }
 public override void WriteObject(object _Object, ByteStream _TargetStream)
 {
     ByteStream obj = (ByteStream)_Object;
     Converter.Converters[typeof(ushort)].WriteObject((ushort)obj.Length, _TargetStream);
     _TargetStream.Write(((ByteStream)_Object).CopyAll());
 }
Пример #36
0
 public override void WriteObject(object _Object, ByteStream _TargetStream)
 {
     _TargetStream.Write((byte)(((bool)_Object) ? 255 : 0));
 }
Пример #37
0
 public override void WriteObject(object _Object, ByteStream _TargetStream)
 {
     _TargetStream.Write((byte)_Object);
 }
Пример #38
0
 public override void WriteObject(object _Object, ByteStream _TargetStream)
 {
     _TargetStream.Write(BitConverter.GetBytes((int)_Object));
 }