private static byte[] peekFirstNotEmptyBytes(InputStream inputStream,int length) throws IOException {
  final int markLength=32 * 1024;
  int bytesRead=0;
  int bytesFound=0;
  byte[] firstBytes=new byte[length];
  byte[] buffer=new byte[512];
  inputStream.mark(markLength);
  try {
    while (bytesRead < markLength) {
      int bytesMarked=IOUtils.read(inputStream,buffer);
      if (bytesMarked <= 0)       return null;
      bytesRead+=bytesMarked;
      for (int i=0; i < bytesMarked; i++) {
        byte b=buffer[i];
        boolean empty=IoUtils.isEmptyOrWhitespace(b) || IoUtils.isBomByte(b);
        if ((!empty || bytesFound > 0) && b != 0) {
          firstBytes[bytesFound]=b;
          bytesFound++;
        }
        if (bytesFound == firstBytes.length) {
          return firstBytes;
        }
      }
      if (bytesMarked < buffer.length) {
        return null;
      }
    }
  }
  finally {
    inputStream.reset();
  }
  return null;
}

private static byte [ ] peekFirstNotEmptyBytes ( InputStream inputStream , int length ) throws IOException { final int markLength = 32 * 1024 ; int bytesRead = 0 ; int bytesFound = 0 ; byte [ ] firstBytes = new byte [ length ] ; byte [ ] buffer = new byte [ 512 ] ; inputStream . mark ( markLength ) ; try { while ( bytesRead < markLength ) { int bytesMarked = IOUtils . read ( inputStream , buffer ) ; if ( bytesMarked <= 0 ) return null ; bytesRead += bytesMarked ; for ( int i = 0 ; i < bytesMarked ; i ++ ) { byte b = buffer [ i ] ; boolean empty = IoUtils . isEmptyOrWhitespace ( b ) || IoUtils . isBomByte ( b ) ; if ( ( ! empty || bytesFound > 0 ) && b != 0 ) { firstBytes [ bytesFound ] = b ; bytesFound ++ ; } if ( bytesFound == firstBytes . length ) { return firstBytes ; } } if ( bytesMarked < buffer . length ) { return null ; } } } finally { inputStream . reset ( ) ; } return null ; }
peeks first n non - empty bytes from a peeking stream .
