Пример #1
0
        public bool AppendBlock(byte[] block, int blockNumber)
        {
            try
            {
                if (!File.Exists(this.Path) && blockNumber > 0)
                {
                    throw new FileNotFoundException();
                }

                if (blockNumber == 0)
                {
                    this.MainStream = new MemoryStream();
                    MainStream.Seek(0, SeekOrigin.Begin);
                    MainStream.Write(block, 0, block.Length);

                    return(true);
                }
                MainStream.Seek(blockNumber * MAX_PACKET_SIZE, SeekOrigin.Begin);
                MainStream.Write(block, 0, block.Length);

                return(true);
            }
            catch (UnauthorizedAccessException)
            {
                this.LastError = "Access denied";
            }
            catch (IOException)
            {
                this.LastError = "File not found";
            }

            return(false);
        }
Пример #2
0
 public byte[] ToByteArray()
 {
     try
     {
         return(MainStream.ToArray());
     }
     catch (Exception)
     {
     }
     return(null);
 }
Пример #3
0
        public string RecvUntil(string stop)
        {
            string sb = "";

            while (true)
            {
                sb += Convert.ToChar(MainStream.ReadByte());
                if (sb.EndsWith(stop))
                {
                    break;
                }
            }

            return(sb);
        }
Пример #4
0
        public string Recv(int length)
        {
            StringBuilder sb = new StringBuilder();
            int           i  = 0;

            while (true)
            {
                if (i == length)
                {
                    break;
                }
                sb.Append(Convert.ToChar(MainStream.ReadByte()));
                i++;
            }
            return(sb.ToString());
        }
Пример #5
0
 public bool DropFile()
 {
     try
     {
         File.WriteAllBytes(this.Path, ToByteArray());
         MainStream.Close();
         return(true);
     }
     catch (UnauthorizedAccessException)
     {
         this.LastError = "Access denied";
     }
     catch (IOException)
     {
         this.LastError = "File not found";
     }
     return(false);
 }
Пример #6
0
 public void Send(string str)
 {
     byte[] buf = Encoding.UTF8.GetBytes(str);
     MainStream.Write(buf, 0, buf.Length);
 }