HKXWriter { public void write(final HKXFile file) throws IOException, UnsupportedVersionError { HKXWriterConnector connector = new HKXWriterConnector(outputBB); HeaderData header = new HKXHeaderFactory().create(file); HKXSectionHandler sectionHandler = new HKXSectionHandler(header); SectionData classnames = new SectionData(); SectionData types = new SectionData(); SectionData data = new SectionData(); sectionHandler.init(HKXSectionHandler.CLASSNAME, classnames); HKXClassnamesHandler cnameHandler = new HKXClassnamesHandler(); ClassnamesData cnameData = cnameHandler.getClassnames(file); long classnamesEnd = connector.writeClassnames(classnames, cnameData); sectionHandler.fillCName(classnames, classnamesEnd); connector.writeHeader(header); connector.writeSection(header, HKXSectionHandler.CLASSNAME, classnames); sectionHandler.init(HKXSectionHandler.TYPES, types); connector.writeSection(header, HKXSectionHandler.TYPES, types); sectionHandler.init(HKXSectionHandler.DATA, data); PointerResolver resolver = new PointerResolver(); HKXDataHandler dataHandler = new HKXDataHandler(outputBB, cnameData, enumResolver); try { long endData = dataHandler.fillFile(data, file, resolver) - data.offset; data.data1 = endData % 0x10 == 0 ? endData : (1 + endData / 0x10) * 0x10; dataHandler.fillPointers(data, resolver); outputBB.flip(); connector.writeSection(header, HKXSectionHandler.DATA, data); } catch (ClassCastException e) { LoggerUtil.add(new WrongInputCastException(String.format(SBundle.getString("error.hkx.write.cast")), e)); } outputBB.position(0); if (outputFile.isPresent()) { try (RandomAccessFile out = new RandomAccessFile(outputFile.get(), "rw")) { byte[] bytes = new byte[outputBB.limit()]; outputBB.get(bytes); out.write(bytes); out.close(); } } } HKXWriter(final File outputFile, final HKXEnumResolver enumResolver);  HKXWriter(final File outputFile, final HKXEnumResolver enumResolver, final int bufferCapacity);  HKXWriter(final ByteBuffer outputBB, final HKXEnumResolver enumResolver); void write(final HKXFile file); static final int DEFAULT_BUFFER_CAPACITY; }
@Test public void testWriteDefaultFileToPhysicalFile() throws Exception { File outputFile = File.createTempFile(TEST_BASE_OUTPUT_NAME, ""); HKXWriter writer = new HKXWriter(outputFile, enumResolver); writer.write(file); assertArrayEquals( Files.toByteArray(outputFile), FileUtils.resourceToByteArray(TEST_BASE_REXOURCE_TARGET)); }
@Test public void testWriteDefaultFileToByteBuffer() throws Exception { ByteBuffer outputBuffer = ByteBuffer.allocate(10000); HKXWriter writer = new HKXWriter(outputBuffer, enumResolver); writer.write(file); byte[] outArray = new byte[outputBuffer.limit()]; outputBuffer.get(outArray); assertArrayEquals( outArray, FileUtils.resourceToByteArray(TEST_BASE_REXOURCE_TARGET)); }