void open() throws ReplicatorException, InterruptedException {
  try {
    if (getFileName() == null) {
      throw new MySQLExtractException("No binlog file specified");
    }
    if (bfdi != null) {
      throw new MySQLExtractException("Attempt to open binlog twice: " + this.fileName);
    }
    File file=new File(getDirectory() + File.separator + getFileName());
    int tryCnt=0;
    while (file.exists() == false && tryCnt++ < 500) {
      Thread.sleep(10);
    }
    if (logger.isDebugEnabled())     logger.debug("Opening file " + file.getName() + " with buffer = "+ bufferSize);
    bfdi=new BufferedFileDataInput(file,bufferSize);
    byte magic[]=new byte[MysqlBinlog.BIN_LOG_HEADER_SIZE];
    try {
      waitAvailable(magic.length,binlogFlushDelayMillis);
      if (available() < magic.length) {
        throw new MySQLExtractException("Failed reading header;  Probably an empty file or very slow file system: " + getBaseName());
      }
      read(magic);
      if (!Arrays.equals(magic,MysqlBinlog.BINLOG_MAGIC)) {
        throw new MySQLExtractException("File is not a binary log file - found : " + LogEvent.hexdump(magic) + " / expected : "+ LogEvent.hexdump(MysqlBinlog.BINLOG_MAGIC));
      }
    }
 catch (    IOException e) {
      throw new MySQLExtractException("Failed reading binlog file header: " + getBaseName(),e);
    }
    mark(2048);
    byte[] header=new byte[MysqlBinlog.PROBE_HEADER_LEN];
    waitAvailable(header.length,binlogFlushDelayMillis);
    if (available() < header.length) {
      throw new MySQLExtractException("Failed reading header;  You may have an incomplete log file or a very slow file system: " + getBaseName());
    }
    bfdi.readFully(header);
    int typeCode=header[4];
    int eventLength=(int)LittleEndianConversion.convert4BytesToLong(header,MysqlBinlog.EVENT_LEN_OFFSET);
    if (typeCode == MysqlBinlog.START_EVENT_V3) {
      if (eventLength == 69) {
        version=MysqlBinlog.BINLOG_V1;
        if (logger.isDebugEnabled())         logger.debug("Binlog format is V1");
      }
 else       if (eventLength == 75) {
        version=MysqlBinlog.BINLOG_V3;
        if (logger.isDebugEnabled())         logger.debug("Binlog format is V3");
      }
 else {
        throw new MySQLExtractException("Unexpected start event length: file=" + this.fileName + " length="+ eventLength);
      }
    }
 else     if (typeCode == MysqlBinlog.FORMAT_DESCRIPTION_EVENT) {
      version=MysqlBinlog.BINLOG_V4;
      if (logger.isDebugEnabled())       logger.debug("Binlog format is V4");
    }
 else     if (typeCode == MysqlBinlog.ROTATE_EVENT) {
      version=MysqlBinlog.BINLOG_V3;
      if (logger.isDebugEnabled())       logger.debug("Binlog format is V3 (special case w/ rotate event)");
    }
 else {
      throw new MySQLExtractException("Unexpected start event type code: file=" + this.fileName + " type code="+ typeCode);
    }
    if (startPosition >= bfdi.getOffset()) {
      bfdi.seek(startPosition);
    }
 else {
      bfdi.reset();
    }
  }
 catch (  FileNotFoundException e) {
    throw new MySQLExtractException("Unable to open binlog file",e);
  }
catch (  IOException e) {
    throw new MySQLExtractException("Unable to scan binlog file",e);
  }
}

void open ( ) throws ReplicatorException , InterruptedException { try { if ( getFileName ( ) == null ) { throw new MySQLExtractException ( "No binlog file specified" ) ; } if ( bfdi != null ) { throw new MySQLExtractException ( "Attempt to open binlog twice: " + this . fileName ) ; } File file = new File ( getDirectory ( ) + File . separator + getFileName ( ) ) ; int tryCnt = 0 ; while ( file . exists ( ) == false && tryCnt ++ < 500 ) { Thread . sleep ( 10 ) ; } if ( logger . isDebugEnabled ( ) ) logger . debug ( "Opening file " + file . getName ( ) + " with buffer = " + bufferSize ) ; bfdi = new BufferedFileDataInput ( file , bufferSize ) ; byte magic [ ] = new byte [ MysqlBinlog . BIN_LOG_HEADER_SIZE ] ; try { waitAvailable ( magic . length , binlogFlushDelayMillis ) ; if ( available ( ) < magic . length ) { throw new MySQLExtractException ( "Failed reading header;  Probably an empty file or very slow file system: " + getBaseName ( ) ) ; } read ( magic ) ; if ( ! Arrays . equals ( magic , MysqlBinlog . BINLOG_MAGIC ) ) { throw new MySQLExtractException ( "File is not a binary log file - found : " + LogEvent . hexdump ( magic ) + " / expected : " + LogEvent . hexdump ( MysqlBinlog . BINLOG_MAGIC ) ) ; } } catch ( IOException e ) { throw new MySQLExtractException ( "Failed reading binlog file header: " + getBaseName ( ) , e ) ; } mark ( 2048 ) ; byte [ ] header = new byte [ MysqlBinlog . PROBE_HEADER_LEN ] ; waitAvailable ( header . length , binlogFlushDelayMillis ) ; if ( available ( ) < header . length ) { throw new MySQLExtractException ( "Failed reading header;  You may have an incomplete log file or a very slow file system: " + getBaseName ( ) ) ; } bfdi . readFully ( header ) ; int typeCode = header [ 4 ] ; int eventLength = ( int ) LittleEndianConversion . convert4BytesToLong ( header , MysqlBinlog . EVENT_LEN_OFFSET ) ; if ( typeCode == MysqlBinlog . START_EVENT_V3 ) { if ( eventLength == 69 ) { version = MysqlBinlog . BINLOG_V1 ; if ( logger . isDebugEnabled ( ) ) logger . debug ( "Binlog format is V1" ) ; } else if ( eventLength == 75 ) { version = MysqlBinlog . BINLOG_V3 ; if ( logger . isDebugEnabled ( ) ) logger . debug ( "Binlog format is V3" ) ; } else { throw new MySQLExtractException ( "Unexpected start event length: file=" + this . fileName + " length=" + eventLength ) ; } } else if ( typeCode == MysqlBinlog . FORMAT_DESCRIPTION_EVENT ) { version = MysqlBinlog . BINLOG_V4 ; if ( logger . isDebugEnabled ( ) ) logger . debug ( "Binlog format is V4" ) ; } else if ( typeCode == MysqlBinlog . ROTATE_EVENT ) { version = MysqlBinlog . BINLOG_V3 ; if ( logger . isDebugEnabled ( ) ) logger . debug ( "Binlog format is V3 (special case w/ rotate event)" ) ; } else { throw new MySQLExtractException ( "Unexpected start event type code: file=" + this . fileName + " type code=" + typeCode ) ; } if ( startPosition >= bfdi . getOffset ( ) ) { bfdi . seek ( startPosition ) ; } else { bfdi . reset ( ) ; } } catch ( FileNotFoundException e ) { throw new MySQLExtractException ( "Unable to open binlog file" , e ) ; } catch ( IOException e ) { throw new MySQLExtractException ( "Unable to scan binlog file" , e ) ; } }
opens up the binlog file , validates the magic number , and checks the header for the binlog version . if this call succeeds the binlog is ready for reading .
