하지만 어떻게 파일 크기를 실제로 전송하지 않고도 얻을 수 있습니까? 나는이 정보를 헤더와 같이 호출과 함께 보낼 수 있지만이 방법을 사용하고 싶지 않습니다.
먼저 파이프 아래로 바이트 스트림의 크기를 보내야합니다. 다른 방법은 없습니다. 그것은 스트림에 씁니다 많은 복잡성을 추가하지 않습니다
(일부 붙박이 방법은 어쨌든 일을 할 수있는 모든 그게 전부가 있다면) :
var bytes = File.ReadAllBytes("somefile.txt");
stream.Write(BitConverter.GetBytes((Int32)bytes.Length), 0, 4);
stream.Write(bytes, 0, bytes.Length);
및 다음 다른 쪽의 스트림을 읽을 때 :
이 단지
예를입니다
byte[] fileLengthBytes =new byte[4];
stream.Read(fileLengthBytes, 0, 4);
int length = BitConverter.ToInt32(fileLengthBytes, 0);
//you know the size of the file now, log it or show the user
var fileBytes = new byte[length];
stream.Read(fileBytes, 0, fileBytes.Length);
- 스트림이 큰 경우 당신이 byte[]
버퍼를 생성 할 수 없습니다.
출처
2012-08-10 13:37:16
wal