public synchronized long read(long offset, final ByteBuffer buf) {
		if (!validState) {
			throw new InvalidStateException();
		}
		try {
			int readed;
			while (true) {
				if (offset >= offsetOutputCommited) {
					if (bufOutput.position() > 0) {
						log.warn("WARN: autoflush forced");
						flushBuffer();
					}
				}
				bufInput.clear();
				readed = fcInput.position(offset).read(bufInput); // Read 1 sector
				if (readed < HEADER_LEN) { // short+int (6 bytes)
					return -1;
				}
				bufInput.flip();
				final int magicB1 = (bufInput.get() & 0xFF); 	// Header - Magic (short, 2 bytes, msb-first)
				final int magicB2 = (bufInput.get() & 0xFF); 	// Header - Magic (short, 2 bytes, lsb-last)
				if (alignBlocks && (magicB1 == MAGIC_PADDING)) {
					final int diffOffset = nextBlockBoundary(offset);
					if (diffOffset > 0) {
						// log.info("WARN: skipping " + diffOffset + "bytes to next block-boundary");
						offset += diffOffset;
						continue;
					}
				}
				final int magic = ((magicB1 << 8) | magicB2);
				if (magic != MAGIC) {
					log.error("MAGIC HEADER fake=" + Integer.toHexString(magic) + " expected="
							+ Integer.toHexString(MAGIC));
					return -1;
				}
				break;
			}
			//
			final int datalen = bufInput.getInt(); 	// Header - Data Size (int, 4 bytes)
			final int dataUnderFlow = (datalen - (readed - HEADER_LEN));
			int footer = -12345678;
			if (dataUnderFlow < 0) {
				footer = bufInput.get(datalen + HEADER_LEN); 	// Footer (byte)
			}
			bufInput.limit(Math.min(readed, datalen + HEADER_LEN));
			buf.put(bufInput);
			if (dataUnderFlow > 0) {
				buf.limit(datalen);
				int len = fcInput.read(buf);
				if (len < dataUnderFlow) {
					log.error("Unable to read payload readed=" + len + " expected=" + dataUnderFlow);
					return -1;
				}
			}
			if (dataUnderFlow >= 0) {
				// Read Footer (byte)
				bufInput.clear();
				bufInput.limit(FOOTER_LEN);
				if (fcInput.read(bufInput) < FOOTER_LEN) {
					return -1;
				}
				bufInput.flip();
				footer = bufInput.get();
			}
			if (footer != MAGIC_FOOT) {
				log.error("MAGIC FOOT fake=" + Integer.toHexString(footer) + " expected="
						+ Integer.toHexString(MAGIC_FOOT));
				return -1;
			}
			buf.flip();
			return (offset + HEADER_LEN + datalen + FOOTER_LEN);
		} catch (Exception e) {
			log.error("Exception in read(" + offset + ")", e);
		}
		return -1;
	}
public synchronized long read ( long offset , final ByteBuffer buf ) { if ( ! validState ) { throw new InvalidStateException ( ) ; } try { int readed ; while ( true ) { if ( offset >= offsetOutputCommited ) { if ( bufOutput . position ( ) > 0 ) { log . warn ( "WARN: autoflush forced" ) ; flushBuffer ( ) ; } } bufInput . clear ( ) ; readed = fcInput . position ( offset ) . read ( bufInput ) ; if ( readed < HEADER_LEN ) { return - 1 ; } bufInput . flip ( ) ; final int magicB1 = ( bufInput . get ( ) & 0xFF ) ; final int magicB2 = ( bufInput . get ( ) & 0xFF ) ; if ( alignBlocks && ( magicB1 == MAGIC_PADDING ) ) { final int diffOffset = nextBlockBoundary ( offset ) ; if ( diffOffset > 0 ) { offset += diffOffset ; continue ; } } final int magic = ( ( magicB1 << 8 ) | magicB2 ) ; if ( magic != MAGIC ) { log . error ( "MAGIC HEADER fake=" + Integer . toHexString ( magic ) + " expected=" + Integer . toHexString ( MAGIC ) ) ; return - 1 ; } break ; } final int datalen = bufInput . getInt ( ) ; final int dataUnderFlow = ( datalen - ( readed - HEADER_LEN ) ) ; int footer = - 12345678 ; if ( dataUnderFlow < 0 ) { footer = bufInput . get ( datalen + HEADER_LEN ) ; } bufInput . limit ( Math . min ( readed , datalen + HEADER_LEN ) ) ; buf . put ( bufInput ) ; if ( dataUnderFlow > 0 ) { buf . limit ( datalen ) ; int len = fcInput . read ( buf ) ; if ( len < dataUnderFlow ) { log . error ( "Unable to read payload readed=" + len + " expected=" + dataUnderFlow ) ; return - 1 ; } } if ( dataUnderFlow >= 0 ) { bufInput . clear ( ) ; bufInput . limit ( FOOTER_LEN ) ; if ( fcInput . read ( bufInput ) < FOOTER_LEN ) { return - 1 ; } bufInput . flip ( ) ; footer = bufInput . get ( ) ; } if ( footer != MAGIC_FOOT ) { log . error ( "MAGIC FOOT fake=" + Integer . toHexString ( footer ) + " expected=" + Integer . toHexString ( MAGIC_FOOT ) ) ; return - 1 ; } buf . flip ( ) ; return ( offset + HEADER_LEN + datalen + FOOTER_LEN ) ; } catch ( Exception e ) { log . error ( "Exception in read(" + offset + ")" , e ) ; } return - 1 ; }
Read block from file

@param offset of block
@param ByteBuffer
@return new offset (offset+headerlen+datalen+footer)