private byte[] pageToByteArray(P page){
  try {
    if (page == null) {
      ByteArrayOutputStream baos=new ByteArrayOutputStream();
      ObjectOutputStream oos=new ObjectOutputStream(baos);
      oos.writeInt(EMPTY_PAGE);
      oos.close();
      baos.close();
      byte[] array=baos.toByteArray();
      byte[] result=new byte[pageSize];
      System.arraycopy(array,0,result,0,array.length);
      return result;
    }
 else {
      ByteArrayOutputStream baos=new ByteArrayOutputStream();
      ObjectOutputStream oos=new ObjectOutputStream(baos);
      oos.writeInt(FILLED_PAGE);
      page.writeExternal(oos);
      oos.close();
      baos.close();
      byte[] array=baos.toByteArray();
      if (array.length > this.pageSize) {
        throw new IllegalArgumentException("Size of page " + page + " is greater than specified"+ " pagesize: "+ array.length+ " > "+ pageSize);
      }
 else       if (array.length == this.pageSize) {
        return array;
      }
 else {
        byte[] result=new byte[pageSize];
        System.arraycopy(array,0,result,0,array.length);
        return result;
      }
    }
  }
 catch (  IOException e) {
    throw new RuntimeException("IOException occurred! ",e);
  }
}

private byte [ ] pageToByteArray ( P page ) { try { if ( page == null ) { ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ; ObjectOutputStream oos = new ObjectOutputStream ( baos ) ; oos . writeInt ( EMPTY_PAGE ) ; oos . close ( ) ; baos . close ( ) ; byte [ ] array = baos . toByteArray ( ) ; byte [ ] result = new byte [ pageSize ] ; System . arraycopy ( array , 0 , result , 0 , array . length ) ; return result ; } else { ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ; ObjectOutputStream oos = new ObjectOutputStream ( baos ) ; oos . writeInt ( FILLED_PAGE ) ; page . writeExternal ( oos ) ; oos . close ( ) ; baos . close ( ) ; byte [ ] array = baos . toByteArray ( ) ; if ( array . length > this . pageSize ) { throw new IllegalArgumentException ( "Size of page " + page + " is greater than specified" + " pagesize: " + array . length + " > " + pageSize ) ; } else if ( array . length == this . pageSize ) { return array ; } else { byte [ ] result = new byte [ pageSize ] ; System . arraycopy ( array , 0 , result , 0 , array . length ) ; return result ; } } } catch ( IOException e ) { throw new RuntimeException ( "IOException occurred! " , e ) ; } }
serializes an object into a byte array .
