public ByteBuffer synchronize(ByteBuffer b) { ByteBuffer bb = new ByteBuffer(b.Capacity); while(b.Remaining >= 1) { byte cur = b.Get(); bb.Put(cur); if((cur&0xFF) == 0xFF && b.Remaining >=1 && b.Peek() == 0x00) { //First part of synchronization b.Get(); } } //We have finished filling the new bytebuffer, so set the limit, and rewind bb.Limit = bb.Position; bb.Rewind(); return bb; }
public Id3v2Tag Read(ByteBuffer data, bool[] ID3Flags, byte version) { int tagSize = data.Limit; byte[] b; Id3v2Tag tag = new Id3v2Tag(); //---------------------------------------------------------------------------- //Traitement en cas d'Header Etendu==true (A COMPLETER) if (version == Id3v2Tag.ID3V23 && ID3Flags[1]) ProcessExtendedHeader(data); //---------------------------------------------------------------------------- //Extraction des champs de texte int specSize = (version == Id3v2Tag.ID3V22) ? 3 : 4; for (int a = 0; a < tagSize; a++) { //Nom de la Frame b = new byte[specSize]; if(data.Remaining <= specSize) break; data.Get(b); string field = new string(System.Text.Encoding.ASCII.GetChars(b)); if (b[0] == 0) break; //La longueur du texte contenu dans la Frame int frameSize = ReadInteger(data, version); if ((frameSize > data.Remaining) || frameSize <= 0){ //ignore empty frames break; } b = new byte[ frameSize + ((version == Id3v2Tag.ID3V23) ? 2 : 0) ]; data.Get(b); if( "" != field) { Id3Frame f = CreateId3Frame(field, b, version); if(f != null) tag.Add(f); } } return tag; }
private int ReadInteger(ByteBuffer bb, int version) { int value = 0; if(version == Id3v2Tag.ID3V23) value += (bb.Get()& 0xFF) << 24; value += (bb.Get()& 0xFF) << 16; value += (bb.Get()& 0xFF) << 8; value += (bb.Get()& 0xFF); return value; }
//Process the Extended Header in the ID3v2 Tag, returns the number of bytes to skip private int ProcessExtendedHeader(ByteBuffer data) { //TODO Verify that we have an syncsfe int byte[] exthead = new byte [4]; data.Get(exthead); int extsize = ReadInteger(data, Id3v2Tag.ID3V23); // The extended header size includes those first four bytes. data.Position = data.Position + extsize; return extsize; }