public static byte[] LoadFile(string filename) { RandomAccessFile file = new RandomAccessFile(filename, "r"); byte[] buffer = new byte[(int)file.Length()]; file.Read(buffer); file.Close(); return buffer; }
/// <exception cref="System.IO.IOException"></exception> /// <exception cref="Mp3net.InvalidDataException"></exception> private void ScanFile(RandomAccessFile file) { byte[] bytes = new byte[bufferLength]; int fileOffset = PreScanFile(file); file.Seek(fileOffset); bool lastBlock = false; int lastOffset = fileOffset; while (!lastBlock) { int bytesRead = file.Read(bytes, 0, bufferLength); if (bytesRead < bufferLength) { lastBlock = true; } if (bytesRead >= MINIMUM_BUFFER_LENGTH) { while (true) { try { int offset = 0; if (startOffset < 0) { offset = ScanBlockForStart(bytes, bytesRead, fileOffset, offset); if (startOffset >= 0 && !scanFile) { return; } lastOffset = startOffset; } offset = ScanBlock(bytes, bytesRead, fileOffset, offset); fileOffset += offset; file.Seek(fileOffset); break; } catch (InvalidDataException e) { if (frameCount < 2) { startOffset = -1; xingOffset = -1; frameCount = 0; bitrates.Clear(); lastBlock = false; fileOffset = lastOffset + 1; if (fileOffset == 0) { throw new InvalidDataException("Valid start of mpeg frames not found", e); } file.Seek(fileOffset); break; } return; } } } } }
/// <exception cref="System.IO.IOException"></exception> public Mp3FileForTesting(Mp3FileTest _enclosing, string filename) { this._enclosing = _enclosing; RandomAccessFile file = new RandomAccessFile(filename, "r"); this.preScanResult = this.PreScanFile(file); }
/// <exception cref="System.IO.IOException"></exception> /// <exception cref="Mp3net.UnsupportedTagException"></exception> /// <exception cref="Mp3net.InvalidDataException"></exception> private void InitId3v2Tag(RandomAccessFile file) { if (xingOffset == 0 || startOffset == 0) { id3v2Tag = null; } else { int bufferLength; if (HasXingFrame()) { bufferLength = xingOffset; } else { bufferLength = startOffset; } byte[] bytes = new byte[bufferLength]; file.Seek(0); int bytesRead = file.Read(bytes, 0, bufferLength); if (bytesRead < bufferLength) { throw new IOException("Not enough bytes read"); } try { id3v2Tag = ID3v2TagFactory.CreateTag(bytes); } catch (NoSuchTagException) { id3v2Tag = null; } } }
/// <exception cref="System.IO.IOException"></exception> private void SaveMpegFrames(RandomAccessFile saveFile) { int filePos = xingOffset; if (filePos < 0) { filePos = startOffset; } if (filePos < 0) { return; } if (endOffset < filePos) { return; } RandomAccessFile file = new RandomAccessFile(filename, "r"); byte[] bytes = new byte[bufferLength]; try { file.Seek(filePos); while (true) { int bytesRead = file.Read(bytes, 0, bufferLength); if (filePos + bytesRead <= endOffset) { saveFile.Write(bytes, 0, bytesRead); filePos += bytesRead; } else { saveFile.Write(bytes, 0, endOffset - filePos + 1); break; } } } finally { file.Close(); } }
/// <exception cref="System.IO.IOException"></exception> private void InitId3v1Tag(RandomAccessFile file) { byte[] bytes = new byte[ID3v1Tag.TAG_LENGTH]; file.Seek(GetLength() - ID3v1Tag.TAG_LENGTH); int bytesRead = file.Read(bytes, 0, ID3v1Tag.TAG_LENGTH); if (bytesRead < ID3v1Tag.TAG_LENGTH) { throw new IOException("Not enough bytes read"); } try { id3v1Tag = new ID3v1Tag(bytes); } catch (NoSuchTagException) { id3v1Tag = null; } }
/// <exception cref="System.IO.IOException"></exception> private void InitCustomTag(RandomAccessFile file) { int bufferLength = (int)(GetLength() - (endOffset + 1)); if (HasId3v1Tag()) { bufferLength -= ID3v1Tag.TAG_LENGTH; } if (bufferLength <= 0) { customTag = null; } else { customTag = new byte[bufferLength]; file.Seek(endOffset + 1); int bytesRead = file.Read(customTag, 0, bufferLength); if (bytesRead < bufferLength) { throw new IOException("Not enough bytes read"); } } }
/// <exception cref="System.IO.IOException"></exception> /// <exception cref="Mp3net.UnsupportedTagException"></exception> /// <exception cref="Mp3net.InvalidDataException"></exception> private void Init() { RandomAccessFile file = new RandomAccessFile(filename, "r"); try { InitId3v1Tag(file); ScanFile(file); if (startOffset < 0) { throw new InvalidDataException("No mpegs frames found"); } InitId3v2Tag(file); if (scanFile) { InitCustomTag(file); } } finally { file.Close(); } }
internal virtual int PreScanFile(RandomAccessFile file) { byte[] bytes = new byte[AbstractID3v2Tag.HEADER_LENGTH]; try { file.Seek(0); int bytesRead = file.Read(bytes, 0, AbstractID3v2Tag.HEADER_LENGTH); if (bytesRead == AbstractID3v2Tag.HEADER_LENGTH) { try { ID3v2TagFactory.SanityCheckTag(bytes); return AbstractID3v2Tag.HEADER_LENGTH + BufferTools.UnpackSynchsafeInteger(bytes[ AbstractID3v2Tag.DATA_LENGTH_OFFSET], bytes[AbstractID3v2Tag.DATA_LENGTH_OFFSET + 1], bytes[AbstractID3v2Tag.DATA_LENGTH_OFFSET + 2], bytes[AbstractID3v2Tag.DATA_LENGTH_OFFSET + 3]); } catch (NoSuchTagException) { } catch (UnsupportedTagException) { } } } catch (IOException) { } // do nothing // do nothing // do nothing return 0; }
/// <exception cref="System.IO.IOException"></exception> /// <exception cref="Mp3net.NotSupportedException"></exception> public virtual void Save(string newFilename) { if (String.Equals(filename, newFilename, StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException("Save filename same as source filename"); } RandomAccessFile saveFile = new RandomAccessFile(newFilename, "rw"); try { if (HasId3v2Tag()) { saveFile.Write(id3v2Tag.ToBytes()); } SaveMpegFrames(saveFile); if (HasCustomTag()) { saveFile.Write(customTag); } if (HasId3v1Tag()) { saveFile.Write(id3v1Tag.ToBytes()); } } finally { saveFile.Close(); } }