public FrameScanner(Stream input) { input.Seek(0, SeekOrigin.Begin); byte[] frameheadbuffer = new byte[byte.MaxValue + 4]; byte[] buffer = new byte[6]; input.Read(buffer, 0, 6); headerOkay = Encoding.ASCII.GetString(buffer, 0, 4) == "MMDb"; if (!headerOkay) return; version = new Version(buffer[4], buffer[5]); if (version == FileVersions.First) { input.Read(buffer, 0, 4); id = BitConverter.ToInt32(buffer, 0); while (input.Position < input.Length) { byte b = (byte)input.ReadByte(); input.Read(frameheadbuffer, 0, 4 + b); FrameIdentifier identifier = new FrameIdentifier(frameheadbuffer, 0, b); int length = BitConverter.ToInt32(frameheadbuffer, b); int position = (int)input.Position; Frame frame = new Frame(position, length, 5 + b); frames.Add(identifier, frame); input.Seek(length, SeekOrigin.Current); } } else { headerOkay = false; return; } }
private bool SeekToFrame(FrameIdentifier identifier) { if (!fs.Contains(identifier)) return false; input.Seek(fs[identifier].DataPosition, SeekOrigin.Begin); return true; }
private bool SeekToFrame(FrameIdentifier identifier, out int length) { length = 0; if (!fs.Contains(identifier)) return false; input.Seek(fs[identifier].DataPosition, SeekOrigin.Begin); length = fs[identifier].DataLength; return true; }
public bool Delete(FrameIdentifier frame) { bool removed = frames.ContainsKey(frame); Frame thisF = frames[frame]; frames.Remove(frame); foreach (KeyValuePair<FrameIdentifier, Frame> f in frames) if (f.Value.FramePosition > thisF.FramePosition) f.Value.Move(thisF.FrameLength); return removed; }
public System.Drawing.Size ReadImageSize(FrameIdentifier identifier) { if (!SeekToFrame(identifier)) throw new ArgumentException(identifier.Value + " Frame not found."); byte[] buffer = new byte[8]; input.Read(buffer, 0, 8); return new System.Drawing.Size(BitConverter.ToInt32(buffer, 0), BitConverter.ToInt32(buffer, 4)); }
public WriterCredit ReadWriter(FrameIdentifier identifier, PersonCollection collection) { return new WriterCredit(readPerson(identifier, collection)); }
public Rating ReadRating(FrameIdentifier identifier) { if (!SeekToFrame(identifier)) throw new ArgumentException(identifier.Value + " Frame not found."); byte[] buffer = new byte[4]; input.Read(buffer, 0, 4); return new Rating(identifier.Value.Substring(1), BitConverter.ToBoolean(buffer, 0), buffer[1], buffer[2], buffer[3]); }
public void Write(FrameIdentifier identifier, string value, Encoding encoding) { MemoryStream ms = new MemoryStream(); AddString(ms, value, encoding); WriteFrameFromStream(identifier, ms, false); ms.Dispose(); }
public ProducerCredit ReadProducer(FrameIdentifier identifier, PersonCollection collection) { return new ProducerCredit(readPerson(identifier, collection)); }
public void Delete(FrameIdentifier identifier) { if (!fs.HeaderOkay) return; if (!fs.Contains(identifier)) throw new Exception("identifier " + identifier.Value + " not found."); Frame frame = fs[identifier]; if (frame.FramePosition + frame.FrameLength == output.Length) { output.Seek(frame.FramePosition - 1, SeekOrigin.Begin); output.SetLength(frame.FramePosition); } else { output.Seek(frame.FramePosition + frame.FrameLength, SeekOrigin.Begin); byte[] buffer = new byte[512]; int read = 0; do { read = output.Read(buffer, 0, buffer.Length); if (read > 0) { output.Seek(-(frame.FrameLength + read), SeekOrigin.Current); output.Write(buffer, 0, read); output.Seek(frame.FrameLength, SeekOrigin.Current); } } while (read > 0); output.SetLength(output.Length - frame.FrameLength); } fs.Delete(identifier); }
public bool Contains(FrameIdentifier identifier) { return fs.Contains(identifier); }
public void ReadImageXNA(FrameIdentifier identifier, out long offset, out int count) { if (!SeekToFrame(identifier)) throw new ArgumentException(identifier.Value + " Frame not found."); input.Seek(8, SeekOrigin.Current); byte[] buffer = new byte[4]; input.Read(buffer, 0, 4); int length = BitConverter.ToInt32(buffer, 0); offset = input.Position; count = length; }
public long ReadInt64(FrameIdentifier identifier) { if (!SeekToFrame(identifier)) throw new ArgumentException(identifier.Value + " Frame not found."); byte[] buffer = new byte[8]; input.Read(buffer, 0, 8); long length = BitConverter.ToInt64(buffer, 0); return length; }
public bool Contains(FrameIdentifier frame) { return frames.ContainsKey(frame); }
public Frame this[FrameIdentifier identifier] { get { return frames[identifier]; } }
public void Write(FrameIdentifier identifier, GenreSet genres) { MemoryStream ms = new MemoryStream(); foreach(Genre g in genres) { byte b = (byte)g.Id; ms.WriteByte(b); } WriteFrameFromStream(identifier, ms, false); ms.Dispose(); }
public void Write(WriterCredit person) { FrameIdentifier identifier = new FrameIdentifier(person); Write(identifier, person); }
public void Write(Role person) { FrameIdentifier identifier = new FrameIdentifier(person); Write(identifier, person); }
public System.Drawing.Image ReadImage(FrameIdentifier identifier) { if (!SeekToFrame(identifier)) throw new ArgumentException(identifier.Value + " Frame not found."); input.Seek(8, SeekOrigin.Current); byte[] buffer = new byte[4]; input.Read(buffer, 0, 4); int length = BitConverter.ToInt32(buffer, 0); MemoryStream ms = new MemoryStream(); int read = 0; buffer = new byte[8192]; do { read = input.Read(buffer, 0, 8192); ms.Write(buffer, 0, read); } while (read > 0); return System.Drawing.Image.FromStream(ms); }
public void Write(FrameIdentifier identifier, long value) { using (MemoryStream ms = new MemoryStream(BitConverter.GetBytes(value))) WriteFrameFromStream(identifier, ms, true); }
public MemoryStream ReadData(FrameIdentifier identifier) { if (!SeekToFrame(identifier)) throw new ArgumentException(identifier.Value + " Frame not found."); input.Seek(8, SeekOrigin.Current); byte[] buffer = new byte[4]; input.Read(buffer, 0, 4); int length = BitConverter.ToInt32(buffer, 0); buffer = new byte[length]; input.Read(buffer, 0, length); MemoryStream ms = new MemoryStream(buffer); return ms; }
private void WriteFrameFromStream(FrameIdentifier identifier, MemoryStream stream, bool fixedSize) { if (!fs.HeaderOkay) return; if (fs.Contains(identifier)) { if (fixedSize || stream.Length <= fs[identifier].DataLength) { output.Seek(fs[identifier].DataPosition, SeekOrigin.Begin); stream.WriteTo(output); } else { Delete(identifier); WriteFrameFromStream(identifier, stream, fixedSize); } } else { output.Seek(0, SeekOrigin.End); output.WriteByte(identifier.ByteLength); output.Write(identifier.GetBytes(), 0, identifier.ByteLength); output.Write(BitConverter.GetBytes((int)stream.Length), 0, 4); fs.Add(identifier, (int)output.Position, (int)stream.Length); stream.WriteTo(output); } }
public string ReadString(FrameIdentifier identifier) { if (!SeekToFrame(identifier)) throw new ArgumentException(identifier.Value + " Frame not found."); return ReadString(); }
public void Add(FrameIdentifier frame, int dataposition, int datalength) { frames.Add(frame, new Frame(dataposition, datalength, frame.ByteLength)); }
public GenreSet ReadGenreSet(FrameIdentifier identifier, GenreCollection collection) { if (!SeekToFrame(identifier)) throw new ArgumentException(identifier.Value + " Frame not found."); GenreSet g = new GenreSet(identifier.Value.Substring(1), collection); int c = fs.Frames[identifier].DataLength; for (int i = 0; i < c; i++) g.Add(collection.GetGenre(input.ReadByte())); return g; }
private Person readPerson(FrameIdentifier identifier, PersonCollection collection) { if (!SeekToFrame(identifier)) throw new ArgumentException(identifier.Value + " Frame not found."); int id = int.Parse(identifier.Value.Substring(1), System.Globalization.NumberStyles.HexNumber); CreditTypes type = (CreditTypes)input.ReadByte(); string name = ReadString(); return collection.GetPerson(id, name); }
public Role ReadRole(FrameIdentifier identifier, PersonCollection collection) { return readRole(readPerson(identifier, collection)); }
public DirectorCredit ReadDirector(FrameIdentifier identifier, PersonCollection collection) { return new DirectorCredit(readPerson(identifier, collection)); }
public PersonCredit ReadPersonCredit(FrameIdentifier identifier, PersonCollection collection) { Person p = readPerson(identifier, collection); if (identifier.Value[1] == 'A') return readRole(p); else if (identifier.Value[1] == 'D') return new DirectorCredit(p); else if (identifier.Value[1] == 'W') return new WriterCredit(p); else if (identifier.Value[1] == 'P') return new ProducerCredit(p); else throw new Exception("Unknown Credit type."); }
public void Write(FrameIdentifier identifier, string value) { Write(identifier, value, Encoding.UTF8); }