BinaryReader溢出错误 [英] BinaryReader overflow error
问题描述
我正在读取一个WAV文件,其中的块中包含非标准数据(基本上,WAV文件正确或错误地在块中具有ID标签-这是我希望我的代码能够处理的东西清理这些文件).
有时,在处理WAV文件中的非标准块时,以下代码可以正常工作.前两个ReadChars语句始终有效,但是在第一个DO WHILE循环中,我正在搜索"fmt",binaryreader语句(
I''m reading a WAV file with non-standard data in the chunk (basically, the WAV file has, rightly or wrongly, ID tags in the chunk - which is something I want my code to handle so that it can clean up these kinds of files).
Sometimes, the following code is working fine when processing a non-stand chunk in a WAV file. The first two ReadChars statements always work, but in the first DO WHILE loop where I''m searching for ''fmt '', the binaryreader statement (
SubChunk1ID = brObject.ReadChars(4)
)有时(并非总是如此,这取决于我正在处理的文件)会产生此错误:
输出char缓冲区太小,无法包含已解码的字符,编码为"Unicode(UTF-8)"后备项"System.Text.DecoderReplacementFallback"参数名称:chars
DO WHILE循环执行一个成功的ReadChars,返回文本"JUNK"(实际上在文件中).在第二遍,它将生成错误.它尝试在第二遍读取的十六进制值为"F0 00 00 00".
显然,有一些异常编码超出了标准字符串变量的长度(这对我来说就是这样).我想这并不奇怪,因为这是二进制数据,而不是文本.
我想我除了ReadChar之外还需要做其他事情,但是我对VB.Net不太了解,也不知道从文件流中获取编码数据的最佳方法是什么.
或者也许我缺少有关C#与VB.Net中的ReadChars行为的一些信息,我没有考虑.
任何想法和评论都将受到欢迎!
) sometimes (not always, depending on which file I''m processing) generates this error:
The output char buffer is too small to contain the decoded characters, encoding ''Unicode (UTF-8)'' fallback ''System.Text.DecoderReplacementFallback'' Parameter name: chars
The DO WHILE loop does one successful ReadChars, bringing back the text "JUNK" (which is actually there in the file). On the second pass, it generates the error. The hex values that it attempts to read in the second pass are "F0 00 00 00".
Obviously there is some unusual encoding that is exceeding the length of a standard string variable (that''s how it looks to me). No surprise, I guess, because this is binary data, not text.
I''m thinking I need to do something else besides ReadChar, but I''m not very experience in VB.Net and don''t know what alternative would be the best to work with in getting encoded data out of a filestream.
Or perhaps there''s something I''m missing about how ReadChars behaves in C# vs VB.Net, which I''m not accounting for.
Any thoughts and comments would be welcomed!
Class WaveIO
Public IsWaveFile As Boolean = False
' *** The "RIFF" chunk descriptor
Public ChunkID As String
Public ChunkSize As Integer
Public Format As String
' *** The "fmt" sub-chunk descriptor
Public SubChunk1ID As String
Public Subchunk1Size As UInt32
Public AudioFormat As UInt16
Public Channels As UInt16
Public SampleRate As UInt32
Public ByteRate As UInt32
Public BlockAlign As UInt16
Public BitsPerSample As UInt16
Public SubChunk2ID As String
Public Subchunk2Size As UInt32
Public FmtSubChunkOffset As Integer
Public DataSubChunkOffset As Integer
' *** Size of the entire file in bytes, minus 8 bytes for the two fields
' *** not included in the ChunkSize field (ChunkID and ChunkSize).
Public FileLength As UInt32
' *** Size of the data in bytes, minus 44 bytes length of the "Riff" chunk
' *** and the "fmt" sub-chunk.
Public DataLength As UInt32
Public Sub WaveHeader(ByVal strPath As String)
Dim fsObject As FileStream
Dim brObject As BinaryReader
fsObject = New FileStream(strPath, FileMode.Open, FileAccess.Read)
brObject = New BinaryReader(fsObject)
Dim byteData() As Byte
Dim charData() As Char
' *** To be a valid WAV file, the first 12 bytes must be the 'RIFF' chunk descriptor.
' ***
' *** RIFF Chunk - The first 4 characters must be 'RIFF' (Resource Interchange File Format).
ChunkID = brObject.ReadChars(4)
If ChunkID <> "RIFF" Then
IsWaveFile = False
Exit Sub
End If
' *** RIFF Chunk - The next 4 characters will be the file's total size, less
' *** the first 8 bytes.
ChunkSize = brObject.ReadInt32
' *** RIFF Chunk - The next 4 characters should be 'WAVE' (Waveform Audio File Format)
Format = brObject.ReadChars(4)
If Format <> "WAVE" Then
IsWaveFile = False
Exit Sub
End If
' *** The 'RIFF' chunk descriptor looks to be valid.
' *** What needs to be identified next is the 'fmt ' (Format) sub-chunk descriptor.
' *** This may or may not immediately follow the 'RIFF' chunk. Some programs and
' *** applications will embed other 'chunks' like 'INFO', 'JUNK', etc. (most likely
' *** meta data, i.e. ID tags.)
' *** Look for a sub-chunk id of 'fmt '.
' Do While (brObject.PeekChar <> -1)
ReDim byteData(3)
ReDim charData(3)
Do While 1 = 1
Try
SubChunk1ID = brObject.ReadChars(4)
Catch Ex As ArgumentException
MessageBox.Show(Ex.Message, "ArgumentException", MessageBoxButton.OK, MessageBoxImage.Information)
Catch ex As Exception
MessageBox.Show(ex.Message, "Unexpected", MessageBoxButton.OK, MessageBoxImage.Information)
Exit Sub
End Try
MessageBox.Show("SubChunk1ID = " & SubChunk1ID, "Test", MessageBoxButton.OK, MessageBoxImage.Information)
MessageBox.Show("Position = " & fsObject.Position.ToString, "Test", MessageBoxButton.OK, MessageBoxImage.Information)
MessageBox.Show("Length = " & fsObject.Length.ToString, "Test", MessageBoxButton.OK, MessageBoxImage.Information)
' *** Found the target...
If SubChunk1ID = "fmt " Then
Exit Do
End If
' *** Back up 3 positions in the file so that it is reading byte-to-btye.
fsObject.Position -= 3
' *** If the 'fmt ' sub-chunk has not been found before the end of the
' *** file will be reached, then this is not a valid 'WAV' file.
If fsObject.Position >= fsObject.Length Then
IsWaveFile = False
Exit Sub
End If
Loop
推荐答案
它看起来像是字符(UTF)编码问题.看看下面的代码是否有效:
It looks like a character (UTF) encoding issue. See if the code below works:
Byte() bytes = brObject.ReadBytes(4)
Format = ASCIIEncoding.ASCII.GetString(bytes)
If Format <> "WAVE" Then
IsWaveFile = False
Exit Sub
End If
打开二进制阅读器以来.
我认为您应该使用ReadBytes方法.
一个char并不总是1个字节,如果是unicode,则为2个字节.
读取4个Unicode字符与读取8个字节相同.
如果字节模式看起来像是Unicode字符,
ReadChars方法可能会读取4个以上的字节.
您应该始终读取字节.
然后将字节转换为字符串(如果它表示字符串).
转换时,必须指定编码格式.
Since you opened a Binary Reader.
I think you should use the ReadBytes method.
A char is not always 1 bytes, in case of unicode, it is 2 bytes.
Reading 4 unicode chars, is the same as reading 8 bytes.
If the byte pattern looks like a unicode character,
the ReadChars method might read more than 4 bytes.
You should always read bytes.
And then convert the bytes to a string, if it represents a string.
When converting you must specify the encoding format.
这篇关于BinaryReader溢出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!