static void writeICC(ICC_Profile profile,ImageOutputStream ios) throws IOException {
  int LENGTH_LENGTH=2;
  final String ID="ICC_PROFILE";
  int ID_LENGTH=ID.length() + 1;
  int COUNTS_LENGTH=2;
  int MAX_ICC_CHUNK_SIZE=65535 - LENGTH_LENGTH - ID_LENGTH- COUNTS_LENGTH;
  byte[] data=profile.getData();
  int numChunks=data.length / MAX_ICC_CHUNK_SIZE;
  if ((data.length % MAX_ICC_CHUNK_SIZE) != 0) {
    numChunks++;
  }
  int chunkNum=1;
  int offset=0;
  for (int i=0; i < numChunks; i++) {
    int dataLength=Math.min(data.length - offset,MAX_ICC_CHUNK_SIZE);
    int segLength=dataLength + COUNTS_LENGTH + ID_LENGTH+ LENGTH_LENGTH;
    ios.write(0xff);
    ios.write(JPEG.APP2);
    MarkerSegment.write2bytes(ios,segLength);
    byte[] id=ID.getBytes("US-ASCII");
    ios.write(id);
    ios.write(0);
    ios.write(chunkNum++);
    ios.write(numChunks);
    ios.write(data,offset,dataLength);
    offset+=dataLength;
  }
}

static void writeICC ( ICC_Profile profile , ImageOutputStream ios ) throws IOException { int LENGTH_LENGTH = 2 ; final String ID = "ICC_PROFILE" ; int ID_LENGTH = ID . length ( ) + 1 ; int COUNTS_LENGTH = 2 ; int MAX_ICC_CHUNK_SIZE = 65535 - LENGTH_LENGTH - ID_LENGTH - COUNTS_LENGTH ; byte [ ] data = profile . getData ( ) ; int numChunks = data . length / MAX_ICC_CHUNK_SIZE ; if ( ( data . length % MAX_ICC_CHUNK_SIZE ) != 0 ) { numChunks ++ ; } int chunkNum = 1 ; int offset = 0 ; for ( int i = 0 ; i < numChunks ; i ++ ) { int dataLength = Math . min ( data . length - offset , MAX_ICC_CHUNK_SIZE ) ; int segLength = dataLength + COUNTS_LENGTH + ID_LENGTH + LENGTH_LENGTH ; ios . write ( 0xff ) ; ios . write ( JPEG . APP2 ) ; MarkerSegment . write2bytes ( ios , segLength ) ; byte [ ] id = ID . getBytes ( "US-ASCII" ) ; ios . write ( id ) ; ios . write ( 0 ) ; ios . write ( chunkNum ++ ) ; ios . write ( numChunks ) ; ios . write ( data , offset , dataLength ) ; offset += dataLength ; } }
write out the given profile to the stream , embedded in the necessary number of app2 segments , per the icc spec . this is the only mechanism for writing an icc profile to a stream .
