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); }
public byte[] ToByteArray() { try { return(MainStream.ToArray()); } catch (Exception) { } return(null); }
public string RecvUntil(string stop) { string sb = ""; while (true) { sb += Convert.ToChar(MainStream.ReadByte()); if (sb.EndsWith(stop)) { break; } } return(sb); }
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()); }
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); }
public void Send(string str) { byte[] buf = Encoding.UTF8.GetBytes(str); MainStream.Write(buf, 0, buf.Length); }