* src/AudioFileIO.cs, src/AudioFile.cs, src/AudioFileWrapper.cs, src/Util/AudioFileReader.cs: Add interfaces for working with streams and mimetypes. Index: src/AudioFileIO.cs =================================================================== --- src/AudioFileIO.cs (revision 48244) +++ src/AudioFileIO.cs (working copy) @@ -80,5 +80,23 @@ return (afr as AudioFileReader).Read(f); } + + public static AudioFile Read(string f, string mimetype) { + object afr = mimetypes[mimetype]; + if( afr == null) + throw new CannotReadException("No Reader associated to this MimeType: "+mimetype); + + return (afr as AudioFileReader).Read(f); + } + + public static AudioFile Read(Stream stream, string mimetype) + { + object afr = mimetypes[mimetype]; + if ( afr == null) + throw new CannotReadException("No Reader associated to this MimeType: "+mimetype); + + return (afr as AudioFileReader).Read(stream); + } + } } Index: src/AudioFile.cs =================================================================== --- src/AudioFile.cs (revision 48243) +++ src/AudioFile.cs (working copy) @@ -42,18 +42,15 @@ public class AudioFile { - private string s; private EncodingInfo info; private Tag tag; - public AudioFile(string s, EncodingInfo info, Tag tag) { - this.s = s; + public AudioFile(EncodingInfo info, Tag tag) { this.info = info; this.tag = tag; } - public AudioFile(string s, EncodingInfo info) { - this.s = s; + public AudioFile(EncodingInfo info) { this.info = info; this.tag = new GenericTag(); } @@ -98,7 +95,7 @@ } public override string ToString() { - return "AudioFile "+s+" --------\n"/*+info.ToString()*/+"\n"+ ( (tag == null) ? "" : tag.ToString())+"\n-------------------"; + return "AudioFile --------\n"/*+info.ToString()*/+"\n"+ ( (tag == null) ? "" : tag.ToString())+"\n-------------------"; } } } Index: src/AudioFileWrapper.cs =================================================================== --- src/AudioFileWrapper.cs (revision 48243) +++ src/AudioFileWrapper.cs (working copy) @@ -25,6 +25,7 @@ */ using System; +using System.IO; using Entagged.Audioformats; using Entagged.Audioformats.Util; @@ -34,12 +35,26 @@ { private AudioFile afb; private string filename; + private string mimetype; public AudioFileWrapper(string filename) { this.filename = filename; afb = AudioFileIO.Read(filename); } + + public AudioFileWrapper(string filename, string mimetype) + { + this.filename = filename; + this.mimetype = mimetype; + afb = AudioFileIO.Read(filename, mimetype); + } + + public AudioFileWrapper(Stream stream, string mimetype) + { + this.mimetype = mimetype; + afb = AudioFileIO.Read(stream, mimetype); + } public int Bitrate { @@ -165,6 +180,13 @@ return filename; } } + + public string MimeType + { + get { + return mimetype; + } + } public EncodingInfo EncodingInfo { Index: src/Util/AudioFileReader.cs =================================================================== --- src/Util/AudioFileReader.cs (revision 48243) +++ src/Util/AudioFileReader.cs (working copy) @@ -39,36 +39,47 @@ protected abstract EncodingInfo GetEncodingInfo( Stream raf ); protected abstract Tag GetTag( Stream raf ); + + public AudioFile Read(string f) { + FileStream stream; + + try { + stream = File.Open (f, FileMode.Open, FileAccess.Read, FileShare.Read); + } catch (Exception e) { + throw new CannotReadException("\""+f+"\": "+e.Message); + } + + return Read (stream); + } - public AudioFile Read(string f) { - FileInfo finfo = new FileInfo(f); + public AudioFile Read(Stream stream) { + if (! stream.CanSeek) + throw new CannotReadException("Stream not seekable"); + + if (stream.Length <= 150) + throw new CannotReadException("Less than 150 byte stream"); - if(finfo.Length <= 150) - throw new CannotReadException("Less than 150 byte \""+f+"\""); - - Stream raf = null; try{ - raf = finfo.OpenRead (); - raf.Seek( 0, SeekOrigin.Begin ); + stream.Seek( 0, SeekOrigin.Begin ); - EncodingInfo info = GetEncodingInfo(raf); + EncodingInfo info = GetEncodingInfo(stream); Tag tag; try { - raf.Seek( 0, SeekOrigin.Begin ); - tag = GetTag(raf); + stream.Seek( 0, SeekOrigin.Begin ); + tag = GetTag(stream); } catch (CannotReadException e) { tag = new GenericTag(); } - - return new AudioFile(f, info, tag); + + return new AudioFile(info, tag); } catch ( Exception e ) { - throw new CannotReadException("\""+f+"\" :"+e); + throw new CannotReadException(e.Message); } finally { try{ - if(raf != null) - raf.Close(); + if(stream != null) + stream.Close(); }catch(Exception ex){ /* We tried everything... */ }