ByteConversion.intToByte( firstPage.getPageHeader().getDataLength() - 6, firstPage .getData(), 2); firstPage.setDirty(true); dataCache.add(firstPage, 3);
        public void write() throws IOException {            if (data == null) return;            int chunkSize = fileHeader.getWorkSize();            int remaining = data.length;            int current = 0;            long next = 0L;            byte[] chunk;            SinglePage page = firstPage;            page.getPageHeader().setDataLength(remaining);            SinglePage nextPage;            // walk through chain of pages            while (remaining > 0) {                if (remaining < chunkSize) chunkSize = remaining;                // copy next chunk of data to the page                System.arraycopy(data, current, page.getData(), 0, chunkSize);                if (page != firstPage)                        page.getPageHeader().setDataLength(chunkSize);                page.setDirty(true);                remaining -= chunkSize;                current += chunkSize;                next = page.getPageHeader().getNextInChain();                if (remaining > 0) {                    if (next > 0) {                        // load next page in chain                        nextPage = (SinglePage) getDataPage(next, false);                        dataCache.add(page);                        page = nextPage;                    } else {                        // add a new page to the chain                        nextPage = (SinglePage) createDataPage();                        nextPage.setData(new byte[fileHeader.getWorkSize()]);                        nextPage.getPageHeader().setNextInChain(0L);                        page.getPageHeader().setNextInChain(                                nextPage.getPageNum());                        dataCache.add(page);                        page = nextPage;                    }                } else {                    page.getPageHeader().setNextInChain(0L);                    if (page != firstPage) {                        page.setDirty(true);                        dataCache.add(page);                        firstPage.getPageHeader().setLastInChain(                                page.getPageNum());                    } else                        firstPage.getPageHeader().setLastInChain(0L);                    // adjust length field in first page                    ByteConversion.intToByte(                            firstPage.getPageHeader().getDataLength() - 6, firstPage                                    .getData(), 2);                    firstPage.setDirty(true);                    dataCache.add(firstPage, 3);                }            }            if (next > 0) {                // there are more pages in the chain:                // remove them                while (next > 0) {                    nextPage = (SinglePage) getDataPage(next, false);                    next = nextPage.getPageHeader().getNextInChain();                    nextPage.setDirty(true);                    nextPage.delete();                    dataCache.remove(nextPage);                }            }        }