public static byte[] unsynchronize(byte[] abySource){
  ByteArrayInputStream input=new ByteArrayInputStream(abySource);
  ByteArrayOutputStream output=new ByteArrayOutputStream(abySource.length);
  int count=0;
  while (input.available() > 0) {
    int firstByte=input.read();
    count++;
    output.write(firstByte);
    if ((firstByte & MPEGFrameHeader.SYNC_BYTE1) == MPEGFrameHeader.SYNC_BYTE1) {
      if (input.available() > 0) {
        input.mark(1);
        int secondByte=input.read();
        if ((secondByte & MPEGFrameHeader.SYNC_BYTE2) == MPEGFrameHeader.SYNC_BYTE2) {
          if (logger.isLoggable(Level.FINEST)) {
            logger.finest("Writing unsynchronisation bit at:" + count);
          }
          output.write(0);
        }
 else         if (secondByte == 0) {
          if (logger.isLoggable(Level.FINEST)) {
            logger.finest("Inserting zero unsynchronisation bit at:" + count);
          }
          output.write(0);
        }
        input.reset();
      }
    }
  }
  if ((abySource[abySource.length - 1] & MPEGFrameHeader.SYNC_BYTE1) == MPEGFrameHeader.SYNC_BYTE1) {
    logger.finest("Adding unsynchronisation bit at end of stream");
    output.write(0);
  }
  return output.toByteArray();
}

public static byte [ ] unsynchronize ( byte [ ] abySource ) { ByteArrayInputStream input = new ByteArrayInputStream ( abySource ) ; ByteArrayOutputStream output = new ByteArrayOutputStream ( abySource . length ) ; int count = 0 ; while ( input . available ( ) > 0 ) { int firstByte = input . read ( ) ; count ++ ; output . write ( firstByte ) ; if ( ( firstByte & MPEGFrameHeader . SYNC_BYTE1 ) == MPEGFrameHeader . SYNC_BYTE1 ) { if ( input . available ( ) > 0 ) { input . mark ( 1 ) ; int secondByte = input . read ( ) ; if ( ( secondByte & MPEGFrameHeader . SYNC_BYTE2 ) == MPEGFrameHeader . SYNC_BYTE2 ) { if ( logger . isLoggable ( Level . FINEST ) ) { logger . finest ( "Writing unsynchronisation bit at:" + count ) ; } output . write ( 0 ) ; } else if ( secondByte == 0 ) { if ( logger . isLoggable ( Level . FINEST ) ) { logger . finest ( "Inserting zero unsynchronisation bit at:" + count ) ; } output . write ( 0 ) ; } input . reset ( ) ; } } } if ( ( abySource [ abySource . length - 1 ] & MPEGFrameHeader . SYNC_BYTE1 ) == MPEGFrameHeader . SYNC_BYTE1 ) { logger . finest ( "Adding unsynchronisation bit at end of stream" ) ; output . write ( 0 ) ; } return output . toByteArray ( ) ; }
unsynchronize an array of bytes , this should only be called if the decision has already been made to unsynchronize the byte array in order to prevent a media player from incorrectly interpreting the contents of a tag , all $ ff bytes followed by a byte with value > = 224 must be followed by a $ 00 byte ( thus , $ ff $ f0 sequences become $ ff $ 00 $ f0 ) . additionally because unsynchronisation is being applied any existing $ ff $ 00 have to be converted to $ ff $ 00 $ 00
