Code example for FileInputStream

Methods: getChannel

0
    _lastModifiedTimestamp = _file.lastModified();
 
    syncStreamOnce(true); 
 
    if ( _byteOffset > 0 )
      _inputStream.getChannel().position(_byteOffset);
 
    _currOffset = _inputStream.getChannel().position();
    _prevOffset = _currOffset;
  } 
 
  @Override 
  public synchronized int read() 
      throws IOException 
  { 
    int retVal = _inputStream.read();
    _prevOffset = _currOffset;
    _currOffset = _inputStream.getChannel().position();
 
    _numReadCalls++;
    boolean isDebugEnabled = LOG.isDebugEnabled();
 
    // if valid byte, return immediately 
    if ( (retVal != EOF) && (retVal != NULL_CHAR))
    { 
      _numReadCallsWithData++;
      if (isDebugEnabled)
        LOG.debug("Byte returned (non-EOF) is :" + retVal + ", State is :" + toString());
 
      return retVal;
    } 
 
    boolean done = false;
    while ( ((retVal == EOF) || (retVal == NULL_CHAR))
    		&& (!_closed) && (!done))
    { 
      if ( retVal == NULL_CHAR)
      { 
        // Force sync using prevOffset 
        syncOnceAndSleep(); 
        retVal = _inputStream.read();
        _prevOffset = _currOffset;
        _currOffset = _inputStream.getChannel().position();
        continue; 
      } 
 
      switch (_eofSync)
      { 
        case NO_SYNC :
        { 
          retVal = _inputStream.read();
          _prevOffset = _currOffset;
          _currOffset = _inputStream.getChannel().position();
 
          if (isDebugEnabled)
            LOG.debug("Byte returned (NO_SYNC) is :" + retVal + ", State is :" + toString());
          done = true;
          break; 
        } 
 
        case SYNC_ONCE :
        { 
          // sync and set behavior to NO_SYNC 
          syncStreamOnce(true); 
          _eofSync = EOFSyncBehavior.NO_SYNC;
          break; 
        } 
 
        case SYNC_TILL_NEW_DATA :
        { 
          // sync and read the next byte 
          syncOnceAndSleep(); 
          retVal = _inputStream.read();
          _prevOffset = _currOffset;
          _currOffset = _inputStream.getChannel().position();
          break; 
        } 
      } 
    } 
 
    if (retVal != EOF)
      _numReadCallsWithData++;
 
    if (isDebugEnabled)
      LOG.debug("Byte returned is :" + retVal + ", State is :" + toString());
 
    return retVal;
  } 
 
  /** 
   * 
   * Verifies if the byte-array contains NULL character between offset and len 
   * 
   * @param b Data buffer 
   * @param offset start location for scanning 
   * @param len number of bytes to scan 
   * @return true if NULL character is found, otherwise false 
   */ 
  private boolean isNullByteSeen(byte[]b, int offset, int len)
  { 
    int limit = offset + len;
    for (int i = offset; i < limit; i++)
    { 
      if ( b[i] == NULL_CHAR)
      { 
        LOG.warn("Found NULL character from data read from underlying stream. Offset :" + offset + ", Length :" + len + " Re-Syncing");
        return true; 
      } 
    } 
    return false; 
  } 
 
  @Override 
  public synchronized int read(byte[] b)
      throws IOException 
  { 
    int retVal = _inputStream.read(b);
    _prevOffset = _currOffset;
    _currOffset = _inputStream.getChannel().position();
 
    _numReadCalls++;
 
    boolean nullBytesSeen = false;
    boolean isDebugEnabled = LOG.isDebugEnabled();
 
    // if not EOF or NULL, return immediately 
    if ( retVal != EOF )
    { 
      nullBytesSeen = isNullByteSeen(b, 0, retVal);
 
      if ( ! nullBytesSeen )
      { 
        _numReadCallsWithData++;
 
        if (isDebugEnabled)
          LOG.debug("Num Bytes returned (non-EOF) is :" + retVal + ", State is :" + toString());
 
        return retVal;
      } 
    } 
 
    boolean done = false;
    while ( ((retVal == EOF) || nullBytesSeen)
    		&& (!_closed) && (!done))
    { 
     if ( nullBytesSeen )
     { 
       // Force sync using prevOffset 
       syncOnceAndSleep(); 
       retVal = _inputStream.read(b);
       _prevOffset = _currOffset;
       _currOffset = _inputStream.getChannel().position();
     } else { 
      switch (_eofSync)
      { 
        case NO_SYNC :
        { 
          retVal = _inputStream.read(b);
          _prevOffset = _currOffset;
          _currOffset = _inputStream.getChannel().position();
 
          if (isDebugEnabled)
            LOG.debug("Num bytes returned (NO_SYNC) is :" + retVal + ", State is :" + toString());
          done = true;
          break; 
        } 
 
        case SYNC_ONCE :
        { 
          // sync and set behavior to NO_SYNC 
          syncStreamOnce(true); 
          _eofSync = EOFSyncBehavior.NO_SYNC;
          break; 
        } 
 
        case SYNC_TILL_NEW_DATA :
        { 
          // sync and read the next byte 
          syncOnceAndSleep(); 
          retVal = _inputStream.read(b);
          _prevOffset = _currOffset;
          _currOffset = _inputStream.getChannel().position();
          break; 
        } 
       } 
     } 
     nullBytesSeen = isNullByteSeen(b, 0, retVal);
    } 
 
    if (retVal != EOF)
      _numReadCallsWithData++;
 
    if (isDebugEnabled)
      LOG.debug("Num Bytes returned is :" + retVal + ", State is :" + toString());
 
    return retVal;
  } 
 
  @Override 
