本文整理汇总了C#中System.IO.Stream.Read方法的典型用法代码示例。如果您正苦于以下问题:C# Stream.Read方法的具体用法?C# Stream.Read怎么用?C# Stream.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Stream
的用法示例。
在下文中一共展示了Stream.Read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Inflate
/// <summary>
/// Inflate the token
/// NOTE: This operation is not continuable and assumes that the entire token is available in the stream
/// </summary>
/// <param name="source">Stream to inflate the token from.</param>
/// <returns>True in case of success, false otherwise.</returns>
public override bool Inflate(Stream source)
{
// Read length of entire message
uint totalLengthOfData = TDSUtilities.ReadUInt(source);
// Read length of the fedauth token
uint tokenLength = TDSUtilities.ReadUInt(source);
// Read the fedauth token
_token = new byte[tokenLength];
source.Read(_token, 0, (int)tokenLength);
// Read nonce if it exists
if (totalLengthOfData > tokenLength)
{
_nonce = new byte[totalLengthOfData - tokenLength];
source.Read(_nonce, 0, (int)(totalLengthOfData - tokenLength));
}
else if (tokenLength > totalLengthOfData)
{
// token length cannot be greater than the total length of the message
return false;
}
return true;
}
示例2: Write
public void Write(Tag tag, Stream apeStream, Stream tempStream) {
ByteBuffer tagBuffer = tc.Create(tag);
if (!TagExists(apeStream)) {
apeStream.Seek(0, SeekOrigin.End);
apeStream.Write(tagBuffer.Data, 0, tagBuffer.Capacity);
} else {
apeStream.Seek( -32 + 8 , SeekOrigin.End);
//Version
byte[] b = new byte[4];
apeStream.Read( b , 0, b.Length);
int version = Utils.GetNumber(b, 0, 3);
if(version != 2000) {
throw new CannotWriteException("APE Tag other than version 2.0 are not supported");
}
//Size
b = new byte[4];
apeStream.Read( b , 0, b.Length);
long oldSize = Utils.GetLongNumber(b, 0, 3) + 32;
int tagSize = tagBuffer.Capacity;
apeStream.Seek(-oldSize, SeekOrigin.End);
apeStream.Write(tagBuffer.Data, 0, tagBuffer.Capacity);
if(oldSize > tagSize) {
//Truncate the file
apeStream.SetLength(apeStream.Length - (oldSize-tagSize));
}
}
}
示例3: Read
public OggTag Read( Stream oggStream )
{
OggTag tag = new OggTag();
byte[] b = new byte[4];
oggStream.Read( b , 0, b .Length);
int vendorstringLength = Utils.GetNumber( b, 0, 3);
b = new byte[vendorstringLength];
oggStream.Read( b , 0, b .Length);
tag.Add("vendor", new string(Encoding.UTF8.GetChars(b)));
b = new byte[4];
oggStream.Read( b , 0, b .Length);
int userComments = Utils.GetNumber( b, 0, 3);
for ( int i = 0; i < userComments; i++ ) {
b = new byte[4];
oggStream.Read( b , 0, b .Length);
int commentLength = Utils.GetNumber( b, 0, 3);
b = new byte[commentLength];
oggStream.Read( b , 0, b .Length);
tag.AddOggField(b);
}
return tag;
}
示例4: ReadBody
public static void ReadBody(Stream inputStream, Stream output, Headers headers, bool strict, ref float progress)
{
// Read Body
byte[] buffer = new byte[8192];
int contentLength = 0;
if (int.TryParse (headers.Get ("Content-Length"), out contentLength)) {
if (contentLength > 0) {
var remaining = contentLength;
while (remaining > 0) {
var count = inputStream.Read (buffer, 0, buffer.Length);
if (count == 0) {
break;
}
remaining -= count;
output.Write (buffer, 0, count);
progress = Mathf.Clamp01 (1.0f - ((float)remaining / (float)contentLength));
}
}
} else {
if (!strict) {
var count = inputStream.Read (buffer, 0, buffer.Length);
while (count > 0) {
output.Write (buffer, 0, count);
count = inputStream.Read (buffer, 0, buffer.Length);
}
}
progress = 1;
}
}
示例5: Read
public bool Read(Id3Tag tag, Stream mp3Stream)
{
//Check wether the file contains an Id3v1 tag--------------------------------
mp3Stream.Seek( -128 , SeekOrigin.End);
byte[] b = new byte[3];
mp3Stream.Read( b, 0, 3 );
mp3Stream.Seek(0, SeekOrigin.Begin);
string tagS = Encoding.ASCII.GetString(b);
if(tagS != "TAG")
return false;
mp3Stream.Seek( - 128 + 3, SeekOrigin.End );
//Parse the tag -)------------------------------------------------
tag.AddTitle(Read(mp3Stream, 30));
tag.AddArtist(Read(mp3Stream, 30));
tag.AddAlbum(Read(mp3Stream, 30));
//------------------------------------------------
tag.AddYear(Read(mp3Stream, 4));
tag.AddComment(Read(mp3Stream, 30));
//string trackNumber;
mp3Stream.Seek(- 2, SeekOrigin.Current);
b = new byte[2];
mp3Stream.Read(b, 0, 2);
if (b[0] == 0)
tag.AddTrack(b[1].ToString());
byte genreByte = (byte) mp3Stream.ReadByte();
mp3Stream.Seek(0, SeekOrigin.Begin);
tag.AddGenre(TagGenres.Get(genreByte));
return true;
}
示例6: Read
public EncodingInfo Read( Stream raf )
{
EncodingInfo info = new EncodingInfo();
//Begin info fetch-------------------------------------------
if ( raf.Length == 0 ) {
//Empty File
throw new CannotReadException("File is empty");
}
raf.Seek( 0 , SeekOrigin.Begin);
//MP+ Header string
byte[] b = new byte[4];
raf.Read(b, 0, b.Length);
string mpc = new string(System.Text.Encoding.ASCII.GetChars(b));
if (mpc != "MAC ") {
throw new CannotReadException("'MAC ' Header not found");
}
b = new byte[4];
raf.Read(b, 0, b.Length);
int version = Utils.GetNumber(b, 0,3);
if(version < 3970)
throw new CannotReadException("Monkey Audio version <= 3.97 is not supported");
b = new byte[44];
raf.Read(b, 0, b.Length);
MonkeyDescriptor md = new MonkeyDescriptor(b);
b = new byte[24];
raf.Read(b, 0, b.Length);
MonkeyHeader mh = new MonkeyHeader(b);
raf.Seek(md.RiffWavOffset, SeekOrigin.Begin);
b = new byte[12];
raf.Read(b, 0, b.Length);
WavRIFFHeader wrh = new WavRIFFHeader(b);
if(!wrh.Valid)
throw new CannotReadException("No valid RIFF Header found");
b = new byte[24];
raf.Read(b, 0, b.Length);
WavFormatHeader wfh = new WavFormatHeader(b);
if(!wfh.Valid)
throw new CannotReadException("No valid WAV Header found");
info.Length = mh.Length;
info.ChannelNumber = wfh.ChannelNumber ;
info.SamplingRate = wfh.SamplingRate ;
info.Bitrate = ComputeBitrate(info.Length, raf.Length) ;
info.EncodingType = "Monkey Audio v" + (((double)version)/1000)+", compression level "+mh.CompressionLevel;
info.ExtraEncodingInfos = "";
return info;
}
示例7: Read
public Id3v2Tag Read(Stream mp3Stream)
{
Id3v2Tag tag;
byte[] b = new byte[3];
mp3Stream.Read(b, 0, b.Length);
mp3Stream.Seek(0, SeekOrigin.Begin);
string ID3 = new string(System.Text.Encoding.ASCII.GetChars(b));
if (ID3 != "ID3") {
throw new CannotReadException("Not an ID3 tag");
}
//Begins tag parsing ---------------------------------------------
mp3Stream.Seek(3, SeekOrigin.Begin);
//----------------------------------------------------------------------------
//Version du tag ID3v2.xx.xx
string versionHigh=mp3Stream.ReadByte() +"";
string versionID3 =versionHigh+ "." + mp3Stream.ReadByte();
//------------------------------------------------------------------------- ---
//D?tection de certains flags (A COMPLETER)
this.ID3Flags = ProcessID3Flags( (byte) mp3Stream.ReadByte() );
//----------------------------------------------------------------------------
// On extrait la taille du tag ID3
int tagSize = ReadSyncsafeInteger(mp3Stream);
//System.err.println("TagSize: "+tagSize);
// ------------------NEWNEWNWENENEWNENWEWN-------------------------------
//Fill a byte buffer, then process according to correct version
b = new byte[tagSize+2];
mp3Stream.Read(b, 0, b.Length);
ByteBuffer bb = new ByteBuffer(b);
if (ID3Flags[0]==true) {
//We have unsynchronization, first re-synchronize
bb = synchronizer.synchronize(bb);
}
if(versionHigh == "2") {
tag = v23.Read(bb, ID3Flags, Id3v2Tag.ID3V22);
}
else if(versionHigh == "3") {
tag = v23.Read(bb, ID3Flags, Id3v2Tag.ID3V23);
}
else if(versionHigh == "4") {
throw new CannotReadException("ID3v2 tag version "+ versionID3 + " not supported !");
}
else {
throw new CannotReadException("ID3v2 tag version "+ versionID3 + " not supported !");
}
return tag;
}
示例8: ReadWriteStream
private void ReadWriteStream(Stream readStream, Stream writeStream)
{
int count = 0x100;
byte[] buffer = new byte[count];
for (int i = readStream.Read(buffer, 0, count); i > 0; i = readStream.Read(buffer, 0, count))
{
writeStream.Write(buffer, 0, i);
}
readStream.Close();
writeStream.Close();
}
示例9: Read
public EncodingInfo Read( Stream raf )
{
EncodingInfo info = new EncodingInfo();
//Begin info fetch-------------------------------------------
if ( raf.Length==0 ) {
//Empty File
throw new CannotReadException("File is empty");
}
raf.Seek( 0 , SeekOrigin.Begin);
//MP+ Header string
byte[] b = new byte[3];
raf.Read(b, 0, b.Length);
string mpc = new string(System.Text.Encoding.ASCII.GetChars(b));
if (mpc != "MP+" && mpc == "ID3") {
//TODO Do we have to do this ??
//we have an ID3v2 tag at the beginning
//We quickly jump to MPC data
raf.Seek(6, SeekOrigin.Begin);
int tagSize = ReadSyncsafeInteger(raf);
raf.Seek(tagSize+10, SeekOrigin.Begin);
//retry to read MPC stream
b = new byte[3];
raf.Read(b, 0, b.Length);
mpc = new string(System.Text.Encoding.ASCII.GetChars(b));
if (mpc != "MP+") {
//We could definitely not go there
throw new CannotReadException("MP+ Header not found");
}
} else if (mpc != "MP+"){
throw new CannotReadException("MP+ Header not found");
}
b = new byte[25];
raf.Read(b, 0, b.Length);
MpcHeader mpcH = new MpcHeader(b);
//We only support v7 Stream format, so if it isn't v7, then returned values
//will be bogus, and the file will be ignored
double pcm = mpcH.SamplesNumber;
info.Length = (int) ( pcm * 1152 / mpcH.SamplingRate );
info.ChannelNumber = mpcH.ChannelNumber;
info.SamplingRate = mpcH.SamplingRate;
info.EncodingType = mpcH.EncodingType;
info.ExtraEncodingInfos = mpcH.EncoderInfo;
info.Bitrate = ComputeBitrate( info.Length, raf.Length );
return info;
}
示例10: receive
//接收线程
public void receive()
{
filename = formm.filenamew[threadh];
strUrl = formm.strurl;
ns = null;
nbytes = new byte[512];
nreadsize = 0;
Console.WriteLine("线程" + threadh.ToString() + "开始接收");
if (!File.Exists(filename))
{
fs = new FileStream(filename, System.IO.FileMode.Create);
}
else
{
fs = File.OpenWrite(filename);
DownLoadSize = fs.Length;
formm.DownLoadtotal += DownLoadSize;
formm.filestartw[threadh] += DownLoadSize;
fs.Seek(DownLoadSize, SeekOrigin.Current); //已经存在
}
if (!formm.threadw[threadh])
{
try
{
request = (HttpWebRequest)HttpWebRequest.Create(strUrl);
//接收的起始位置及接收的长度
request.AddRange(formm.filestartw[threadh], formm.filestartw[threadh] + formm.filesizew[threadh]-DownLoadSize);
ns = request.GetResponse().GetResponseStream();//获得接收流
nreadsize = ns.Read(nbytes, 0, 512);
while (nreadsize > 0)
{
fs.Write(nbytes, 0, nreadsize);
nreadsize = ns.Read(nbytes, 0, 512);
formm.DownLoadtotal += 512;
Console.WriteLine("线程" + threadh.ToString() + "正在接收" + formm.DownLoadSpeed.ToString() + "k/s___已经下了" + formm.DownLoadtotal.ToString() + "还剩下" + formm.LeftDownLoadTime.ToString() + "s" + "百分比" + formm.DownLoadPercentage.ToString());
}
fs.Close();
ns.Close();
}
catch (Exception er)
{
Console.WriteLine("123");
fs.Close();
}
}
Console.WriteLine("进程" + threadh.ToString() + "接收完毕!");
formm.threadw[threadh] = true;
}
示例11: ReadFrom
public override int ReadFrom(Stream channel)
{
this.ExpectIncomplete();
var read = 0;
// have we read the request size yet?
if (this.sizeBuffer.Remaining() > 0)
{
read += channel.Read(this.sizeBuffer.Array, 0, 4);
this.sizeBuffer.Position = read;
}
// have we allocated the request buffer yet?
if (this.contentBuffer == null && !this.sizeBuffer.HasRemaining())
{
this.sizeBuffer.Rewind();
var size = this.sizeBuffer.GetInt();
if (size <= 0)
{
throw new InvalidRequestException(string.Format("{0} is not a valid request size", size));
}
if (size > this.MaxSize)
{
throw new InvalidRequestException(
string.Format(
"Request of length {0} is not valid, it is larget than the maximum size of {1} bytes",
size,
this.MaxSize));
}
this.contentBuffer = this.ByteBufferAllocate(size);
}
// if we have a buffer read some stuff into it
if (this.contentBuffer != null)
{
read = channel.Read(this.contentBuffer.Array, (int)(this.contentBuffer.ArrayOffset() + this.contentBuffer.Position), this.contentBuffer.Remaining());
this.contentBuffer.Position += read;
// did we get everything?
if (!this.contentBuffer.HasRemaining())
{
this.contentBuffer.Rewind();
this.complete = true;
}
}
return read;
}
示例12: Read
public EncodingInfo Read(Stream raf)
{
//Read the infos--------------------------------------------------------
if (raf.Length == 0) {
//Empty File
throw new CannotReadException("Error: File empty");
}
raf.Seek(0, SeekOrigin.Begin);
//FLAC Header string
byte[] b = new byte[4];
raf.Read(b, 0, b.Length);
string flac = new string(System.Text.Encoding.ASCII.GetChars(b));
if (flac != "fLaC") {
throw new CannotReadException("fLaC Header not found");
}
MetadataBlockDataStreamInfo mbdsi = null;
bool isLastBlock = false;
while (!isLastBlock) {
b = new byte[4];
raf.Read(b, 0, b.Length);
MetadataBlockHeader mbh = new MetadataBlockHeader(b);
if (mbh.BlockType == (int) MetadataBlockHeader.BlockTypes.StreamInfo) {
b = new byte[mbh.DataLength];
raf.Read(b, 0, b.Length);
mbdsi = new MetadataBlockDataStreamInfo(b);
if (!mbdsi.Valid) {
throw new CannotReadException("FLAC StreamInfo not valid");
}
break;
}
raf.Seek(raf.Position + mbh.DataLength, SeekOrigin.Begin);
isLastBlock = mbh.IsLastBlock;
mbh = null; //Free memory
}
EncodingInfo info = new EncodingInfo();
info.Length = mbdsi.Length;
info.ChannelNumber = mbdsi.ChannelNumber;
info.SamplingRate = mbdsi.SamplingRate;
info.EncodingType = mbdsi.EncodingType;
info.ExtraEncodingInfos = "";
info.Bitrate = ComputeBitrate(mbdsi.Length, raf.Length);
return info;
}
示例13: Read
public Id3v1Tag Read( Stream mp3Stream )
{
Id3v1Tag tag = new Id3v1Tag();
//Check wether the file contains an Id3v1 tag--------------------------------
mp3Stream.Seek( -128 , SeekOrigin.End);
byte[] b = new byte[3];
mp3Stream.Read( b, 0, 3 );
mp3Stream.Seek(0, SeekOrigin.Begin);
string tagS = new string(System.Text.Encoding.ASCII.GetChars( b ));
if(tagS != "TAG"){
throw new CannotReadException("There is no Id3v1 Tag in this file");
}
mp3Stream.Seek( - 128 + 3, SeekOrigin.End );
//Parse the tag -)------------------------------------------------
string songName = Read(mp3Stream, 30);
//------------------------------------------------
string artist = Read(mp3Stream, 30);
//------------------------------------------------
string album = Read(mp3Stream, 30);
//------------------------------------------------
string year = Read(mp3Stream, 4);
//------------------------------------------------
string comment = Read(mp3Stream, 30);
//------------------------------------------------
string trackNumber = "";
mp3Stream.Seek(- 2, SeekOrigin.Current);
b = new byte[2];
mp3Stream.Read(b, 0, 2);
if ( b[0] == 0 ) {
trackNumber = b[1].ToString ();
}
//------------------------------------------------
byte genreByte = (byte) mp3Stream.ReadByte();
mp3Stream.Seek(0, SeekOrigin.Begin);
tag.SetTitle( songName );
tag.SetArtist( artist );
tag.SetAlbum( album );
tag.SetYear( year );
tag.SetComment( comment );
tag.SetTrack( trackNumber );
tag.SetGenre( tag.TranslateGenre(genreByte) );
return tag;
}
示例14: recv
/**
* Receive a message of the given size from the inputstream.
* Makes sure the complete message is received, raises IOException otherwise.
*/
public static byte[] recv(Stream ins, int size) {
byte [] bytes = new byte [size];
int numRead = ins.Read(bytes, 0, size);
if(numRead<=0) {
throw new IOException("premature end of data");
}
while (numRead < size) {
int len = ins.Read(bytes, numRead, size - numRead);
if(len<=0) {
throw new IOException("premature end of data");
}
numRead+=len;
}
return bytes;
}
示例15: Echo
public Stream Echo(Stream data)
{
MemoryStream dataStorage = new MemoryStream();
byte[] byteArray = new byte[8192];
int bytesRead = data.Read(byteArray, 0, 8192);
while (bytesRead > 0)
{
dataStorage.Write(byteArray, 0, bytesRead);
bytesRead = data.Read(byteArray, 0, 8192);
}
data.Close();
dataStorage.Seek(0, SeekOrigin.Begin);
return dataStorage;
}