private int addPart ( LogRecord logRecord , byte [ ] fillingBuffer , boolean completed , int offset , int partLength ) throws ObjectManagerException { final String methodName = "addPart" ; if ( Tracing . isAnyTracingEnabled ( ) && trace . isEntryEnabled ( ) ) trace . entry ( this , cclass , methodName , new Object [ ] { logRecord , fillingBuffer , new Boolean ( completed ) , new Integer ( offset ) , new Integer ( partLength ) } ) ; // Make the part header. byte [ ] partHeader = new byte [ partHeaderLength ] ; if ( completed ) partHeader [ 0 ] = PART_Last ; else if ( logRecord . atStart ( ) ) partHeader [ 0 ] = PART_First ; else partHeader [ 0 ] = PART_Middle ; partHeader [ 1 ] = logRecord . multiPartID ; partHeader [ 2 ] = ( byte ) ( partLength >>> 8 ) ; partHeader [ 3 ] = ( byte ) ( partLength >>> 0 ) ; // Place the part header into the logBuffer. // Calculate how much of the partHeader will fit in this page. int remainder = pageSize - offset % pageSize ; int length = Math . min ( remainder , partHeaderLength ) ; System . arraycopy ( partHeader , 0 , fillingBuffer , offset , length ) ; offset = offset + length ; // If we are now positioned at the first byte in a page, skip over it. if ( remainder <= partHeaderLength ) offset ++ ; // Have we wrapped the logBuffer? if ( offset >= fillingBuffer . length ) { fillingBuffer = logBuffer . buffer ; offset = 1 ; } // if (offset >= logBuffer.length). // Did the whole part header fit in the previous page, was there any overflow? if ( length < partHeaderLength ) { // Copy the piece covering the page boundary one byte further down the logBuffer. // We may copy the entire part header. System . arraycopy ( partHeader , length , fillingBuffer , offset , partHeaderLength - length ) ; offset = offset + partHeaderLength - length ; } // if (length < partHeaderLength). // Now copy in the the part of the LogRecord. int bytesToAdd = Math . min ( pageSize - ( offset % pageSize ) , partLength ) ; // Fill one page at a time. // If offset leaves us about to write on the first byte in a page then we will write // zero bytes first time round the loop and step over the sector byte. for ( ; ; ) { // See if we have stepped past the end of the log buffer. if ( offset >= fillingBuffer . length ) { fillingBuffer = logBuffer . buffer ; offset = 1 ; } // if (offset >= logBuffer.length). offset = logRecord . fillBuffer ( fillingBuffer , offset , bytesToAdd ) ; partLength = partLength - bytesToAdd ; if ( partLength == 0 ) break ; bytesToAdd = Math . min ( pageSize - 1 , partLength ) ; offset ++ ; // Step past the next sector bits. } // for (;;). if ( Tracing . isAnyTracingEnabled ( ) && trace . isEntryEnabled ( ) ) trace . exit ( this , cclass , methodName , new Object [ ] { new Integer ( offset ) } ) ; return offset ; }
Add the partHeader before the logRecord and then the part of the logRecord . If necessary wrap round the end of the log buffer back to the start .