Hướng dẫn dùng stream readbyte trong PHP
Skip to main content This browser is no longer supported. Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Stream.ReadByte Method
DefinitionIn this articleReads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.
ReturnsInt32The unsigned byte cast to an Int32, or -1 if at the end of the stream. ExceptionsThe stream does not support reading. Methods were called after the stream was closed. RemarksUse the CanRead property to determine whether the current instance supports reading. Attempts to manipulate the stream after the stream has been closed could throw an ObjectDisposedException. Notes to InheritorsThe default implementation on Applies toSee also
Khái niệm về streamMột luồng (stream) là một đối tượng được sử dụng để truyền dữ liệu. Khi dữ liệu truyền từ các nguồn bên ngoài vào ứng dụng ta gọi đó là Nguồn bên ngoài thường là các file, tuy nhiên tổng quát thì nó có thể từ nhiều loại như đọc/ghi dữ liệu thông qua một giao thức mạng để trao đổi dữ liệu với một máy remote khác, đọc/ghi vào một nhớ ... Một số stream chỉ cho đọc, một số chỉ cho ghi, một số lại cho phép truy cập nhẫu nhiên chứ không chỉ truy cập tuần tự (cho phép thay đổi vị trí con trỏ đọc dữ liệu trong luồng - ví dụ dịch chuyển vào giữa luồng dữ liệu để đọc dữ liệu ở khoảng nào đó) Thư viện .NET cung cấp lớp cơ sở Lấy thông tin về
stream - khi có đối tượng lớp
Một số phương thức cho đối tượng Stream
Lớp string filepath = "/home/data/data.txt"; using (var stream = new FileStream(path:filepath, mode: FileMode.Open, access: FileAccess.Read, share: FileShare.Read)) { // code sử dụng stream (System.IO.Stream) } Như vậy, để tạo ra một stream file, để trao đổi dữ liệu cần 4 thông tin:
Lớp Lấy thông tin về stream - khi có đối tượng lớp string filepath = "/mycode/1.txt"; using (var stream = new FileStream( path:filepath, mode: FileMode.Open, access: FileAccess.ReadWrite, share: FileShare.Read)) { Console.WriteLine(stream.Name); Console.WriteLine($"Kích thước stream {stream.Length} bytes / Vị trí {stream.Position}"); Console.WriteLine($"Stream có thể : Đọc {stream.CanRead} - Ghi {stream.CanWrite} - Seek {stream.CanSeek} - Timeout {stream.CanTimeout} "); } // /mycode/1.txt // Kích thước stream 289 bytes / Vị trí 0 // Stream có thể : Đọc True - Ghi True - Seek True - Timeout False Lấy thông tin encoding của file Text Khi đọc các file text (không phải file nhị phân), đầu tiên cần xác định encoding của nó (UTF8, Unicode, UTF32 ...). Thông tin về encoding được lưu ở vài byte đầu tiên của file nó gọi là BOM - Preamble (xem thêm BOM). Tùy thuộc vào giá trị của khoảng 5 byte đầu tiên này mà xác định được encoding. Thường bạn chỉ việc 5 byte đầu tiên, để xác định encoding như sau: using System; using System.IO; using System.Text; namespace CS016_Stream_FileStream { public class UtilsEncoding { public static Encoding GetEncoding (Stream stream) { byte[] BOMBytes = new byte[4]; // mảng chứa 4 byte để làm bộ nhớ lưu byte đọc được int offset = 0; // vị trí (index) trong buffer - nơi ghi byte đầu tiên đọc được int count = 4; // đọc 4 byte int numberbyte = stream.Read (BOMBytes, offset, count); // bắt đầu đọc 4 đầu tiên lưu vào buffer if (BOMBytes[0] == 0xfe && BOMBytes[1] == 0xff) { stream.Seek (2, SeekOrigin.Begin); // Di chuyển về vị trí bắt đầu của dữ liệu (đã trừ BOM) return Encoding.BigEndianUnicode; } if (BOMBytes[0] == 0xff && BOMBytes[1] == 0xfe) { stream.Seek (2, SeekOrigin.Begin); // Di chuyển về vị trí bắt đầu của dữ liệu (đã trừ BOM) return Encoding.Unicode; } if (BOMBytes[0] == 0xef && BOMBytes[1] == 0xbb && BOMBytes[2] == 0xbf) { stream.Seek (3, SeekOrigin.Begin); return Encoding.UTF8; } if (BOMBytes[0] == 0x2b && BOMBytes[1] == 0x2f && BOMBytes[2] == 0x76) { stream.Seek (3, SeekOrigin.Begin); return Encoding.UTF7; } if (BOMBytes[0] == 0xff && BOMBytes[1] == 0xfe && BOMBytes[2] == 0 && BOMBytes[3] == 0) { stream.Seek (4, SeekOrigin.Begin); return Encoding.UTF32; } if (BOMBytes[0] == 0 && BOMBytes[1] == 0 && BOMBytes[2] == 0xfe && BOMBytes[3] == 0xff) { stream.Seek (4, SeekOrigin.Begin); return Encoding.GetEncoding (12001); } stream.Seek (0, SeekOrigin.Begin); return Encoding.Default; } } } Với ASCII, 7 bit biểu diễn các ký tự - nó đủ biểu diễn bảng chữ cái tiếng Anh (in hoa, thường, số ký tự đặc biệt) - ASCII 1 byte biểu diễn 1 ký tự. UTF-16 thì dùng 2 byte biểu diễn 1 ký tự. UTF-32 dùng 4 byte biểu diễn 1 ký tự. Với UTF-8 (dùng nhiều ngày nay) - nó dùng biến để xác định bao nhiêu byte cho mỗi ký tự cụ thể, Mỗi ký tự có thể từ 1 - 6 byte Ghi file text bằng stream Ghi file text (tạo mới, ghi đè) - chọn file có Encoding UTF8, đầu tiên phải ghi các bytes BOM, lấy mảng bytes DOM bằng cách gọi string filepath = "/mycode/2.txt"; using (var stream = new FileStream( path:filepath, mode: FileMode.Create, access: FileAccess.Write, share: FileShare.None)) { //Write BOM - UTF8 Encoding encoding = Encoding.UTF8; byte[] bom = encoding.GetPreamble(); stream.Write(bom, 0, bom.Length); string s1 = "Xuanthulab.net - Xin chào các bạn! \n"; string s2 = "Ví dụ - ghi file text bằng stream"; // Encode chuỗi - lưu vào mảng bytes byte[] buffer = encoding.GetBytes(s1); stream.Write(buffer, 0, buffer.Length); // lưu vào stream buffer = encoding.GetBytes(s2); stream.Write(buffer, 0, buffer.Length); // lưu vào stream } Đọc file text bằng stream Đầu tiên xác định Encoding của file text, sau đó đọc từng khối byte vào buffer (mảng byte), rồi thực hiện Encoding để xác định chuỗi. string filepath = "/mycode/1.txt"; int SIZEBUFFER = 256; using (var stream = new FileStream( path:filepath, mode: FileMode.Open, access: FileAccess.ReadWrite, share: FileShare.Read)) { Encoding encoding = GetEncoding(stream); Console.WriteLine(encoding.ToString()); byte[] buffer = new byte[SIZEBUFFER]; bool endread = false; do { int numberRead = stream.Read(buffer, 0, SIZEBUFFER); if (numberRead == 0) endread = true; if (numberRead < SIZEBUFFER) { Array.Clear(buffer, numberRead, SIZEBUFFER - numberRead); } string s = encoding.GetString(buffer, 0, numberRead); Console.WriteLine(s); } while (!endread); } Copy file stream Tạo 2 stream, một để đọc - một để lưu string filepath_src = "/mycode/1.txt"; string filepath_des = "/mycode/3.txt"; int SIZEBUFFER = 5; // tăng lên đọc sẽ nhanh using (var streamwrite = File.OpenWrite(filepath_des)) using (var streamread = File.OpenRead(filepath_src)) { byte[] buffer = new byte[SIZEBUFFER]; bool endread = false; do { int numberRead = streamread.Read(buffer, 0, SIZEBUFFER); if (numberRead == 0) endread = true; else { streamwrite.Write(buffer, 0, numberRead); } } while (!endread); } Tham khảo mã nguồn CS016_Stream_FileStream (git) hoặc tải về ex016-1 |