public synchronized int read(byte b[], int off, int len)
      throws IOException 
  { 
    int retVal = _inputStream.read(b, off, len);
    _prevOffset = _currOffset;
    _currOffset = _inputStream.getChannel().position();
 
    _numReadCalls++;
 
    boolean isDebugEnabled = LOG.isDebugEnabled();
    boolean nullBytesSeen = false;
 
    // if not EOF or NULL, return immediately 
    if ( retVal != EOF )
    { 
      nullBytesSeen = isNullByteSeen(b, off, retVal);
 
      if ( ! nullBytesSeen)
      { 
        _numReadCallsWithData++;
 
        if (isDebugEnabled)
          LOG.debug("Num Bytes returned (non-EOF) is :" + retVal + ", State is :" + toString());
 
        return retVal;
      } 
    } 
 
    boolean done = false;
    while ( ((retVal == EOF) || nullBytesSeen)
    		&& (!_closed) && (!done))
    { 
      if ( nullBytesSeen )
      { 
        // Force sync using prevOffset 
        syncOnceAndSleep(); 
        retVal = _inputStream.read(b, off, len);
        _prevOffset = _currOffset;
        _currOffset = _inputStream.getChannel().position();
      } else { 
       switch (_eofSync)
       { 
        case NO_SYNC :
        { 
          retVal = _inputStream.read(b, off, len);
          _prevOffset = _currOffset;
          _currOffset = _inputStream.getChannel().position();
 
          if (isDebugEnabled)
            LOG.debug("Num bytes returned (NO_SYNC) is :" + retVal + ", State is :" + toString());
 
          done = true;
        } 
        case SYNC_ONCE :
        { 
          // sync and set behavior to NO_SYNC 
          syncStreamOnce(true); 
          _eofSync = EOFSyncBehavior.NO_SYNC;
          break; 
        } 
        case SYNC_TILL_NEW_DATA :
        { 
          // sync and read the next byte 
          syncOnceAndSleep(); 
          if ( ! _closed)
          { 
        	retVal = _inputStream.read(b, off, len);
            _prevOffset = _currOffset;
        	_currOffset = _inputStream.getChannel().position();
          } 
          break; 
        } 
       } 
      } 
      nullBytesSeen = isNullByteSeen(b, off, retVal);
    } 
 
    if (retVal != EOF)
      _numReadCallsWithData++;
 
    if (isDebugEnabled)
      LOG.debug("Num Bytes returned is :" + retVal + ", State is :" + toString());
 
    return retVal;
  } 
 
  /** 
   * 
   * Decide if stream is to be synced. If not,then sleep 
   * 
   * @throws IOException 
   */ 
  private synchronized void syncOnceAndSleep() 
      throws IOException 
  { 
	// Ensure eofSync is Sync_TILL_NEW_DATA once inside the monitor 
	if(_eofSync == EOFSyncBehavior.SYNC_TILL_NEW_DATA)
	{ 
		boolean refreshed = syncStreamOnce(false);
 
		if ( ! refreshed )
		{ 
			try 
			{ 
				wait(_resyncIntervalMs);
			} catch (InterruptedException ie) {
 
			} 
		} 
	} 
  } 
 
 
  /** 
   * 
   * Sync by reopening underlying stream. 
   * If force == false, syncing is done only if lastModifiedTimestamp got updated otherwise, syncing is done unconditionally 
   * 
   * @param force 
   * @throws IOException 
   */ 
  private synchronized boolean syncStreamOnce(boolean force)
      throws IOException 
  { 
    boolean doSync = force;
 
    long newLastModifiedTs = _file.lastModified();
    if ( !force )
    { 
      if (_lastModifiedTimestamp < newLastModifiedTs)
        doSync = true;
    } 
 
    if ( doSync)
    { 
      if(LOG.isDebugEnabled()) 
        LOG.debug("Syncing from file :" + _file);
 
      closeStream(); 
      _inputStream = new FileInputStream(_file);
      _inputStream.getChannel().position(_prevOffset);
      _lastModifiedTimestamp = newLastModifiedTs;
    } else { 
      // Even if sync did not happen, we should align channel position with the offset 
      _inputStream.getChannel().position(_prevOffset);
    } 
 
    //Re-align the curOffset 
    _currOffset = _inputStream.getChannel().position();
    return doSync;
  } 
 
  @Override 
  public synchronized void close() throws IOException