├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── libraries │ ├── Maven__ant_ant_1_6_5.xml │ ├── Maven__asm_asm_3_1.xml │ ├── Maven__com_sun_jersey_jersey_core_1_8.xml │ ├── Maven__com_sun_jersey_jersey_json_1_8.xml │ ├── Maven__com_sun_jersey_jersey_server_1_8.xml │ ├── Maven__com_sun_xml_bind_jaxb_impl_2_2_3_1.xml │ ├── Maven__commons_beanutils_commons_beanutils_1_7_0.xml │ ├── Maven__commons_beanutils_commons_beanutils_core_1_8_0.xml │ ├── Maven__commons_cli_commons_cli_1_2.xml │ ├── Maven__commons_codec_commons_codec_1_4.xml │ ├── Maven__commons_collections_commons_collections_3_2_1.xml │ ├── Maven__commons_configuration_commons_configuration_1_6.xml │ ├── Maven__commons_digester_commons_digester_1_8.xml │ ├── Maven__commons_el_commons_el_1_0.xml │ ├── Maven__commons_httpclient_commons_httpclient_3_0_1.xml │ ├── Maven__commons_io_commons_io_2_1.xml │ ├── Maven__commons_io_commons_io_2_4.xml │ ├── Maven__commons_lang_commons_lang_2_4.xml │ ├── Maven__commons_logging_commons_logging_1_0_3.xml │ ├── Maven__commons_net_commons_net_1_4_1.xml │ ├── Maven__hsqldb_hsqldb_1_8_0_10.xml │ ├── Maven__javax_activation_activation_1_1.xml │ ├── Maven__javax_xml_bind_jaxb_api_2_2_2.xml │ ├── Maven__javax_xml_stream_stax_api_1_0_2.xml │ ├── Maven__junit_junit_4_11.xml │ ├── Maven__log4j_log4j_1_2_17.xml │ ├── Maven__net_java_dev_jets3t_jets3t_0_6_1.xml │ ├── Maven__org_apache_commons_commons_math_2_1.xml │ ├── Maven__org_apache_hadoop_hadoop_core_1_1_2.xml │ ├── Maven__org_codehaus_jackson_jackson_core_asl_1_7_1.xml │ ├── Maven__org_codehaus_jackson_jackson_jaxrs_1_7_1.xml │ ├── Maven__org_codehaus_jackson_jackson_mapper_asl_1_8_8.xml │ ├── Maven__org_codehaus_jackson_jackson_xc_1_7_1.xml │ ├── Maven__org_codehaus_jettison_jettison_1_1.xml │ ├── Maven__org_eclipse_jdt_core_3_1_1.xml │ ├── Maven__org_hamcrest_hamcrest_core_1_3.xml │ ├── Maven__org_mortbay_jetty_jetty_6_1_26.xml │ ├── Maven__org_mortbay_jetty_jetty_util_6_1_26.xml │ ├── Maven__org_mortbay_jetty_jsp_2_1_6_1_14.xml │ ├── Maven__org_mortbay_jetty_jsp_api_2_1_6_1_14.xml │ ├── Maven__org_mortbay_jetty_servlet_api_2_5_20081211.xml │ ├── Maven__org_mortbay_jetty_servlet_api_2_5_6_1_14.xml │ ├── Maven__org_slf4j_slf4j_api_1_7_7.xml │ ├── Maven__org_slf4j_slf4j_simple_1_7_7.xml │ ├── Maven__oro_oro_2_0_8.xml │ ├── Maven__stax_stax_api_1_0_1.xml │ ├── Maven__tomcat_jasper_compiler_5_5_12.xml │ ├── Maven__tomcat_jasper_runtime_5_5_12.xml │ └── Maven__xmlenc_xmlenc_0_52.xml ├── misc.xml ├── modules.xml ├── scopes │ └── scope_settings.xml ├── vcs.xml └── workspace.xml ├── README.md ├── badiff-cli ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── badiff-cli.iml ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── badiff │ │ └── cli │ │ ├── Arguments.java │ │ ├── DiffMain.java │ │ ├── Main.java │ │ ├── PatchMain.java │ │ └── io │ │ ├── ListenableRandomInput.java │ │ ├── ProgressInputListener.java │ │ └── RandomInputListener.java │ └── resources │ └── org │ └── badiff │ └── cli │ ├── diff_args.properties │ └── patch_args.properties ├── badiff-examples ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── badiff-examples.iml ├── pom.xml └── src │ └── main │ └── java │ └── org │ └── badiff │ └── exa │ ├── ComputeFileDiffs.java │ ├── LargeFiles.java │ └── UsingPipelines.java ├── badiff-hadoop ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── badiff-hadoop aws.launch ├── badiff-hadoop cdh4.launch ├── badiff-hadoop.iml ├── pom.xml ├── seed.txt └── src │ └── main │ └── java │ └── org │ └── badiff │ └── hadoop │ └── aig │ ├── AdjustableInertialGraphJob.java │ ├── CsvGeneticParams.java │ ├── GeneticCounters.java │ ├── GeneticInputFormat.java │ ├── GeneticMapper.java │ ├── GeneticOutputFormat.java │ ├── GeneticReducer.java │ └── InputBytesGenerator.java ├── badiff-kryo ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── pom.xml └── src │ ├── main │ └── java │ │ └── org │ │ └── badiff │ │ └── kryo │ │ ├── BadiffSerializer.java │ │ └── KryoSerialization.java │ └── test │ └── java │ └── org │ └── badiff │ └── kryo │ └── BadiffSerializerTest.java ├── badiff-parent ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ └── org.eclipse.m2e.core.prefs ├── badiff-parent.iml ├── header.java ├── license.txt └── pom.xml └── badiff ├── .classpath ├── .gitignore ├── .project ├── .settings ├── org.eclipse.core.resources.prefs ├── org.eclipse.jdt.core.prefs └── org.eclipse.m2e.core.prefs ├── badiff.iml ├── pom.xml └── src ├── main └── java │ └── org │ └── badiff │ ├── Applyable.java │ ├── BadiffCli.java │ ├── ByteArrayDiffs.java │ ├── Diff.java │ ├── FileDiffs.java │ ├── MemoryDiffs.java │ ├── Op.java │ ├── Queueable.java │ ├── Storeable.java │ ├── alg │ ├── AdjustableInertialGraph.java │ ├── EditGraph.java │ ├── Graph.java │ ├── GraphFactory.java │ ├── InertialGraph.java │ ├── MinNextEditGraph.java │ └── package-info.java │ ├── fmt │ ├── BadiffFormat.java │ ├── GdiffFormat.java │ ├── InputFormat.java │ ├── OutputFormat.java │ └── package-info.java │ ├── imp │ ├── BadiffFileDiff.java │ ├── FileDiff.java │ ├── MemoryDiff.java │ ├── StreamQueueable.java │ ├── StreamStoreable.java │ └── package-info.java │ ├── io │ ├── ByteBufferRandomInput.java │ ├── DataInputInputStream.java │ ├── DataOutputOutputStream.java │ ├── DefaultSerialization.java │ ├── EmptyInputStream.java │ ├── FileRandomInput.java │ ├── GraphContext.java │ ├── NonClosingInputStream.java │ ├── NonClosingOutputStream.java │ ├── NoopOutputStream.java │ ├── PushbufferInputStream.java │ ├── Random.java │ ├── RandomInput.java │ ├── RandomInputStream.java │ ├── RuntimeIOException.java │ ├── Serialization.java │ ├── Serialized.java │ ├── SmallNumberSerialization.java │ ├── StreamRandomInput.java │ └── package-info.java │ ├── p │ ├── Pipe.java │ ├── Pipeline.java │ ├── Pipes.java │ └── package-info.java │ ├── package-info.java │ ├── q │ ├── BufferChunkingOpQueue.java │ ├── ChainOpQueue.java │ ├── ChunkingOpQueue.java │ ├── CoalescingOpQueue.java │ ├── CompactingOpQueue.java │ ├── FilterOpQueue.java │ ├── FutureOpQueue.java │ ├── GraphOpQueue.java │ ├── ListOpQueue.java │ ├── OneWayOpQueue.java │ ├── OpQueue.java │ ├── ParallelGraphOpQueue.java │ ├── PumpingOpQueue.java │ ├── RandomChunkingOpQueue.java │ ├── ReferenceOpQueue.java │ ├── ReplaceOpQueue.java │ ├── RewindingOpQueue.java │ ├── StreamChunkingOpQueue.java │ ├── UnchunkingOpQueue.java │ ├── UndoOpQueue.java │ └── package-info.java │ └── util │ ├── Diffs.java │ ├── Digests.java │ ├── Integers.java │ ├── Serials.java │ ├── Streams.java │ └── package-info.java └── test ├── java └── org │ └── badiff │ ├── ByteArrayDiffsTest.java │ ├── DiffsTest.java │ ├── FileDiffsTest.java │ ├── alg │ ├── AdjustableInertialGraphBehaviorTest.java │ ├── BehaviorTestDataGenerator.java │ ├── EditGraphBehaviorTest.java │ ├── InertialGraphBehaviorTest.java │ ├── IntertialGraphTest.java │ └── MinNextEditGraphTest.java │ ├── imp │ └── BadiffFileDiffTest.java │ ├── io │ ├── DefaultFileDiffTest.java │ └── DefaultSerializationTest.java │ ├── q │ ├── GraphOpQueueTest.java │ ├── ParallelGraphOpQueueTest.java │ └── RewindingOpQueueTest.java │ └── util │ ├── Integers2Test.java │ └── IntegersTest.java └── resources └── org └── badiff └── alg ├── ad_inertial.txt ├── edit.txt └── inertial.txt /.idea/.name: -------------------------------------------------------------------------------- 1 | badiff -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__ant_ant_1_6_5.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__asm_asm_3_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__com_sun_jersey_jersey_core_1_8.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__com_sun_jersey_jersey_json_1_8.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__com_sun_jersey_jersey_server_1_8.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__com_sun_xml_bind_jaxb_impl_2_2_3_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_beanutils_commons_beanutils_1_7_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_beanutils_commons_beanutils_core_1_8_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_cli_commons_cli_1_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_codec_commons_codec_1_4.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_collections_commons_collections_3_2_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_configuration_commons_configuration_1_6.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_digester_commons_digester_1_8.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_el_commons_el_1_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_httpclient_commons_httpclient_3_0_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_io_commons_io_2_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_io_commons_io_2_4.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_lang_commons_lang_2_4.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_logging_commons_logging_1_0_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_net_commons_net_1_4_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__hsqldb_hsqldb_1_8_0_10.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__javax_activation_activation_1_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__javax_xml_bind_jaxb_api_2_2_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__javax_xml_stream_stax_api_1_0_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__junit_junit_4_11.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__log4j_log4j_1_2_17.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__net_java_dev_jets3t_jets3t_0_6_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_commons_commons_math_2_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_hadoop_hadoop_core_1_1_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_codehaus_jackson_jackson_core_asl_1_7_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_codehaus_jackson_jackson_jaxrs_1_7_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_codehaus_jackson_jackson_mapper_asl_1_8_8.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_codehaus_jackson_jackson_xc_1_7_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_codehaus_jettison_jettison_1_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_eclipse_jdt_core_3_1_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_mortbay_jetty_jetty_6_1_26.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_mortbay_jetty_jetty_util_6_1_26.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_mortbay_jetty_jsp_2_1_6_1_14.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_mortbay_jetty_jsp_api_2_1_6_1_14.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_mortbay_jetty_servlet_api_2_5_20081211.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_mortbay_jetty_servlet_api_2_5_6_1_14.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_slf4j_slf4j_api_1_7_7.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_slf4j_slf4j_simple_1_7_7.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__oro_oro_2_0_8.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__stax_stax_api_1_0_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__tomcat_jasper_compiler_5_5_12.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__tomcat_jasper_runtime_5_5_12.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__xmlenc_xmlenc_0_52.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /.idea/scopes/scope_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # badiff 2 | 3 | A pure-java byte-level diffing library for working with large inputs, taking advantage of parallel processing during optimization. 4 | 5 | ## Development & Issue Tracking 6 | [This github repo](https://github.com/org-badiff/badiff) is a mirror of the definitive repo hosted on [stash.robindps.com](http://stash.robindps.com/projects/BDF/repos/badiff/browse). 7 | 8 | The primary issue tracker is at [jira.robindps.com](http://jira.robindps.com/browse/BDF/?selectedTab=com.atlassian.jira.jira-projects-plugin:summary-panel). Issues created via [github issue tracker](https://github.com/org-badiff/badiff/issues) will be imported into the JIRA, but responded to on github. 9 | 10 | badiff has no external dependencies, but requires Java 1.6 or higher. badiff is released under a BSD license. 11 | 12 | ## Find it on maven: 13 | 14 | ```xml 15 | 16 | org.badiff 17 | badiff 18 | 1.2.1 19 | 20 | ``` 21 | ## Example usage 22 | 23 | 24 | Diffing two files, and storing the result as a file 25 | 26 | ```java 27 | File orig, target, diff; 28 | JFileChooser chooser = new JFileChooser(); 29 | 30 | // prompt the user for the files to diff and file to save to 31 | if(chooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) 32 | return; 33 | orig = chooser.getSelectedFile(); 34 | 35 | if(chooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) 36 | return; 37 | target = chooser.getSelectedFile(); 38 | 39 | if(chooser.showSaveDialog(null) != JFileChooser.APPROVE_OPTION) 40 | return; 41 | diff = chooser.getSelectedFile(); 42 | 43 | // do the diff 44 | FileDiff computed = FileDiffs.diff(orig, target); 45 | 46 | // move the generated diff file to the dest file location 47 | diff.delete(); 48 | computed.renameTo(diff); 49 | ``` 50 | 51 | Diffing two byte arrays and storing the result as a byte array 52 | 53 | ```java 54 | String orig = "Hello world!"; 55 | String target = "Hellish cruel world!"; 56 | 57 | byte[] diff = ByteArrayDiffs.diff(orig.getBytes(), target.getBytes()); // bidirectional diff 58 | byte[] udiff = ByteArrayDiffs.udiff(orig.getBytes(), target.getBytes()); // unidirectional diff 59 | ``` 60 | ## Algorithm 61 | 62 | badiff uses the O(ND) algorithm described in this paper: http://www.xmailserver.org/diff2.pdf‎ 63 | 64 | By default, input is run through the diffing graph in chunks of 1KB. Increasing chunk size can potentially decrease the size of the resulting diff, but the cost grows with the square of the increase; a chunk size of 2KB takes 4 times longer to compute than a chunk size of 1KB. After chunked graphing is completed the resultant edit list is post-processed to remove some obvious artifacts of chunking, such as pairs of (INSERT,DELETE) operations with identical data that can potentially occur at chunk boundaries. 65 | 66 | Chunked graphing is done in parallel by a thread pool sized to the number of available processor cores. 67 | 68 | Post-processing is done serially rather than in parallel, so for large input sizes (>50MB) it may be advantageous to skip the post-processing. 69 | 70 | ## Copyright 71 | 72 | badiff is copyrighted ©2013 Robin Kirkman 73 | -------------------------------------------------------------------------------- /badiff-cli/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /badiff-cli/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /badiff-cli/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | badiff-cli 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /badiff-cli/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/main/resources=UTF-8 4 | encoding/=UTF-8 5 | -------------------------------------------------------------------------------- /badiff-cli/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 3 | org.eclipse.jdt.core.compiler.compliance=1.6 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.6 6 | -------------------------------------------------------------------------------- /badiff-cli/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /badiff-cli/badiff-cli.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /badiff-cli/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | badiff-cli 5 | 6 | org.badiff 7 | badiff-parent 8 | 1.2.1 9 | ../badiff-parent 10 | 11 | 12 | 13 | 14 | 15 | org.apache.maven.plugins 16 | maven-deploy-plugin 17 | 2.8.1 18 | 19 | true 20 | 21 | 22 | 23 | org.apache.maven.plugins 24 | maven-shade-plugin 25 | 2.1 26 | 27 | 28 | package 29 | 30 | shade 31 | 32 | 33 | false 34 | true 35 | 36 | 38 | org.badiff.cli.Main 39 | 40 | 41 | 42 | 43 | *:* 44 | 45 | META-INF/*.SF 46 | META-INF/*.DSA 47 | META-INF/*.RSA 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | org.badiff 61 | badiff 62 | 63 | 64 | commons-cli 65 | commons-cli 66 | 1.2 67 | 68 | 69 | -------------------------------------------------------------------------------- /badiff-cli/src/main/java/org/badiff/cli/Arguments.java: -------------------------------------------------------------------------------- 1 | package org.badiff.cli; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.util.Properties; 6 | 7 | import org.apache.commons.cli.CommandLine; 8 | import org.apache.commons.cli.Option; 9 | import org.apache.commons.cli.Options; 10 | import org.apache.commons.cli.ParseException; 11 | import org.apache.commons.cli.PosixParser; 12 | 13 | public abstract class Arguments { 14 | 15 | public static class DiffArguments extends AbstractArguments { 16 | private static final long serialVersionUID = 0; 17 | 18 | public static final String PIPELINE = "pipeline"; 19 | public static final String OUTPUT = "output"; 20 | public static final String BEFORE = "before"; 21 | public static final String AFTER = "after"; 22 | public static final String VERBOSE = "verbose"; 23 | public static final String CHUNK = "chunk"; 24 | 25 | private DiffArguments() { 26 | super("diff_args.properties"); 27 | opt("p", PIPELINE, true, "diff pipeline code to use"); 28 | req("o", OUTPUT, true, "output file"); 29 | req("1", BEFORE, true, "original file for comparison"); 30 | req("2", AFTER, true, "target file for comparison"); 31 | opt("v", VERBOSE, false, "be verbose during comparison"); 32 | opt("c", CHUNK, true, "chunk size when diffing"); 33 | } 34 | } 35 | 36 | public static class PatchArguments extends AbstractArguments { 37 | private static final long serialVersionUID = 0; 38 | 39 | public static final String PATCH = "patch"; 40 | public static final String BEFORE = "before"; 41 | public static final String AFTER = "after"; 42 | public static final String VERBOSE = "verbose"; 43 | 44 | private PatchArguments() { 45 | super("patch_args.properties"); 46 | req("p", PATCH, true, "output file"); 47 | req("1", BEFORE, true, "original file for comparison"); 48 | req("o", AFTER, true, "target file for comparison"); 49 | opt("v", VERBOSE, false, "be verbose during comparison"); 50 | } 51 | 52 | } 53 | 54 | public static final DiffArguments DIFF = new DiffArguments(); 55 | 56 | public static final PatchArguments PATCH = new PatchArguments(); 57 | 58 | public static abstract class AbstractArguments extends Options { 59 | private static final long serialVersionUID = 0; 60 | 61 | protected Properties defaults; 62 | 63 | public AbstractArguments(String rsrc) { 64 | defaults = new Properties(); 65 | try { 66 | InputStream in = Arguments.class.getResourceAsStream(rsrc); 67 | try { 68 | defaults.load(in); 69 | } finally { 70 | in.close(); 71 | } 72 | } catch(IOException ioe) { 73 | throw new RuntimeException(ioe); 74 | } 75 | } 76 | 77 | public CommandLine parse(String... args) throws ParseException { 78 | return new PosixParser().parse(this, args, defaults); 79 | } 80 | 81 | protected void opt(String opt, String longOpt, boolean hasArg, String desc) { 82 | Option o = new Option(opt, longOpt, hasArg, desc); 83 | addOption(o); 84 | } 85 | 86 | protected void req(String opt, String longOpt, boolean hasArg, String desc) { 87 | Option o = new Option(opt, longOpt, hasArg, desc); 88 | o.setRequired(true); 89 | addOption(o); 90 | } 91 | 92 | public Properties getDefaults() { 93 | return defaults; 94 | } 95 | } 96 | 97 | private Arguments() {} 98 | } 99 | -------------------------------------------------------------------------------- /badiff-cli/src/main/java/org/badiff/cli/DiffMain.java: -------------------------------------------------------------------------------- 1 | package org.badiff.cli; 2 | 3 | import static org.badiff.cli.Arguments.DiffArguments.AFTER; 4 | import static org.badiff.cli.Arguments.DiffArguments.BEFORE; 5 | import static org.badiff.cli.Arguments.DiffArguments.OUTPUT; 6 | import static org.badiff.cli.Arguments.DiffArguments.PIPELINE; 7 | import static org.badiff.cli.Arguments.DiffArguments.VERBOSE; 8 | import static org.badiff.cli.Arguments.DiffArguments.CHUNK; 9 | 10 | import java.io.DataOutputStream; 11 | import java.io.File; 12 | import java.io.FileOutputStream; 13 | 14 | import org.apache.commons.cli.CommandLine; 15 | import org.badiff.alg.GraphFactory; 16 | import org.badiff.cli.io.ListenableRandomInput; 17 | import org.badiff.cli.io.ProgressInputListener; 18 | import org.badiff.imp.BadiffFileDiff; 19 | import org.badiff.imp.BadiffFileDiff.Header; 20 | import org.badiff.imp.FileDiff; 21 | import org.badiff.io.DefaultSerialization; 22 | import org.badiff.io.FileRandomInput; 23 | import org.badiff.p.Pipeline; 24 | import org.badiff.q.OpQueue; 25 | import org.badiff.q.ParallelGraphOpQueue; 26 | import org.badiff.q.RandomChunkingOpQueue; 27 | import org.badiff.util.Digests; 28 | 29 | public class DiffMain { 30 | 31 | public static void main(String[] args) throws Exception { 32 | CommandLine cli = Arguments.DIFF.parse(args); 33 | 34 | File orig = new File(cli.getOptionValue(BEFORE)); 35 | File target = new File(cli.getOptionValue(AFTER)); 36 | BadiffFileDiff output = new BadiffFileDiff(cli.getOptionValue(OUTPUT)); 37 | String pipeline = cli.getOptionValue(PIPELINE); 38 | int chunk = Integer.parseInt(cli.getOptionValue(CHUNK)); 39 | 40 | ListenableRandomInput oin = new ListenableRandomInput(new FileRandomInput(orig)); 41 | ListenableRandomInput tin = new ListenableRandomInput(new FileRandomInput(target)); 42 | 43 | if(cli.hasOption(VERBOSE)) 44 | new ProgressInputListener(oin, tin); 45 | 46 | byte[] preHash = Digests.digest(orig, Digests.defaultDigest()); 47 | byte[] postHash = Digests.digest(target, Digests.defaultDigest()); 48 | 49 | FileDiff tmp = new FileDiff(output.getParentFile(), output.getName() + ".tmp"); 50 | 51 | OpQueue q; 52 | q = new RandomChunkingOpQueue(oin, tin, chunk); 53 | 54 | q = new ParallelGraphOpQueue(q, Runtime.getRuntime().availableProcessors(), chunk, GraphFactory.INERTIAL_GRAPH); 55 | 56 | q = new Pipeline(q).into(pipeline).outlet(); 57 | 58 | tmp.store(q); 59 | 60 | Header h = new Header(); 61 | 62 | Header.Optional opt = h.new Optional(); 63 | h.setOptional(opt); 64 | opt.setHashAlgorithm(Digests.defaultDigest().getAlgorithm()); 65 | opt.setPreHash(preHash); 66 | opt.setPostHash(postHash); 67 | 68 | DataOutputStream self = new DataOutputStream(new FileOutputStream(output)); 69 | BadiffFileDiff.store(self, DefaultSerialization.newInstance(), h, tmp.queue()); 70 | self.close(); 71 | 72 | tmp.delete(); 73 | 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /badiff-cli/src/main/java/org/badiff/cli/Main.java: -------------------------------------------------------------------------------- 1 | package org.badiff.cli; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) throws Exception { 8 | if(args.length == 0) { 9 | help(args); 10 | System.exit(0); 11 | } 12 | 13 | if("help".equals(args[0])) 14 | help(Arrays.copyOfRange(args, 1, args.length)); 15 | else if("diff".equals(args[0])) 16 | DiffMain.main(Arrays.copyOfRange(args, 1, args.length)); 17 | else if("patch".equals(args[0])) 18 | PatchMain.main(Arrays.copyOfRange(args, 1, args.length)); 19 | else 20 | help(Arrays.copyOfRange(args, 1, args.length)); 21 | } 22 | 23 | private static void help(String[] args) { 24 | if(args.length == 0) { 25 | p("badiff-cli command [ARGS...]"); 26 | p("Valid commands:"); 27 | p("\thelp [command]"); 28 | p("\tdiff -1 orig -2 target -o patch [-v] [-c chunk_size]"); 29 | p("\tpatch -1 orig -p patch -o target [-v]"); 30 | } else if("help".equals(args[0])) { 31 | p("badiff-cli help [command]"); 32 | p("Shows help for all commands, or the specific command if specified"); 33 | } else if("diff".equals(args[0])) { 34 | p("badiff-cli diff -1 orig -2 target -o patch [-v]"); 35 | p("\t-1\tThe original file"); 36 | p("\t-2\tThe modified file"); 37 | p("\t-o\tThe patch file to generate (created)"); 38 | p("\t-v\tBe verbose"); 39 | p("\t-c\tChunk size when diffing"); 40 | } else if("patch".equals(args[0])) { 41 | p("badiff-cli patch -1 orig -p patch -o target [-v]"); 42 | p("\t-1\tThe original file"); 43 | p("\t-o\tThe modified file (created)"); 44 | p("\t-p\tThe patch file"); 45 | p("\t-v\tBe verbose"); 46 | } else { 47 | p("Unrecognized command:" + args[0]); 48 | } 49 | } 50 | 51 | private static void p(Object s) { 52 | System.out.println(s); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /badiff-cli/src/main/java/org/badiff/cli/PatchMain.java: -------------------------------------------------------------------------------- 1 | package org.badiff.cli; 2 | 3 | import static org.badiff.cli.Arguments.PatchArguments.AFTER; 4 | import static org.badiff.cli.Arguments.PatchArguments.BEFORE; 5 | import static org.badiff.cli.Arguments.PatchArguments.PATCH; 6 | 7 | import java.io.File; 8 | 9 | import org.apache.commons.cli.CommandLine; 10 | import org.badiff.imp.BadiffFileDiff; 11 | import org.badiff.util.Diffs; 12 | 13 | public class PatchMain { 14 | 15 | public static void main(String[] args) throws Exception { 16 | CommandLine cli = Arguments.PATCH.parse(args); 17 | 18 | File orig = new File(cli.getOptionValue(BEFORE)); 19 | File target = new File(cli.getOptionValue(AFTER)); 20 | BadiffFileDiff patch = new BadiffFileDiff(cli.getOptionValue(PATCH)); 21 | 22 | Diffs.apply(patch, orig, target); 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /badiff-cli/src/main/java/org/badiff/cli/io/ProgressInputListener.java: -------------------------------------------------------------------------------- 1 | package org.badiff.cli.io; 2 | 3 | 4 | public class ProgressInputListener implements RandomInputListener { 5 | 6 | protected ListenableRandomInput[] in; 7 | protected long[] pos; 8 | 9 | public ProgressInputListener(ListenableRandomInput... in) { 10 | this.in = in; 11 | pos = new long[in.length]; 12 | for(int i = 0; i < in.length; i++) { 13 | in[i].addListener(this); 14 | } 15 | status(); 16 | } 17 | 18 | protected int pos(ListenableRandomInput input) { 19 | for(int i = 0; i < in.length; i++) 20 | if(in[i] == input) 21 | return i; 22 | return -1; 23 | } 24 | 25 | @Override 26 | public void moved(ListenableRandomInput thiz) { 27 | int i = pos(thiz); 28 | long step = thiz.last() / 10; 29 | if(thiz.position() - pos[i] > step || thiz.position() == thiz.last() && pos[i] < thiz.position()) { 30 | pos[i] = thiz.position(); 31 | status(); 32 | } 33 | } 34 | 35 | public void status() { 36 | StringBuilder sb = new StringBuilder(); 37 | String sep = ""; 38 | for(int i = 0; i < in.length; i++) { 39 | sb.append(sep); 40 | sb.append((100 * in[i].position() / in[i].last()) + "%"); 41 | sep = " "; 42 | } 43 | System.out.println(sb); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /badiff-cli/src/main/java/org/badiff/cli/io/RandomInputListener.java: -------------------------------------------------------------------------------- 1 | package org.badiff.cli.io; 2 | 3 | 4 | public interface RandomInputListener { 5 | public void moved(ListenableRandomInput thiz); 6 | } 7 | -------------------------------------------------------------------------------- /badiff-cli/src/main/resources/org/badiff/cli/diff_args.properties: -------------------------------------------------------------------------------- 1 | pipeline=ccro 2 | chunk=2048 3 | -------------------------------------------------------------------------------- /badiff-cli/src/main/resources/org/badiff/cli/patch_args.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/org-badiff/badiff/e892699e457ba307269243bf5cb960ddb1058770/badiff-cli/src/main/resources/org/badiff/cli/patch_args.properties -------------------------------------------------------------------------------- /badiff-examples/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /badiff-examples/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /badiff-examples/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | badiff-examples 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /badiff-examples/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding/=UTF-8 4 | -------------------------------------------------------------------------------- /badiff-examples/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 3 | org.eclipse.jdt.core.compiler.compliance=1.6 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.6 6 | -------------------------------------------------------------------------------- /badiff-examples/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /badiff-examples/badiff-examples.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /badiff-examples/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | badiff-examples 5 | 6 | org.badiff 7 | badiff-parent 8 | 1.2.1 9 | ../badiff-parent 10 | 11 | 12 | 13 | org.badiff 14 | badiff 15 | 16 | 17 | 18 | 19 | 20 | 21 | org.apache.maven.plugins 22 | maven-deploy-plugin 23 | 2.7 24 | 25 | true 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /badiff-examples/src/main/java/org/badiff/exa/ComputeFileDiffs.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.exa; 31 | 32 | import java.io.File; 33 | 34 | import javax.swing.JFileChooser; 35 | 36 | import org.badiff.FileDiffs; 37 | import org.badiff.imp.FileDiff; 38 | 39 | public class ComputeFileDiffs { 40 | 41 | public static void main(String[] args) throws Exception { 42 | File orig, target, diff; 43 | JFileChooser chooser = new JFileChooser(); 44 | 45 | if(chooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) 46 | return; 47 | orig = chooser.getSelectedFile(); 48 | 49 | if(chooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) 50 | return; 51 | target = chooser.getSelectedFile(); 52 | 53 | if(chooser.showSaveDialog(null) != JFileChooser.APPROVE_OPTION) 54 | return; 55 | diff = chooser.getSelectedFile(); 56 | 57 | FileDiff computed = FileDiffs.diff(orig, target); 58 | 59 | diff.delete(); 60 | computed.renameTo(diff); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /badiff-examples/src/main/java/org/badiff/exa/UsingPipelines.java: -------------------------------------------------------------------------------- 1 | package org.badiff.exa; 2 | 3 | import org.badiff.imp.MemoryDiff; 4 | import org.badiff.io.DefaultSerialization; 5 | import org.badiff.p.Pipeline; 6 | import org.badiff.p.Pipes; 7 | import org.badiff.q.OpQueue; 8 | import org.badiff.q.ReplaceOpQueue; 9 | import org.badiff.util.Serials; 10 | 11 | public class UsingPipelines { 12 | /** 13 | * Custom diff that uses pipelines 14 | * @param orig The original byte sequence 15 | * @param target The target byte sequence 16 | * @return The diff, as a byte sequence 17 | */ 18 | public static byte[] finelyTunedDiff(byte[] orig, byte[] target) { 19 | // Create an OpQueue with only tqo operations: delete orig and insert target 20 | OpQueue q = new ReplaceOpQueue(orig, target); 21 | 22 | // Use a pipeline to construct an OpQueue chain for the diff 23 | /* 24 | * NOTE: 25 | * Just constructing a pipeline will not cause the OpQueue chain 26 | * to start processing. 27 | */ 28 | 29 | Pipeline pipeline = Pipes.SPLIT.from(q) // Split it into chunks processable by the edit graphs 30 | .into(Pipes.GRAPH) // Single-threaded edit graphing 31 | .into(Pipes.UNCHUNK) // Reorder inserts and deletes where possible 32 | .into(Pipes.COMPACT) // Combine individual but sequential operations into single operations 33 | .into(Pipes.COALESS) // Coalesce identical inserts and deletes into NEXT 34 | .into(Pipes.ONE_WAY); // Strip reversable data 35 | 36 | // Return a new diff stored in memory 37 | /* 38 | * NOTE: 39 | * MemoryDiff's constructor will pull all the operations off the pipeline, 40 | * and so the diff will be computed here. 41 | */ 42 | MemoryDiff diff = new MemoryDiff(pipeline.outlet()); 43 | 44 | // Serialize the diff to a byte[] 45 | return Serials.serialize(DefaultSerialization.newInstance(), MemoryDiff.class, diff); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /badiff-hadoop/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /badiff-hadoop/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /dist 3 | -------------------------------------------------------------------------------- /badiff-hadoop/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | badiff-hadoop 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /badiff-hadoop/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding/=UTF-8 4 | -------------------------------------------------------------------------------- /badiff-hadoop/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 3 | org.eclipse.jdt.core.compiler.compliance=1.6 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.6 6 | -------------------------------------------------------------------------------- /badiff-hadoop/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /badiff-hadoop/badiff-hadoop aws.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /badiff-hadoop/badiff-hadoop cdh4.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /badiff-hadoop/seed.txt: -------------------------------------------------------------------------------- 1 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 2 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 3 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 4 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 5 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 6 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 7 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 8 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 9 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 10 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 11 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 12 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 13 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 14 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 15 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 16 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 17 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 18 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 19 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 20 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 21 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 22 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 23 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 24 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 25 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 26 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 27 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 28 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 29 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 30 | 0, 0,2,3,1,0,0,3,1,0,2,1,1,0,2,3,0 31 | -------------------------------------------------------------------------------- /badiff-hadoop/src/main/java/org/badiff/hadoop/aig/GeneticCounters.java: -------------------------------------------------------------------------------- 1 | package org.badiff.hadoop.aig; 2 | 3 | public enum GeneticCounters { 4 | GAMES_PLAYED, 5 | CLOCK_TICKS, 6 | LINES_CLEARED, 7 | } 8 | -------------------------------------------------------------------------------- /badiff-hadoop/src/main/java/org/badiff/hadoop/aig/GeneticOutputFormat.java: -------------------------------------------------------------------------------- 1 | package org.badiff.hadoop.aig; 2 | 3 | import org.apache.hadoop.io.NullWritable; 4 | import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; 5 | 6 | public class GeneticOutputFormat extends TextOutputFormat { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /badiff-hadoop/src/main/java/org/badiff/hadoop/aig/GeneticReducer.java: -------------------------------------------------------------------------------- 1 | package org.badiff.hadoop.aig; 2 | 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | import java.util.Collections; 6 | import java.util.List; 7 | import java.util.PriorityQueue; 8 | 9 | import org.apache.hadoop.io.FloatWritable; 10 | import org.apache.hadoop.io.NullWritable; 11 | import org.apache.hadoop.mapreduce.Job; 12 | import org.apache.hadoop.mapreduce.Reducer; 13 | import org.apache.log4j.Logger; 14 | 15 | public class GeneticReducer extends Reducer { 16 | 17 | private static final String PREFIX = GeneticReducer.class.getName() + "."; 18 | private static final String SURVIVORS = PREFIX + "survivors"; 19 | 20 | @SuppressWarnings("unused") 21 | private static final Logger log = Logger.getLogger(GeneticReducer.class); 22 | 23 | public static void setSurvivorSize(Job job, int survivorSize) { 24 | job.getConfiguration().setInt(SURVIVORS, survivorSize); 25 | } 26 | 27 | private PriorityQueue survivors = new PriorityQueue(); 28 | private int survivorSize; 29 | 30 | @Override 31 | protected void setup(Context context) 32 | throws IOException, InterruptedException { 33 | survivorSize = context.getConfiguration().getInt(SURVIVORS, 1000); 34 | } 35 | 36 | @Override 37 | protected void reduce(FloatWritable key, Iterable values, Context context) 38 | throws IOException, InterruptedException { 39 | for(CsvGeneticParams p : values) { 40 | survivors.offer(p.clone()); 41 | while(survivors.size() > survivorSize) 42 | survivors.poll(); 43 | } 44 | } 45 | 46 | @Override 47 | protected void cleanup(Context context) 48 | throws IOException, InterruptedException { 49 | List s = new ArrayList(); 50 | while(survivors.size() > 0) { 51 | s.add(survivors.poll()); 52 | } 53 | Collections.sort(s, CsvGeneticParams.PRETTY_COMPARATOR); 54 | for(CsvGeneticParams p : s) { 55 | context.write(NullWritable.get(), p); 56 | System.out.println(p.toPrettyString()); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /badiff-hadoop/src/main/java/org/badiff/hadoop/aig/InputBytesGenerator.java: -------------------------------------------------------------------------------- 1 | package org.badiff.hadoop.aig; 2 | 3 | import java.util.Random; 4 | 5 | public class InputBytesGenerator { 6 | private Random random; 7 | private byte[] from; 8 | private byte[] to; 9 | 10 | public InputBytesGenerator(long seed, int iteration) { 11 | this(seed, iteration, 32*1024); 12 | } 13 | 14 | public InputBytesGenerator(long seed, int iteration, int length) { 15 | random = new Random(seed + iteration); // predictable Random 16 | 17 | from = new byte[length]; 18 | to = new byte[length]; 19 | 20 | // initialize from and to 21 | random.nextBytes(from); 22 | System.arraycopy(from, 0, to, 0, from.length); 23 | 24 | // Do a whole lot of random swaps 25 | for(int i = 0; i < length / 128; i++) { 26 | byte[] b1 = new byte[random.nextInt(512)]; 27 | byte[] b2 = new byte[b1.length]; 28 | byte[] b3 = new byte[random.nextInt(256)]; 29 | 30 | int pos1 = random.nextInt(to.length - b1.length); 31 | int pos2 = random.nextInt(to.length - b2.length); 32 | 33 | System.arraycopy(to, pos1, b1, 0, b1.length); 34 | System.arraycopy(to, pos2, b2, 0, b2.length); 35 | random.nextBytes(b3); 36 | 37 | System.arraycopy(b1, 0, to, pos2, b1.length); 38 | System.arraycopy(b2, 0, to, pos1, b2.length); 39 | System.arraycopy(b3, 0, to, random.nextInt(to.length - b3.length), b3.length); 40 | } 41 | } 42 | 43 | public byte[] getFrom() { 44 | return from; 45 | } 46 | 47 | public byte[] getTo() { 48 | return to; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /badiff-kryo/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /badiff-kryo/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /badiff-kryo/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | badiff-kryo 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /badiff-kryo/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/main/resources=UTF-8 4 | encoding//src/test/java=UTF-8 5 | encoding//src/test/resources=UTF-8 6 | encoding/=UTF-8 7 | -------------------------------------------------------------------------------- /badiff-kryo/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 3 | org.eclipse.jdt.core.compiler.compliance=1.6 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.6 6 | -------------------------------------------------------------------------------- /badiff-kryo/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /badiff-kryo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | badiff-kryo 4 | 5 | org.badiff 6 | badiff-parent 7 | 1.2.0 8 | ../badiff-parent 9 | 10 | 11 | 12 | org.badiff 13 | badiff 14 | 15 | 16 | com.esotericsoftware 17 | kryo 18 | 3.0.0 19 | 20 | 21 | junit 22 | junit 23 | test 24 | 25 | 26 | -------------------------------------------------------------------------------- /badiff-kryo/src/main/java/org/badiff/kryo/BadiffSerializer.java: -------------------------------------------------------------------------------- 1 | package org.badiff.kryo; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | 5 | import org.badiff.ByteArrayDiffs; 6 | 7 | import com.esotericsoftware.kryo.Kryo; 8 | import com.esotericsoftware.kryo.Serializer; 9 | import com.esotericsoftware.kryo.io.Input; 10 | import com.esotericsoftware.kryo.io.Output; 11 | 12 | public class BadiffSerializer extends Serializer { 13 | protected Kryo bytesKryo; 14 | protected Class type; 15 | 16 | public BadiffSerializer(Class type) { 17 | this(type, new Kryo()); 18 | } 19 | 20 | public BadiffSerializer(Class type, Kryo bytesKryo) { 21 | this.bytesKryo = bytesKryo; 22 | this.type = type; 23 | } 24 | 25 | protected byte[] toBytes(T object) { 26 | ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 27 | Output bytesOutput = new Output(bytes); 28 | bytesKryo.writeClassAndObject(bytesOutput, object); 29 | bytesOutput.close(); 30 | return bytes.toByteArray(); 31 | } 32 | 33 | protected T fromBytes(byte[] buf) { 34 | Input bytesInput = new Input(buf); 35 | return type.cast(bytesKryo.readClassAndObject(bytesInput)); 36 | } 37 | 38 | @Override 39 | @SuppressWarnings("unchecked") 40 | public void write(Kryo kryo, Output output, T object) { 41 | if(kryo == bytesKryo) 42 | throw new IllegalArgumentException("Cannot re-use Kryo instances"); 43 | 44 | byte[] previous = (byte[]) kryo.getGraphContext().get(this, new byte[0]); 45 | byte[] next = toBytes(object); 46 | byte[] diff = ByteArrayDiffs.udiff(previous, next); 47 | 48 | output.writeInt(diff.length, true); 49 | output.write(diff); 50 | 51 | kryo.getGraphContext().put(this, next); 52 | } 53 | 54 | @Override 55 | @SuppressWarnings("unchecked") 56 | public T read(Kryo kryo, Input input, Class type) { 57 | if(kryo == bytesKryo) 58 | throw new IllegalArgumentException("Cannot re-use Kryo instances"); 59 | 60 | byte[] diff = input.readBytes(input.readInt(true)); 61 | byte[] previous = (byte[]) kryo.getGraphContext().get(this, new byte[0]); 62 | byte[] next = ByteArrayDiffs.apply(previous, diff); 63 | 64 | T object = fromBytes(next); 65 | 66 | kryo.getGraphContext().put(this, next); 67 | 68 | return object; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /badiff-kryo/src/main/java/org/badiff/kryo/KryoSerialization.java: -------------------------------------------------------------------------------- 1 | package org.badiff.kryo; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.OutputStream; 6 | import java.util.Map; 7 | 8 | import org.badiff.io.GraphContext; 9 | import org.badiff.io.Serialization; 10 | import org.badiff.io.Serialized; 11 | 12 | import com.esotericsoftware.kryo.Kryo; 13 | import com.esotericsoftware.kryo.Serializer; 14 | import com.esotericsoftware.kryo.io.Input; 15 | import com.esotericsoftware.kryo.io.Output; 16 | 17 | public class KryoSerialization implements Serialization { 18 | 19 | private static class SerializedSerializer extends Serializer { 20 | @Override 21 | public void write(Kryo kryo, Output output, Serialized object) { 22 | @SuppressWarnings("unchecked") 23 | KryoSerialization serial = (KryoSerialization) kryo.getContext().get(KryoSerialization.class); 24 | try { 25 | object.serialize(serial, output); 26 | } catch (IOException e) { 27 | throw new RuntimeException(e); 28 | } 29 | } 30 | 31 | @Override 32 | public Serialized read(Kryo kryo, Input input, Class type) { 33 | @SuppressWarnings("unchecked") 34 | KryoSerialization serial = (KryoSerialization) kryo.getContext().get(KryoSerialization.class); 35 | Serialized object = kryo.newInstance(type); 36 | kryo.reference(object); 37 | try { 38 | object.deserialize(serial, input); 39 | } catch(IOException e) { 40 | throw new RuntimeException(e); 41 | } 42 | return object; 43 | } 44 | 45 | } 46 | 47 | private Kryo kryo; 48 | private GraphContext graphContext; 49 | 50 | public KryoSerialization() { 51 | this(new Kryo()); 52 | } 53 | 54 | @SuppressWarnings("unchecked") 55 | public KryoSerialization(Kryo kryo) { 56 | this.kryo = kryo; 57 | this.kryo.addDefaultSerializer(Serialized.class, SerializedSerializer.class); 58 | this.kryo.getContext().put(KryoSerialization.class, this); 59 | graphContext = new GraphContext((Map) this.kryo.getGraphContext()); 60 | } 61 | 62 | private static Output wrap(OutputStream out) { 63 | if(out instanceof Output) 64 | return (Output) out; 65 | return new Output(out); 66 | } 67 | 68 | private static Input wrap(InputStream in) { 69 | if(in instanceof Input) 70 | return (Input) in; 71 | return new Input(in); 72 | } 73 | 74 | @Override 75 | public void writeObject(OutputStream out, Class type, T object) throws IOException { 76 | kryo.writeObjectOrNull(wrap(out), object, type); 77 | } 78 | 79 | @Override 80 | public T readObject(InputStream in, Class type) throws IOException { 81 | return kryo.readObjectOrNull(wrap(in), type); 82 | } 83 | 84 | @SuppressWarnings("unchecked") 85 | @Override 86 | public Map context() { 87 | return (Map) kryo.getContext(); 88 | } 89 | 90 | @Override 91 | public GraphContext graphContext() { 92 | return graphContext; 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /badiff-kryo/src/test/java/org/badiff/kryo/BadiffSerializerTest.java: -------------------------------------------------------------------------------- 1 | package org.badiff.kryo; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import com.esotericsoftware.kryo.Kryo; 9 | import com.esotericsoftware.kryo.io.Input; 10 | import com.esotericsoftware.kryo.io.Output; 11 | 12 | public class BadiffSerializerTest { 13 | @Test 14 | public void testSerialize() { 15 | String abc = "abcdefghijklmnopqrstuvwxyz"; 16 | String def = "defghijklmnopqrstuvwxyzabc"; 17 | 18 | Kryo kryo = new Kryo(); 19 | kryo.register(String.class, new BadiffSerializer(String.class)); 20 | kryo.setAutoReset(false); 21 | 22 | ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 23 | Output output = new Output(bytes); 24 | 25 | kryo.writeObject(output, abc); 26 | output.flush(); 27 | int abcSize = bytes.size(); 28 | kryo.writeObject(output, def); 29 | output.flush(); 30 | int defSize = bytes.size() - abcSize; 31 | 32 | Assert.assertTrue(abcSize > defSize); 33 | 34 | output.close(); 35 | 36 | kryo.reset(); 37 | Input input = new Input(bytes.toByteArray()); 38 | 39 | Assert.assertEquals(abc, kryo.readObject(input, String.class)); 40 | Assert.assertEquals(def, kryo.readObject(input, String.class)); 41 | } 42 | 43 | public static interface I {} 44 | public static class C implements I { 45 | private String alpha; 46 | public C() {} 47 | 48 | public C(String alpha) { 49 | if(alpha == null) 50 | throw new IllegalArgumentException(); 51 | this.alpha = alpha; 52 | } 53 | 54 | @Override 55 | public boolean equals(Object obj) { 56 | if(obj instanceof C) { 57 | return alpha.equals(((C) obj).alpha); 58 | } 59 | return false; 60 | } 61 | 62 | @Override 63 | public int hashCode() { 64 | if(alpha == null) 65 | return 0; 66 | return alpha.hashCode(); 67 | } 68 | 69 | @Override 70 | public String toString() { 71 | return alpha; 72 | } 73 | } 74 | 75 | @Test 76 | public void testDefaultSerialize() { 77 | C abc = new C("abcdefghijklmnopqrstuvwxyz"); 78 | C def = new C("defghijklmnopqrstuvwxyzabc"); 79 | 80 | Kryo kryo = new Kryo(); 81 | kryo.addDefaultSerializer(I.class, BadiffSerializer.class); 82 | kryo.setAutoReset(false); 83 | 84 | Assert.assertTrue(kryo.getSerializer(C.class) instanceof BadiffSerializer); 85 | 86 | ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 87 | Output output = new Output(bytes); 88 | 89 | kryo.writeClassAndObject(output, abc); 90 | output.flush(); 91 | int abcSize = bytes.size(); 92 | kryo.writeClassAndObject(output, def); 93 | output.flush(); 94 | int defSize = bytes.size() - abcSize; 95 | 96 | Assert.assertTrue(abcSize > defSize); 97 | 98 | output.close(); 99 | 100 | kryo.reset(); 101 | Input input = new Input(bytes.toByteArray()); 102 | 103 | Assert.assertEquals(abc, kryo.readClassAndObject(input)); 104 | Assert.assertEquals(def, kryo.readClassAndObject(input)); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /badiff-parent/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /badiff-parent/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | badiff-parent 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.m2e.core.maven2Builder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.m2e.core.maven2Nature 16 | 17 | 18 | -------------------------------------------------------------------------------- /badiff-parent/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding/=UTF-8 3 | -------------------------------------------------------------------------------- /badiff-parent/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /badiff-parent/badiff-parent.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /badiff-parent/header.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | -------------------------------------------------------------------------------- /badiff-parent/license.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013, Robin Kirkman 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 8 | Neither the name of the badiff nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 9 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 10 | -------------------------------------------------------------------------------- /badiff/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /badiff/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /badiff/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | badiff 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /badiff/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/test/java=UTF-8 4 | encoding//src/test/resources=UTF-8 5 | encoding/=UTF-8 6 | -------------------------------------------------------------------------------- /badiff/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 3 | org.eclipse.jdt.core.compiler.compliance=1.6 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.6 6 | -------------------------------------------------------------------------------- /badiff/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /badiff/badiff.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /badiff/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | badiff 5 | 6 | org.badiff 7 | badiff-parent 8 | 1.2.1 9 | ../badiff-parent 10 | 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | commons-io 19 | commons-io 20 | 2.4 21 | test 22 | 23 | 24 | 25 | 26 | 27 | 28 | org.apache.maven.plugins 29 | maven-jar-plugin 30 | 2.2 31 | 32 | 33 | 34 | test-jar 35 | 36 | 37 | 38 | default-jar 39 | 40 | 41 | 42 | org.badiff.BadiffCli 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | org.apache.maven.plugins 52 | maven-surefire-plugin 53 | 2.16 54 | 55 | -Xmx1024m 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/Applyable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff; 31 | 32 | import java.io.IOException; 33 | import java.io.InputStream; 34 | import java.io.OutputStream; 35 | 36 | /** 37 | * An object which can be applied to an "original" {@link InputStream} and write 38 | * the results to a "target" {@link OutputStream}. Objects which implement 39 | * {@link Applyable} might not be re-usable; for some objects only one call to 40 | * {@link #apply(InputStream, OutputStream)} will work. 41 | * @author robin 42 | * 43 | */ 44 | public interface Applyable { 45 | /** 46 | * Apply this object to {@code orig} and write the result to {@code target}. 47 | * @param orig 48 | * @param target 49 | * @throws IOException 50 | */ 51 | public void apply(InputStream orig, OutputStream target) throws IOException; 52 | 53 | } 54 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/Diff.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff; 31 | 32 | import java.io.IOException; 33 | import java.util.Iterator; 34 | 35 | import org.badiff.q.OpQueue; 36 | 37 | /** 38 | * A byte-level difference between two inputs. Can be applied to streams 39 | * via {@link Applyable}. {@link Diff} is a re-usable instance of {@link Applyable}. 40 | * @author robin 41 | * 42 | */ 43 | public interface Diff extends Applyable, Storeable, Queueable { 44 | /** 45 | * The default size of a chunk for operations which chunk their input 46 | */ 47 | public final int DEFAULT_CHUNK = 2048; 48 | 49 | /** 50 | * Overwrite this {@link Diff}'s operations with the operations from the 51 | * argument {@link Iterator} 52 | * @param ops 53 | * @throws IOException 54 | */ 55 | @Override 56 | public void store(Iterator ops) throws IOException; 57 | 58 | /** 59 | * Return a copy of this {@link Diff}'s operations. This copy may 60 | * be {@link OpQueue#poll()}'d from but not {@link OpQueue#offer(Op)}'d to. 61 | * @return 62 | * @throws IOException 63 | */ 64 | @Override 65 | public OpQueue queue() throws IOException; 66 | } 67 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/Queueable.java: -------------------------------------------------------------------------------- 1 | package org.badiff; 2 | 3 | import java.io.IOException; 4 | 5 | import org.badiff.q.OpQueue; 6 | 7 | /** 8 | * An object which can provide an {@link OpQueue} 9 | * @author robin 10 | * 11 | */ 12 | public interface Queueable { 13 | /** 14 | * Return this object's operations. This queue 15 | * be {@link OpQueue#poll()}'d from but not {@link OpQueue#offer(Op)}'d to. 16 | * @return 17 | * @throws IOException 18 | */ 19 | public OpQueue queue() throws IOException; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/Storeable.java: -------------------------------------------------------------------------------- 1 | package org.badiff; 2 | 3 | import java.io.IOException; 4 | import java.util.Iterator; 5 | 6 | /** 7 | * An object which can store a sequence of {@link Op}s 8 | * @author robin 9 | * 10 | */ 11 | public interface Storeable { 12 | /** 13 | * Overwrite this object with the operations from the 14 | * argument {@link Iterator} 15 | * @param ops 16 | * @throws IOException 17 | */ 18 | public void store(Iterator ops) throws IOException; 19 | 20 | } 21 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/alg/Graph.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.alg; 31 | 32 | import org.badiff.q.OpQueue; 33 | 34 | /** 35 | * An object which computes an {@link OpQueue} to convert an input byte array to an 36 | * output byte array. Usually this will use a mathematical graph construct. 37 | * @author robin 38 | * 39 | */ 40 | public interface Graph { 41 | /** 42 | * Reset this graph and then recompute it for the new original and target arrays 43 | * @param orig 44 | * @param target 45 | */ 46 | public void compute(byte[] orig, byte[] target); 47 | /** 48 | * Return the {@link OpQueue} of operations computed by this object 49 | * @return 50 | */ 51 | public OpQueue queue(); 52 | } 53 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/alg/GraphFactory.java: -------------------------------------------------------------------------------- 1 | package org.badiff.alg; 2 | 3 | 4 | public interface GraphFactory { 5 | public static final GraphFactory EDIT_GRAPH = new GraphFactory() { 6 | @Override 7 | public Graph newGraph(int capacity) { 8 | return new EditGraph(capacity); 9 | } 10 | }; 11 | public static final GraphFactory INERTIAL_GRAPH = new GraphFactory() { 12 | @Override 13 | public Graph newGraph(int capacity) { 14 | return new InertialGraph(capacity); 15 | } 16 | }; 17 | public static final GraphFactory ADJUSTABLE_GRAPH = new GraphFactory() { 18 | @Override 19 | public Graph newGraph(int capacity) { 20 | return new AdjustableInertialGraph(capacity); 21 | } 22 | }; 23 | 24 | public Graph newGraph(int capacity); 25 | } -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/alg/MinNextEditGraph.java: -------------------------------------------------------------------------------- 1 | package org.badiff.alg; 2 | 3 | import java.util.Arrays; 4 | 5 | import org.badiff.Op; 6 | 7 | public class MinNextEditGraph extends EditGraph { 8 | 9 | protected int minNext = 1; 10 | 11 | public MinNextEditGraph(int size) { 12 | this(size, 1); 13 | } 14 | 15 | public MinNextEditGraph(int size, int minNext) { 16 | super(size); 17 | this.minNext = minNext; 18 | } 19 | 20 | @Override 21 | public void compute(byte[] orig, byte[] target) { 22 | xval = new byte[orig.length + 1]; 23 | yval = new byte[target.length + 1]; 24 | 25 | System.arraycopy(orig, 0, xval, 1, orig.length); 26 | System.arraycopy(target, 0, yval, 1, target.length); 27 | 28 | // mark short next runs as unusable 29 | Arrays.fill(flags, (byte) -1); 30 | for(int y = 0; y < yval.length; y++) { 31 | for(int x = 0; x < xval.length; x++) { 32 | if(x == 0 || y == 0 || xval[x] != yval[y]) 33 | continue; 34 | int pos = x + y * xval.length; 35 | 36 | if(flags[pos] == Op.NEXT || flags[pos] == Op.STOP) 37 | continue; 38 | 39 | int extent = 0; 40 | int ix = x; 41 | int iy = y; 42 | do { 43 | extent++; 44 | ix++; 45 | iy++; 46 | if(ix == xval.length || iy == yval.length) 47 | break; 48 | } while(xval[ix] == yval[iy]); 49 | 50 | while(--ix >= x && --iy >= y) { 51 | flags[ix + iy * xval.length] = (extent >= minNext) ? Op.NEXT : Op.STOP; 52 | } 53 | } 54 | } 55 | 56 | // "normal" compute 57 | for(int y = 0; y < yval.length; y++) { 58 | for(int x = 0; x < xval.length; x++) { 59 | if(x == 0 && y == 0) 60 | continue; 61 | int pos = x + y * xval.length; 62 | 63 | // check for equality, but ensure that if equal not marked as too short 64 | if(x > 0 && y > 0 && flags[pos] == Op.NEXT) { 65 | lengths[pos] = (short) (1 + lengths[pos - xval.length - 1]); 66 | continue; 67 | } 68 | short dlen = x > 0 ? (short)(1 + lengths[pos-1]) : Short.MAX_VALUE; 69 | short ilen = y > 0 ? (short)(1 + lengths[pos - xval.length]) : Short.MAX_VALUE; 70 | if(dlen <= ilen) { 71 | flags[pos] = Op.DELETE; 72 | lengths[pos] = dlen; 73 | } else { 74 | flags[pos] = Op.INSERT; 75 | lengths[pos] = ilen; 76 | } 77 | 78 | } 79 | } 80 | } 81 | 82 | public int getMinNext() { 83 | return minNext; 84 | } 85 | 86 | public void setMinNext(int minNext) { 87 | if(minNext < 1) 88 | throw new IllegalArgumentException(); 89 | this.minNext = minNext; 90 | } 91 | 92 | } 93 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/alg/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Edit-graphing algorithm implementations.

3 | * 4 | * Contains various implementations of the basic edit graph concept, such 5 | * as weighted edit graphs. 6 | */ 7 | package org.badiff.alg; -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/fmt/InputFormat.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.fmt; 31 | 32 | import java.io.IOException; 33 | import org.badiff.io.RandomInput; 34 | import org.badiff.q.OpQueue; 35 | 36 | /** 37 | * An object which can import a diff from an external format 38 | * @author robin 39 | * 40 | */ 41 | public interface InputFormat { 42 | /** 43 | * Import the externally formatted diff 44 | * @param orig The original file 45 | * @param extDiff The diff to import 46 | * @return A badiff diff 47 | */ 48 | public OpQueue importDiff(RandomInput orig, RandomInput ext) throws IOException; 49 | } 50 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/fmt/OutputFormat.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.fmt; 31 | 32 | import java.io.DataOutput; 33 | import java.io.IOException; 34 | import org.badiff.Diff; 35 | import org.badiff.io.RandomInput; 36 | 37 | /** 38 | * An object which can convert a {@link Diff} to an external format for long-term storage 39 | * @author robin 40 | * 41 | */ 42 | public interface OutputFormat { 43 | /** 44 | * Export the diff to the external format. 45 | * @param diff The diff to export 46 | * @param orig The original file to reference while exporting 47 | * @param out The output to which to write the exported diff 48 | * @throws IOException 49 | */ 50 | public void exportDiff(Diff diff, RandomInput orig, DataOutput out) throws IOException; 51 | } 52 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/fmt/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes for reading and writing diffs in various formats. 3 | */ 4 | package org.badiff.fmt; -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/imp/StreamQueueable.java: -------------------------------------------------------------------------------- 1 | package org.badiff.imp; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | 6 | import org.badiff.Op; 7 | import org.badiff.Queueable; 8 | import org.badiff.io.DefaultSerialization; 9 | import org.badiff.io.RuntimeIOException; 10 | import org.badiff.io.Serialization; 11 | import org.badiff.q.OpQueue; 12 | 13 | /** 14 | * {@link Queueable} that reads its elements from an {@link InputStream}. 15 | * The {@link #queue()} method can be called only once; calling it again 16 | * will return the next sequence of {@link Op}s in the input stream. 17 | * @author robin 18 | * 19 | */ 20 | public class StreamQueueable implements Queueable { 21 | /** 22 | * Serialization used for this stream 23 | */ 24 | protected Serialization serial; 25 | /** 26 | * The input stream 27 | */ 28 | protected InputStream in; 29 | 30 | /** 31 | * Create a {@link StreamQueueable} with the {@link DefaultSerialization} 32 | * @param in 33 | */ 34 | public StreamQueueable(InputStream in) { 35 | this(in, DefaultSerialization.newInstance()); 36 | } 37 | 38 | /** 39 | * Create a {@link StreamQueueable} 40 | * @param in 41 | * @param serial 42 | */ 43 | public StreamQueueable(InputStream in, Serialization serial) { 44 | this.in = in; 45 | this.serial = serial; 46 | } 47 | 48 | @Override 49 | public OpQueue queue() throws IOException { 50 | return new StreamOpQueue(); 51 | } 52 | 53 | /** 54 | * {@link OpQueue} that reads from a stream 55 | * @author robin 56 | * 57 | */ 58 | private class StreamOpQueue extends OpQueue { 59 | private boolean closed; 60 | 61 | @Override 62 | public boolean offer(Op e) { 63 | throw new UnsupportedOperationException(); 64 | } 65 | 66 | @Override 67 | protected boolean pull() { 68 | if(closed) 69 | return false; 70 | 71 | try { 72 | Op e = serial.readObject(in, Op.class); 73 | if(e.getOp() == Op.STOP) { 74 | closed = true; 75 | return false; 76 | } else { 77 | prepare(e); 78 | return true; 79 | } 80 | } catch(IOException ioe) { 81 | throw new RuntimeIOException(ioe); 82 | } 83 | } 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/imp/StreamStoreable.java: -------------------------------------------------------------------------------- 1 | package org.badiff.imp; 2 | 3 | import java.io.IOException; 4 | import java.io.OutputStream; 5 | import java.util.Iterator; 6 | 7 | import org.badiff.Op; 8 | import org.badiff.Storeable; 9 | import org.badiff.io.DefaultSerialization; 10 | import org.badiff.io.Serialization; 11 | 12 | public class StreamStoreable implements Storeable { 13 | 14 | protected OutputStream out; 15 | protected Serialization serial; 16 | 17 | public StreamStoreable(OutputStream out) { 18 | this(out, DefaultSerialization.newInstance()); 19 | } 20 | 21 | public StreamStoreable(OutputStream out, Serialization serial) { 22 | this.out = out; 23 | this.serial = serial; 24 | } 25 | 26 | @Override 27 | public void store(Iterator ops) throws IOException { 28 | while(ops.hasNext()) { 29 | Op e = ops.next(); 30 | e.serialize(serial, out); 31 | } 32 | new Op(Op.STOP, 1, null).serialize(serial, out); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/imp/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Implementations of {@link org.badiff.Diff} 3 | */ 4 | package org.badiff.imp; -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/io/DataInputInputStream.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.io; 31 | 32 | import java.io.DataInput; 33 | import java.io.EOFException; 34 | import java.io.IOException; 35 | import java.io.InputStream; 36 | import java.io.ObjectInput; 37 | 38 | /** 39 | * {@link InputStream} that reads from an {@link ObjectInput} 40 | * @author robin 41 | * 42 | */ 43 | public class DataInputInputStream extends InputStream { 44 | 45 | protected DataInput in; 46 | 47 | public DataInputInputStream(DataInput in) { 48 | this.in = in; 49 | } 50 | 51 | @Override 52 | public int read() throws IOException { 53 | try { 54 | return 0xff & in.readByte(); 55 | } catch(EOFException eof) { 56 | return -1; 57 | } 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/io/DataOutputOutputStream.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.io; 31 | 32 | import java.io.DataOutput; 33 | import java.io.IOException; 34 | import java.io.ObjectOutput; 35 | import java.io.OutputStream; 36 | 37 | /** 38 | * {@link OutputStream} that reads from an {@link ObjectOutput} 39 | * @author robin 40 | * 41 | */ 42 | public class DataOutputOutputStream extends OutputStream { 43 | 44 | protected DataOutput out; 45 | 46 | public DataOutputOutputStream(DataOutput out) { 47 | this.out = out; 48 | } 49 | 50 | @Override 51 | public void write(int b) throws IOException { 52 | out.write(b); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/io/EmptyInputStream.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.io; 31 | 32 | import java.io.IOException; 33 | import java.io.InputStream; 34 | 35 | /** 36 | * {@link InputStream} that has no content 37 | * @author robin 38 | * 39 | */ 40 | public class EmptyInputStream extends InputStream { 41 | 42 | @Override 43 | public int read() throws IOException { 44 | return -1; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/io/FileRandomInput.java: -------------------------------------------------------------------------------- 1 | package org.badiff.io; 2 | 3 | import java.io.DataInputStream; 4 | import java.io.File; 5 | import java.io.FileInputStream; 6 | import java.io.FilterInputStream; 7 | import java.io.IOException; 8 | 9 | public class FileRandomInput extends FilterInputStream implements RandomInput { 10 | protected File file; 11 | protected long pos; 12 | protected DataInputStream data; 13 | 14 | public FileRandomInput(File file) throws IOException { 15 | super(new FileInputStream(file)); 16 | this.file = file; 17 | data = new DataInputStream(this); 18 | } 19 | 20 | @Override 21 | public long first() { 22 | return 0; 23 | } 24 | 25 | @Override 26 | public long last() { 27 | return file.length(); 28 | } 29 | 30 | @Override 31 | public long position() { 32 | return pos; 33 | } 34 | 35 | @Override 36 | public void seek(long pos) throws IOException { 37 | skip(pos - this.pos); 38 | } 39 | 40 | @Override 41 | public int read() throws IOException { 42 | int b = super.read(); 43 | if(b >= 0) 44 | pos++; 45 | return b; 46 | } 47 | 48 | @Override 49 | public int read(byte[] b) throws IOException { 50 | return super.read(b); 51 | } 52 | 53 | @Override 54 | public int read(byte[] b, int off, int len) throws IOException { 55 | int r = super.read(b, off, len); 56 | if(r > 0) 57 | pos += r; 58 | return r; 59 | } 60 | 61 | @Override 62 | public long skip(long n) throws IOException { 63 | if(n > 0) { 64 | long s = in.skip(n); 65 | pos += s; 66 | return s; 67 | } else if(n < 0) { 68 | in.close(); 69 | in = new FileInputStream(file); 70 | in.skip(pos + n); 71 | pos = pos + n; 72 | return n; 73 | } else 74 | return 0; 75 | } 76 | 77 | public final void readFully(byte[] b) throws IOException { 78 | data.readFully(b); 79 | } 80 | 81 | public final void readFully(byte[] b, int off, int len) throws IOException { 82 | data.readFully(b, off, len); 83 | } 84 | 85 | public final boolean readBoolean() throws IOException { 86 | return data.readBoolean(); 87 | } 88 | 89 | public final byte readByte() throws IOException { 90 | return data.readByte(); 91 | } 92 | 93 | public final int readUnsignedByte() throws IOException { 94 | return data.readUnsignedByte(); 95 | } 96 | 97 | public final short readShort() throws IOException { 98 | return data.readShort(); 99 | } 100 | 101 | public final int readUnsignedShort() throws IOException { 102 | return data.readUnsignedShort(); 103 | } 104 | 105 | public final char readChar() throws IOException { 106 | return data.readChar(); 107 | } 108 | 109 | public final int readInt() throws IOException { 110 | return data.readInt(); 111 | } 112 | 113 | public final long readLong() throws IOException { 114 | return data.readLong(); 115 | } 116 | 117 | public final float readFloat() throws IOException { 118 | return data.readFloat(); 119 | } 120 | 121 | public final double readDouble() throws IOException { 122 | return data.readDouble(); 123 | } 124 | 125 | @Deprecated 126 | public final String readLine() throws IOException { 127 | return data.readLine(); 128 | } 129 | 130 | public final String readUTF() throws IOException { 131 | return data.readUTF(); 132 | } 133 | 134 | @Override 135 | public int skipBytes(int n) throws IOException { 136 | return data.skipBytes(n); 137 | } 138 | 139 | } 140 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/io/GraphContext.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.io; 31 | 32 | import java.util.HashMap; 33 | import java.util.Map; 34 | 35 | public class GraphContext extends HashMap { 36 | private static final long serialVersionUID = 0; 37 | 38 | private Map context; 39 | 40 | public GraphContext(Map context) { 41 | this.context = context; 42 | } 43 | 44 | @Override 45 | public boolean containsKey(Object key) { 46 | return super.containsKey(key) || context.containsKey(key); 47 | } 48 | 49 | @Override 50 | public Object get(Object key) { 51 | return super.containsKey(key) ? super.get(key) : context.get(key); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/io/NonClosingInputStream.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.io; 31 | 32 | import java.io.FilterInputStream; 33 | import java.io.IOException; 34 | import java.io.InputStream; 35 | 36 | /** 37 | * {@link FilterInputStream} which doesn't close its wrapped {@link InputStream} 38 | * @author robin 39 | * 40 | */ 41 | public class NonClosingInputStream extends FilterInputStream { 42 | 43 | public NonClosingInputStream(InputStream in) { 44 | super(in); 45 | } 46 | 47 | @Override 48 | public void close() throws IOException { 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/io/NonClosingOutputStream.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.io; 31 | 32 | import java.io.FilterOutputStream; 33 | import java.io.IOException; 34 | import java.io.OutputStream; 35 | 36 | /** 37 | * {@link FilterOutputStream} which doesn't close its wrapped {@link OutputStream} 38 | * @author robin 39 | * 40 | */ 41 | public class NonClosingOutputStream extends FilterOutputStream { 42 | 43 | public NonClosingOutputStream(OutputStream out) { 44 | super(out); 45 | } 46 | 47 | @Override 48 | public void close() throws IOException { 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/io/NoopOutputStream.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.io; 31 | 32 | import java.io.IOException; 33 | import java.io.OutputStream; 34 | 35 | public class NoopOutputStream extends OutputStream { 36 | 37 | private long bytesWritten = 0; 38 | 39 | public NoopOutputStream() { 40 | } 41 | 42 | @Override 43 | public void write(int b) throws IOException { 44 | bytesWritten++; 45 | } 46 | 47 | @Override 48 | public void write(byte[] b) throws IOException { 49 | bytesWritten += b.length; 50 | } 51 | 52 | @Override 53 | public void write(byte[] b, int off, int len) throws IOException { 54 | bytesWritten += len; 55 | } 56 | 57 | public long getBytesWritten() { 58 | return bytesWritten; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/io/Random.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.io; 31 | 32 | import java.io.IOException; 33 | import java.io.InputStream; 34 | 35 | public interface Random { 36 | /** 37 | * Returns the first position that can be {@link #seek(long)}ed 38 | * @return 39 | */ 40 | public long first(); 41 | /** 42 | * Returns the last position, plus one, that can be {@link #seek(long)}ed 43 | * @return 44 | */ 45 | public long last(); 46 | /** 47 | * Returns the current position 48 | * @return 49 | */ 50 | public long position(); 51 | /** 52 | * Set the current position 53 | * @param pos 54 | * @throws IOException 55 | */ 56 | public void seek(long pos) throws IOException; 57 | /** 58 | * @see InputStream#skip(long) 59 | * @param count 60 | * @throws IOException 61 | */ 62 | public long skip(long count) throws IOException; 63 | 64 | } 65 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/io/RandomInput.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.io; 31 | 32 | import java.io.DataInput; 33 | import java.io.IOException; 34 | 35 | /** 36 | * A random-access {@link DataInput} 37 | * @author robin 38 | * 39 | */ 40 | public interface RandomInput extends DataInput, Random { 41 | public int read() throws IOException; 42 | 43 | public int read(byte[] b) throws IOException; 44 | 45 | public int read(byte[] b, int off, int len) throws IOException; 46 | 47 | public int available() throws IOException; 48 | 49 | public void close() throws IOException; 50 | } 51 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/io/RuntimeIOException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.io; 31 | 32 | import java.io.IOException; 33 | 34 | /** 35 | * {@link RuntimeException} wrapper around {@link IOException} 36 | * @author robin 37 | * 38 | */ 39 | public class RuntimeIOException extends RuntimeException { 40 | private static final long serialVersionUID = 0; 41 | 42 | public RuntimeIOException(IOException cause) { 43 | super(cause); 44 | } 45 | 46 | public RuntimeIOException(String message, IOException cause) { 47 | super(message, cause); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/io/Serialization.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.io; 31 | 32 | import java.io.IOException; 33 | import java.io.InputStream; 34 | import java.io.OutputStream; 35 | import java.util.Map; 36 | 37 | /** 38 | * A mechanism for serializing data to/from {@link OutputStream} / {@link InputStream} 39 | * @author robin 40 | * 41 | */ 42 | public interface Serialization { 43 | /** 44 | * Write the argument object to the {@link OutputStream}. 45 | * The object may be null. 46 | * @param out 47 | * @param type 48 | * @param object 49 | * @throws IOException 50 | */ 51 | public void writeObject(OutputStream out, Class type, T object) throws IOException; 52 | 53 | /** 54 | * Read an object of the argument type from the {@link InputStream} 55 | * @param in 56 | * @param type 57 | * @return 58 | * @throws IOException 59 | */ 60 | public T readObject(InputStream in, Class type) throws IOException; 61 | 62 | /** 63 | * Storage for data which should be available to serializers and persist across serializations and deserializations 64 | * @return 65 | */ 66 | public Map context(); 67 | 68 | /** 69 | * Storage for data which is erased immediately prior to each top-level serialization or deserialization. 70 | * @return 71 | */ 72 | public GraphContext graphContext(); 73 | } 74 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/io/Serialized.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.io; 31 | 32 | import java.io.IOException; 33 | import java.io.InputStream; 34 | import java.io.OutputStream; 35 | 36 | /** 37 | * Interface for objects which can be serialized and deserialized with a {@link Serialization} 38 | * @author robin 39 | * 40 | */ 41 | public interface Serialized { 42 | /** 43 | * Serialize this object 44 | * @param serial 45 | * @param out 46 | * @throws IOException 47 | */ 48 | public void serialize(Serialization serial, OutputStream out) throws IOException; 49 | /** 50 | * Deserialize this object 51 | * @param serial 52 | * @param in 53 | * @throws IOException 54 | */ 55 | public void deserialize(Serialization serial, InputStream in) throws IOException; 56 | } 57 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/io/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes used for IO or serialization. 3 | */ 4 | package org.badiff.io; -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/p/Pipe.java: -------------------------------------------------------------------------------- 1 | package org.badiff.p; 2 | 3 | import org.badiff.q.OpQueue; 4 | 5 | public interface Pipe { 6 | public Pipeline from(OpQueue q); 7 | } 8 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/p/Pipeline.java: -------------------------------------------------------------------------------- 1 | package org.badiff.p; 2 | 3 | import org.badiff.q.OpQueue; 4 | 5 | public class Pipeline { 6 | 7 | protected OpQueue q; 8 | 9 | public Pipeline(OpQueue q) { 10 | this.q = q; 11 | } 12 | 13 | public Pipeline(OpQueue q, Pipe... pipes) { 14 | this(q); 15 | this.q = into(pipes).outlet(); 16 | } 17 | 18 | public Pipeline(OpQueue q, String codes) { 19 | this(q); 20 | this.q = into(codes).outlet(); 21 | } 22 | 23 | public Pipeline into(Pipe... pipes) { 24 | Pipeline pl = this; 25 | for(Pipe p : pipes) 26 | pl = p.from(pl.outlet()); 27 | return pl; 28 | } 29 | 30 | public Pipeline into(String codes) { 31 | return into(Pipes.fromCodes(codes)); 32 | } 33 | 34 | public OpQueue outlet() { 35 | return q; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/p/Pipes.java: -------------------------------------------------------------------------------- 1 | package org.badiff.p; 2 | 3 | import org.badiff.alg.GraphFactory; 4 | import org.badiff.alg.InertialGraph; 5 | import org.badiff.q.ChunkingOpQueue; 6 | import org.badiff.q.CoalescingOpQueue; 7 | import org.badiff.q.CompactingOpQueue; 8 | import org.badiff.q.GraphOpQueue; 9 | import org.badiff.q.OneWayOpQueue; 10 | import org.badiff.q.OpQueue; 11 | import org.badiff.q.ParallelGraphOpQueue; 12 | import org.badiff.q.PumpingOpQueue; 13 | import org.badiff.q.RewindingOpQueue; 14 | import org.badiff.q.UnchunkingOpQueue; 15 | import org.badiff.q.UndoOpQueue; 16 | 17 | public enum Pipes implements Pipe { 18 | SPLIT('s'), 19 | COALESS('c'), 20 | COMPACT('C'), 21 | GRAPH('g'), 22 | ONE_WAY('o'), 23 | PARALLEL_GRAPH('G'), 24 | PUMP('p'), 25 | REWIND('r'), 26 | UNCHUNK('u'), 27 | UNDO('U'), 28 | ; 29 | 30 | private char code; 31 | 32 | private Pipes(char code) { 33 | this.code = code; 34 | } 35 | 36 | public char code() { 37 | return code; 38 | } 39 | 40 | public static Pipes fromCode(char code) { 41 | for(Pipes pipe : Pipes.values()) 42 | if(pipe.code() == code) 43 | return pipe; 44 | throw new IllegalArgumentException("No such pipe code:" + code); 45 | } 46 | 47 | public static Pipes[] fromCodes(String codes) { 48 | Pipes[] pipes = new Pipes[codes.length()]; 49 | for(int i = 0; i < pipes.length; i++) 50 | pipes[i] = Pipes.fromCode(codes.charAt(i)); 51 | return pipes; 52 | } 53 | 54 | public static String toCodes(Pipes... pipes) { 55 | StringBuilder sb = new StringBuilder(); 56 | for(Pipes pipe : pipes) 57 | sb.append(pipe.code()); 58 | return sb.toString(); 59 | } 60 | 61 | @Override 62 | public Pipeline from(OpQueue q) { 63 | switch(this) { 64 | case SPLIT: 65 | q = new ChunkingOpQueue(q); 66 | break; 67 | case COALESS: 68 | q = new CoalescingOpQueue(q); 69 | break; 70 | case COMPACT: 71 | q = new CompactingOpQueue(q); 72 | break; 73 | case GRAPH: 74 | q = new GraphOpQueue(q, new InertialGraph()); 75 | break; 76 | case ONE_WAY: 77 | q = new OneWayOpQueue(q); 78 | break; 79 | case PARALLEL_GRAPH: 80 | q = new ParallelGraphOpQueue(q, GraphFactory.INERTIAL_GRAPH); 81 | break; 82 | case PUMP: 83 | q = new PumpingOpQueue(q); 84 | break; 85 | case REWIND: 86 | q = new RewindingOpQueue(q); 87 | break; 88 | case UNCHUNK: 89 | q = new UnchunkingOpQueue(q); 90 | break; 91 | case UNDO: 92 | q = new UndoOpQueue(q); 93 | break; 94 | default: 95 | throw new UnsupportedOperationException(); 96 | } 97 | return new Pipeline(q); 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/p/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes to support {@link org.badiff.p.Pipeline} diffing 3 | */ 4 | package org.badiff.p; -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * High-level diffing API.

3 | * 4 | * Contains classes and interfaces intended for use by those who do not 5 | * wish to "get their hands dirty" with the internals of badiff. 6 | */ 7 | package org.badiff; -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/q/BufferChunkingOpQueue.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.q; 31 | 32 | import java.nio.ByteBuffer; 33 | 34 | import org.badiff.Diff; 35 | import org.badiff.Op; 36 | 37 | public class BufferChunkingOpQueue extends OpQueue { 38 | 39 | protected ByteBuffer orig; 40 | protected ByteBuffer target; 41 | protected int chunk; 42 | 43 | public BufferChunkingOpQueue(ByteBuffer orig, ByteBuffer target) { 44 | this(orig, target, Diff.DEFAULT_CHUNK); 45 | } 46 | 47 | public BufferChunkingOpQueue(ByteBuffer orig, ByteBuffer target, int chunk) { 48 | this.orig = orig; 49 | this.target = target; 50 | this.chunk = chunk; 51 | } 52 | 53 | @Override 54 | protected boolean pull() { 55 | byte[] obuf = readChunk(orig); 56 | byte[] tbuf = readChunk(target); 57 | 58 | if(obuf != null) 59 | prepare(new Op(Op.DELETE, obuf.length, obuf)); 60 | if(tbuf != null) 61 | prepare(new Op(Op.INSERT, tbuf.length, tbuf)); 62 | 63 | return obuf != null || tbuf != null; 64 | } 65 | 66 | protected byte[] readChunk(ByteBuffer in) { 67 | if(in.remaining() == 0) 68 | return null; 69 | byte[] buf = new byte[Math.min(chunk, in.remaining())]; 70 | in.get(buf); 71 | return buf; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/q/CompactingOpQueue.java: -------------------------------------------------------------------------------- 1 | package org.badiff.q; 2 | 3 | import java.util.Arrays; 4 | 5 | import org.badiff.Op; 6 | 7 | public class CompactingOpQueue extends FilterOpQueue { 8 | 9 | public CompactingOpQueue(OpQueue source) { 10 | super(source); 11 | } 12 | 13 | @Override 14 | protected boolean pull() { 15 | if(!require(2)) 16 | return flush(); 17 | 18 | while(filtering.get(0).getOp() == filtering.get(1).getOp()) { 19 | Op e1 = filtering.remove(0); 20 | Op e2 = filtering.remove(0); 21 | byte[] data = null; 22 | if(e1.getData() != null && e2.getData() != null) { 23 | data = Arrays.copyOf(e1.getData(), e1.getData().length + e2.getData().length); 24 | System.arraycopy(e2.getData(), 0, data, e1.getData().length, e2.getData().length); 25 | } 26 | filtering.add(0, new Op(e1.getOp(), e1.getRun() + e2.getRun(), data)); 27 | if(!require(2)) 28 | return flush(); 29 | } 30 | 31 | prepare(filtering.remove(0)); 32 | return true; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/q/FutureOpQueue.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.q; 31 | 32 | import java.util.concurrent.ExecutionException; 33 | import java.util.concurrent.Future; 34 | 35 | import org.badiff.Op; 36 | 37 | /** 38 | * {@link OpQueue} similar to {@link FilterOpQueue}, but draws its elements 39 | * from an evaluated {@link Future}{@code } rather than a direct {@link OpQueue}. 40 | * @author robin 41 | * 42 | */ 43 | public class FutureOpQueue extends OpQueue { 44 | /** 45 | * The {@link Future} that will supply an {@link OpQueue} to draw elements from 46 | */ 47 | protected Future source; 48 | 49 | /** 50 | * {@link OpQueue} that will draw elements from a {@link Future}{@code } 51 | * @param source 52 | */ 53 | public FutureOpQueue(Future source) { 54 | this.source = source; 55 | } 56 | 57 | @Override 58 | public boolean offer(Op e) { 59 | throw new UnsupportedOperationException(); 60 | } 61 | 62 | @Override 63 | protected boolean pull() { 64 | try { 65 | Op e; 66 | boolean pulled = false; 67 | while((e = source.get().poll()) != null) { 68 | prepare(e); 69 | pulled = true; 70 | } 71 | return pulled; 72 | } catch(InterruptedException ie) { 73 | throw new RuntimeException(ie); 74 | } catch(ExecutionException ee) { 75 | throw new RuntimeException(ee); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/q/GraphOpQueue.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.q; 31 | 32 | import org.badiff.Op; 33 | import org.badiff.alg.EditGraph; 34 | import org.badiff.alg.Graph; 35 | 36 | /** 37 | * {@link OpQueue} that replaces ({@link Op#DELETE},{@link Op#INSERT}) pairs 38 | * with their {@link EditGraph}'d equivalents 39 | * @author robin 40 | * 41 | */ 42 | public class GraphOpQueue extends FilterOpQueue { 43 | 44 | protected Graph graph; 45 | 46 | public GraphOpQueue(OpQueue source, int chunk) { 47 | this(source, new EditGraph((chunk+1) * (chunk+1))); 48 | } 49 | 50 | public GraphOpQueue(OpQueue source, Graph graph) { 51 | super(source); 52 | this.graph = graph; 53 | } 54 | 55 | @Override 56 | protected boolean pull() { 57 | if(!require(2)) 58 | return flush(); 59 | 60 | Op delete = null; 61 | Op insert = null; 62 | 63 | if(filtering.get(0).getOp() == Op.DELETE && filtering.get(1).getOp() == Op.INSERT) { 64 | delete = filtering.get(0); 65 | insert = filtering.get(1); 66 | } else if(filtering.get(0).getOp() == Op.INSERT && filtering.get(1).getOp() == Op.DELETE) { 67 | delete = filtering.get(1); 68 | insert = filtering.get(0); 69 | } else 70 | return flush(); 71 | 72 | filtering.remove(1); 73 | filtering.remove(0); 74 | 75 | graph.compute(delete.getData(), insert.getData()); 76 | OpQueue q = graph.queue(); 77 | for(Op e = q.poll(); e != null; e = q.poll()) 78 | prepare(e); 79 | 80 | return true; 81 | } 82 | 83 | 84 | } 85 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/q/ListOpQueue.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.q; 31 | 32 | import java.util.Iterator; 33 | import java.util.List; 34 | 35 | import org.badiff.Op; 36 | 37 | /** 38 | * {@link OpQueue} that initially populates its {@link OpQueue#pending} 39 | * from a {@link List} 40 | * @author robin 41 | * 42 | */ 43 | public class ListOpQueue extends OpQueue { 44 | 45 | /** 46 | * Return an {@link OpQueue} with pre-populated {@link OpQueue#pending} 47 | * from the argument list 48 | * @param ops 49 | */ 50 | public ListOpQueue(List ops) { 51 | for(Op e : ops) 52 | prepare(e); 53 | } 54 | 55 | public ListOpQueue(Iterator ops) { 56 | while(ops.hasNext()) { 57 | prepare(ops.next()); 58 | } 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/q/OneWayOpQueue.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.q; 31 | 32 | import org.badiff.Op; 33 | 34 | /** 35 | * {@link OpQueue} that strips the {@link Op#getData()} from {@link Op#DELETE}s 36 | * @author robin 37 | * 38 | */ 39 | public class OneWayOpQueue extends FilterOpQueue { 40 | 41 | public OneWayOpQueue(OpQueue source) { 42 | super(source); 43 | } 44 | 45 | @Override 46 | protected boolean pull() { 47 | if(!require(1)) 48 | return flush(); 49 | Op e = filtering.remove(0); 50 | if(e.getOp() == Op.DELETE) 51 | e = new Op(Op.DELETE, e.getRun(), null); 52 | if(e.getOp() == Op.NEXT) 53 | e = new Op(Op.NEXT, e.getRun(), null); 54 | 55 | prepare(e); 56 | return true; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/q/ReferenceOpQueue.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.q; 31 | 32 | public class ReferenceOpQueue extends FilterOpQueue { 33 | 34 | public ReferenceOpQueue(OpQueue source, int size) { 35 | super(source); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/q/ReplaceOpQueue.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.q; 31 | 32 | import org.badiff.Op; 33 | 34 | /** 35 | * {@link OpQueue} that is pre-populated with two {@link Op}s, one 36 | * to {@link Op#DELETE} a {@code byte[]} and one to {@link Op#INSERT} 37 | * a {@code byte[]} 38 | * @author robin 39 | * 40 | */ 41 | public class ReplaceOpQueue extends OpQueue { 42 | 43 | /** 44 | * Create an {@link OpQueue} populated with a {@link Op#DELETE} 45 | * and a {@link Op#INSERT} 46 | * @param orig 47 | * @param target 48 | */ 49 | public ReplaceOpQueue(byte[] orig, byte[] target) { 50 | if(orig.length > 0) 51 | prepare(new Op(Op.DELETE, orig.length, orig)); 52 | if(target.length > 0) 53 | prepare(new Op(Op.INSERT, target.length, target)); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/q/UnchunkingOpQueue.java: -------------------------------------------------------------------------------- 1 | package org.badiff.q; 2 | 3 | import java.util.Collections; 4 | import java.util.Comparator; 5 | 6 | import org.badiff.Op; 7 | 8 | public class UnchunkingOpQueue extends FilterOpQueue { 9 | 10 | protected static final Comparator OP_ORDER = new Comparator() { 11 | @Override 12 | public int compare(Op o1, Op o2) { 13 | Byte b1 = o1.getOp(); 14 | Byte b2 = o2.getOp(); 15 | return b1.compareTo(b2); 16 | } 17 | }; 18 | 19 | public UnchunkingOpQueue(OpQueue source) { 20 | super(source); 21 | } 22 | 23 | @Override 24 | protected boolean pull() { 25 | if(!require(1)) 26 | return flush(); 27 | 28 | if(filtering.get(0).getOp() == Op.NEXT) 29 | return flush(); 30 | 31 | for(Op e = source.poll(); e != null; e = source.poll()) { 32 | filtering.add(e); 33 | if(e.getOp() == Op.NEXT) { 34 | Collections.sort(filtering, OP_ORDER); 35 | return flush(); 36 | } 37 | } 38 | 39 | Collections.sort(filtering, OP_ORDER); 40 | return flush(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/q/UndoOpQueue.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.q; 31 | 32 | import org.badiff.Op; 33 | 34 | public class UndoOpQueue extends FilterOpQueue { 35 | 36 | public UndoOpQueue(OpQueue source) { 37 | super(source); 38 | } 39 | 40 | @Override 41 | protected boolean pull() { 42 | if(!require(1)) 43 | return flush(); 44 | Op e = filtering.remove(0); 45 | Op u; 46 | if(e.getOp() == Op.DELETE) 47 | u = new Op(Op.INSERT, e.getRun(), e.getData()); 48 | else if(e.getOp() == Op.INSERT) 49 | u = new Op(Op.DELETE, e.getRun(), e.getData()); 50 | else 51 | u = e; 52 | prepare(u); 53 | return true; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/q/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Extensions of {@link org.badiff.q.OpQueue} that implement various ways 3 | * of processing sequences of edit operations. 4 | */ 5 | package org.badiff.q; -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/util/Serials.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.util; 31 | 32 | import java.io.ByteArrayInputStream; 33 | import java.io.ByteArrayOutputStream; 34 | import java.io.IOException; 35 | 36 | import org.badiff.io.RuntimeIOException; 37 | import org.badiff.io.Serialization; 38 | 39 | public class Serials { 40 | 41 | public static byte[] serialize(Serialization serial, Class type, T object) { 42 | ByteArrayOutputStream b = new ByteArrayOutputStream(); 43 | try { 44 | serial.writeObject(b, type, object); 45 | } catch(IOException ioe) { 46 | throw new RuntimeIOException(ioe); 47 | } 48 | return b.toByteArray(); 49 | } 50 | 51 | public static T deserialize(Serialization serial, Class type, byte[] buf) { 52 | ByteArrayInputStream b = new ByteArrayInputStream(buf); 53 | try { 54 | return serial.readObject(b, type); 55 | } catch(IOException ioe) { 56 | throw new RuntimeIOException(ioe); 57 | } 58 | } 59 | 60 | private Serials() { 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /badiff/src/main/java/org/badiff/util/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Utility classes 3 | */ 4 | package org.badiff.util; -------------------------------------------------------------------------------- /badiff/src/test/java/org/badiff/ByteArrayDiffsTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff; 31 | 32 | import java.util.Arrays; 33 | 34 | import org.badiff.io.DefaultSerialization; 35 | import org.badiff.io.Serialization; 36 | import org.junit.Assert; 37 | import org.junit.Test; 38 | 39 | public class ByteArrayDiffsTest { 40 | 41 | protected Serialization serial; 42 | 43 | public ByteArrayDiffsTest() { 44 | this(DefaultSerialization.newInstance()); 45 | } 46 | 47 | protected ByteArrayDiffsTest(Serialization serial) { 48 | this.serial = serial; 49 | 50 | } 51 | 52 | @Test 53 | public void testDiff() throws Exception { 54 | String orig = "Hello world!"; 55 | String target = "Hellish cruel world!"; 56 | 57 | byte[] diff = ByteArrayDiffs.diff(orig.getBytes(), target.getBytes()); 58 | System.out.println("diff:" + diff.length); 59 | 60 | byte[] udiff = ByteArrayDiffs.udiff(orig.getBytes(), target.getBytes()); 61 | System.out.println("udiff:" + udiff.length); 62 | } 63 | 64 | @Test 65 | public void testDiff_issue_2() throws Exception { 66 | byte[] prior = new byte[2270]; 67 | byte[] current = new byte[2281]; 68 | ByteArrayDiffs.diff(prior, current); 69 | } 70 | 71 | @Test 72 | public void testUndo() throws Exception { 73 | byte[] orig = "Hello World".getBytes(); 74 | byte[] target = "Hellish Cruel World".getBytes(); 75 | 76 | byte[] diff = ByteArrayDiffs.diff(orig, target); 77 | 78 | byte[] undone = ByteArrayDiffs.undo(target, diff); 79 | 80 | Assert.assertTrue(Arrays.equals(orig, undone)); 81 | } 82 | 83 | @Test 84 | public void testZeroLengthOriginal() { 85 | ByteArrayDiffs.diff(new byte[0], new byte[1]); 86 | } 87 | 88 | @Test 89 | public void testZeroLengthTarget() { 90 | ByteArrayDiffs.diff(new byte[1], new byte[0]); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /badiff/src/test/java/org/badiff/DiffsTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff; 31 | 32 | import org.badiff.imp.MemoryDiff; 33 | import org.badiff.q.GraphOpQueue; 34 | import org.badiff.q.OpQueue; 35 | import org.badiff.util.Diffs; 36 | import org.junit.Assert; 37 | import org.junit.Test; 38 | 39 | public class DiffsTest { 40 | 41 | @Test 42 | public void testDiff() throws Exception { 43 | String orig = "Hello world!"; 44 | String target = "Hellish cruel world!"; 45 | 46 | OpQueue diff = Diffs.queue(orig.getBytes(), target.getBytes()); 47 | diff = new GraphOpQueue(diff, 1024); 48 | 49 | MemoryDiff md = new MemoryDiff(diff); 50 | 51 | byte[] result = Diffs.apply(md, orig.getBytes()); 52 | 53 | 54 | Assert.assertEquals(target, new String(result)); 55 | } 56 | 57 | @Test 58 | public void testDiffFromEmpty() throws Exception { 59 | String orig = ""; 60 | String target = "Hellish cruel world!"; 61 | 62 | OpQueue diff = Diffs.queue(orig.getBytes(), target.getBytes()); 63 | diff = new GraphOpQueue(diff, 1024); 64 | 65 | MemoryDiff md = new MemoryDiff(diff); 66 | 67 | byte[] result = Diffs.apply(md, orig.getBytes()); 68 | 69 | 70 | Assert.assertEquals(target, new String(result)); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /badiff/src/test/java/org/badiff/FileDiffsTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff; 31 | 32 | import java.io.File; 33 | import java.io.FileInputStream; 34 | import java.io.FileOutputStream; 35 | import java.io.InputStream; 36 | import java.io.OutputStream; 37 | import java.util.concurrent.TimeUnit; 38 | 39 | import org.badiff.imp.FileDiff; 40 | import org.badiff.util.Streams; 41 | 42 | public class FileDiffsTest { 43 | // @Test 44 | public void testPerformanceBig() throws Exception { 45 | final int SIZE = 50 * 1024 * 1024; 46 | 47 | File orig = File.createTempFile("orig", ".tmp"); 48 | orig.deleteOnExit(); 49 | 50 | File target = File.createTempFile("target", ".tmp"); 51 | target.deleteOnExit(); 52 | 53 | InputStream random = new FileInputStream("/dev/urandom"); 54 | 55 | OutputStream out; 56 | 57 | Streams.copy(random, out = new FileOutputStream(orig), SIZE); out.close(); 58 | Streams.copy(random, out = new FileOutputStream(target), SIZE); out.close(); 59 | 60 | random.close(); 61 | 62 | long start = System.nanoTime(); 63 | FileDiff fd = FileDiffs.mdiff(orig, target); 64 | long end = System.nanoTime(); 65 | 66 | System.out.println("Computed FileDiff for " + SIZE + " bytes in " + TimeUnit.MILLISECONDS.convert(end - start, TimeUnit.NANOSECONDS) + "ms"); 67 | 68 | fd.delete(); 69 | orig.delete(); 70 | target.delete(); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /badiff/src/test/java/org/badiff/alg/AdjustableInertialGraphBehaviorTest.java: -------------------------------------------------------------------------------- 1 | package org.badiff.alg; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | import org.junit.Assert; 10 | import org.junit.Test; 11 | import org.junit.runner.RunWith; 12 | import org.junit.runners.Parameterized; 13 | import org.junit.runners.Parameterized.Parameters; 14 | 15 | @RunWith(Parameterized.class) 16 | public class AdjustableInertialGraphBehaviorTest { 17 | @Parameters() 18 | public static Iterable params() throws IOException { 19 | List p = new ArrayList(); 20 | BufferedReader br = new BufferedReader(new InputStreamReader(InertialGraphBehaviorTest.class.getResourceAsStream("ad_inertial.txt"))); 21 | for(String line = br.readLine(); line != null; line = br.readLine()) { 22 | String[] s = line.split("\t"); 23 | s[0] = s[0].replaceAll("\"", ""); 24 | s[1] = s[1].replaceAll("\"", ""); 25 | p.add(s); 26 | } 27 | return p; 28 | } 29 | 30 | private String orig; 31 | private String target; 32 | private String summary; 33 | 34 | public AdjustableInertialGraphBehaviorTest(String orig, String target, String summary) { 35 | this.orig = orig; 36 | this.target = target; 37 | this.summary = summary; 38 | } 39 | 40 | @Test 41 | public void testGraph() { 42 | Graph g = new AdjustableInertialGraph((orig.length() + 1) * (target.length() + 1)); 43 | g.compute(orig.getBytes(), target.getBytes()); 44 | Assert.assertEquals(summary, g.queue().consummerize()); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /badiff/src/test/java/org/badiff/alg/BehaviorTestDataGenerator.java: -------------------------------------------------------------------------------- 1 | package org.badiff.alg; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.File; 5 | import java.io.FileReader; 6 | import java.io.IOException; 7 | import java.util.Iterator; 8 | import java.util.Set; 9 | import java.util.TreeSet; 10 | 11 | import org.apache.commons.io.FileUtils; 12 | 13 | public class BehaviorTestDataGenerator { 14 | protected static Set gatherStrings() throws IOException { 15 | Set strings = new TreeSet(); 16 | 17 | for(File java : FileUtils.listFiles(new File("src"), new String[]{"java"}, true)) { 18 | BufferedReader br = new BufferedReader(new FileReader(java)); 19 | try { 20 | for(String line = br.readLine(); line != null; line = br.readLine()) { 21 | line = line.replaceAll("\"", ""); 22 | line = line.trim().replaceAll("\\s+", " "); 23 | if(line.length() > 4) 24 | strings.add(line); 25 | } 26 | } finally { 27 | br.close(); 28 | } 29 | } 30 | 31 | return strings; 32 | } 33 | 34 | public static void main(String[] args) throws Exception { 35 | Iterator si = gatherStrings().iterator(); 36 | 37 | Graph graph = new InertialGraph(200 * 200); 38 | 39 | String prev = si.next(); 40 | while(si.hasNext()) { 41 | String next = si.next(); 42 | 43 | graph.compute(prev.getBytes(), next.getBytes()); 44 | 45 | System.out.print("\"" + prev + "\"\t"); 46 | System.out.print("\"" + next + "\"\t"); 47 | System.out.println(graph.queue().consummerize()); 48 | 49 | 50 | prev = next; 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /badiff/src/test/java/org/badiff/alg/EditGraphBehaviorTest.java: -------------------------------------------------------------------------------- 1 | package org.badiff.alg; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | import org.junit.Assert; 10 | import org.junit.Test; 11 | import org.junit.runner.RunWith; 12 | import org.junit.runners.Parameterized; 13 | import org.junit.runners.Parameterized.Parameters; 14 | 15 | @RunWith(Parameterized.class) 16 | public class EditGraphBehaviorTest { 17 | @Parameters() 18 | public static Iterable params() throws IOException { 19 | List p = new ArrayList(); 20 | BufferedReader br = new BufferedReader(new InputStreamReader(InertialGraphBehaviorTest.class.getResourceAsStream("edit.txt"))); 21 | for(String line = br.readLine(); line != null; line = br.readLine()) { 22 | String[] s = line.split("\t"); 23 | s[0] = s[0].replaceAll("\"", ""); 24 | s[1] = s[1].replaceAll("\"", ""); 25 | p.add(s); 26 | } 27 | return p; 28 | } 29 | 30 | private String orig; 31 | private String target; 32 | private String summary; 33 | 34 | public EditGraphBehaviorTest(String orig, String target, String summary) { 35 | this.orig = orig; 36 | this.target = target; 37 | this.summary = summary; 38 | } 39 | 40 | @Test 41 | public void testGraph() { 42 | Graph g = new EditGraph((orig.length() + 1) * (target.length() + 1)); 43 | g.compute(orig.getBytes(), target.getBytes()); 44 | Assert.assertEquals(summary, g.queue().consummerize()); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /badiff/src/test/java/org/badiff/alg/InertialGraphBehaviorTest.java: -------------------------------------------------------------------------------- 1 | package org.badiff.alg; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | import org.junit.Assert; 10 | import org.junit.Test; 11 | import org.junit.runner.RunWith; 12 | import org.junit.runners.Parameterized; 13 | import org.junit.runners.Parameterized.Parameters; 14 | 15 | @RunWith(Parameterized.class) 16 | public class InertialGraphBehaviorTest { 17 | @Parameters() 18 | public static Iterable params() throws IOException { 19 | List p = new ArrayList(); 20 | BufferedReader br = new BufferedReader(new InputStreamReader(InertialGraphBehaviorTest.class.getResourceAsStream("inertial.txt"))); 21 | for(String line = br.readLine(); line != null; line = br.readLine()) { 22 | String[] s = line.split("\t"); 23 | s[0] = s[0].replaceAll("\"", ""); 24 | s[1] = s[1].replaceAll("\"", ""); 25 | p.add(s); 26 | } 27 | return p; 28 | } 29 | 30 | private String orig; 31 | private String target; 32 | private String summary; 33 | 34 | public InertialGraphBehaviorTest(String orig, String target, String summary) { 35 | this.orig = orig; 36 | this.target = target; 37 | this.summary = summary; 38 | } 39 | 40 | @Test 41 | public void testGraph() { 42 | Graph g = new InertialGraph((orig.length() + 1) * (target.length() + 1)); 43 | g.compute(orig.getBytes(), target.getBytes()); 44 | String csum = g.queue().consummerize(); 45 | if(!summary.equals(csum)) 46 | System.out.println("\"" + orig + "\" -> \"" + target + "\" -> " + csum); 47 | Assert.assertEquals(summary, csum); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /badiff/src/test/java/org/badiff/alg/MinNextEditGraphTest.java: -------------------------------------------------------------------------------- 1 | package org.badiff.alg; 2 | 3 | import org.badiff.imp.MemoryDiff; 4 | import org.badiff.io.DefaultSerialization; 5 | import org.badiff.q.OneWayOpQueue; 6 | import org.badiff.util.Diffs; 7 | import org.badiff.util.Serials; 8 | import org.junit.Assert; 9 | import org.junit.Test; 10 | 11 | public class MinNextEditGraphTest { 12 | @Test 13 | public void testGraph() { 14 | byte[] orig = "Hello world!".getBytes(); 15 | byte[] target = "Hellish cruel world!".getBytes(); 16 | 17 | MinNextEditGraph ig = new MinNextEditGraph((orig.length + 1) * (target.length + 1)); 18 | ig.setMinNext(5); 19 | ig.compute(orig, target); 20 | 21 | MemoryDiff imd = new MemoryDiff(new OneWayOpQueue(ig.queue())); 22 | System.out.println(imd); 23 | 24 | byte[] result = Diffs.apply(imd, orig); 25 | System.out.println(new String(result)); 26 | 27 | Assert.assertEquals(new String(target), new String(result)); 28 | 29 | EditGraph g = new EditGraph((orig.length + 1) * (target.length + 1)); 30 | g.compute(orig, target); 31 | 32 | MemoryDiff emd = new MemoryDiff(g.queue()); 33 | System.out.println(emd); 34 | 35 | byte[] simd = Serials.serialize(DefaultSerialization.newInstance(), MemoryDiff.class, imd); 36 | byte[] semd = Serials.serialize(DefaultSerialization.newInstance(), MemoryDiff.class, emd); 37 | 38 | System.out.println("minnext diff length:" + simd.length); 39 | System.out.println("edit diff length:" + semd.length); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /badiff/src/test/java/org/badiff/imp/BadiffFileDiffTest.java: -------------------------------------------------------------------------------- 1 | package org.badiff.imp; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.ByteArrayOutputStream; 5 | import java.io.File; 6 | import java.util.Arrays; 7 | import org.apache.commons.io.FileUtils; 8 | import org.badiff.q.OpQueue; 9 | import org.junit.Assert; 10 | import org.junit.Test; 11 | 12 | public class BadiffFileDiffTest { 13 | @Test 14 | public void testDiffWithRewind() throws Exception { 15 | File orig = File.createTempFile("orig", ".tmp"); 16 | File target = File.createTempFile("target", ".tmp"); 17 | 18 | FileUtils.write(orig, "12345678901234567890abcdefghjiklmnopqrstuvwxyz"); 19 | FileUtils.write(target, "abcdefghjiklmnopqrstuvwxyz12345678901234567890"); 20 | 21 | BadiffFileDiff diff = new BadiffFileDiff(File.createTempFile("diff", ".tmp")); 22 | diff.diff(orig, target); 23 | 24 | MemoryDiff md = new MemoryDiff(diff.queue()); 25 | System.out.println(md); 26 | 27 | File applied = File.createTempFile("applied", ".tmp"); 28 | diff.apply(orig, applied); 29 | 30 | try { 31 | Assert.assertEquals(FileUtils.readFileToString(target), FileUtils.readFileToString(applied)); 32 | Assert.assertTrue(diff.header().getStats().getRewindCount() > 0); 33 | } finally { 34 | orig.delete(); 35 | target.delete(); 36 | diff.delete(); 37 | applied.delete(); 38 | } 39 | } 40 | @Test 41 | public void testDiffFromEmpty() throws Exception { 42 | File orig = File.createTempFile("orig", ".tmp"); 43 | File target = File.createTempFile("target", ".tmp"); 44 | 45 | FileUtils.write(orig, ""); 46 | FileUtils.write(target, "abcdefghjiklmnopqrstuvwxyz12345678901234567890"); 47 | 48 | BadiffFileDiff diff = new BadiffFileDiff(File.createTempFile("diff", ".tmp")); 49 | diff.diff(orig, target); 50 | 51 | MemoryDiff md = new MemoryDiff(diff.queue()); 52 | System.out.println(md); 53 | 54 | File applied = File.createTempFile("applied", ".tmp"); 55 | diff.apply(orig, applied); 56 | 57 | try { 58 | Assert.assertEquals(FileUtils.readFileToString(target), FileUtils.readFileToString(applied)); 59 | } finally { 60 | orig.delete(); 61 | target.delete(); 62 | diff.delete(); 63 | applied.delete(); 64 | } 65 | } 66 | 67 | @Test 68 | public void testPerformance() throws Exception { 69 | final int SIZE = 2048 * 2048; 70 | 71 | ByteArrayOutputStream orig = new ByteArrayOutputStream(SIZE); 72 | ByteArrayOutputStream target = new ByteArrayOutputStream(SIZE); 73 | 74 | for(int i = 0; i < SIZE; i++) { 75 | orig.write((int)(256 * Math.random())); 76 | target.write((int)(256 * Math.random())); 77 | } 78 | 79 | BadiffFileDiff diff = new BadiffFileDiff(File.createTempFile("badiff", ".diff")); 80 | File origFile = File.createTempFile("orig", ".tmp"); 81 | File targetFile = File.createTempFile("target", ".tmp"); 82 | FileUtils.writeByteArrayToFile(origFile, orig.toByteArray()); 83 | FileUtils.writeByteArrayToFile(targetFile, target.toByteArray()); 84 | diff.diff(origFile, targetFile); 85 | 86 | OpQueue q = diff.queue(); 87 | 88 | ByteArrayOutputStream result = new ByteArrayOutputStream(SIZE); 89 | 90 | q.apply( 91 | new ByteArrayInputStream(orig.toByteArray()), 92 | result); 93 | 94 | Assert.assertTrue(Arrays.equals(target.toByteArray(), result.toByteArray())); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /badiff/src/test/java/org/badiff/io/DefaultFileDiffTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * badiff - byte array diff - fast pure-java byte-level diffing 3 | * 4 | * Copyright (c) 2013, Robin Kirkman All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without modification, 7 | * are permitted provided that the following conditions are met: 8 | * 9 | * 1) Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * 2) Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 3) Neither the name of the badiff nor the names of its contributors may be 15 | * used to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 | * THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package org.badiff.io; 31 | 32 | import java.io.File; 33 | import java.util.Arrays; 34 | 35 | import org.badiff.imp.FileDiff; 36 | import org.badiff.util.Diffs; 37 | import org.junit.Assert; 38 | import org.junit.Test; 39 | 40 | public class DefaultFileDiffTest { 41 | 42 | @Test 43 | public void testWriteRead() throws Exception { 44 | byte[] orig = "Hello world!".getBytes(); 45 | byte[] target = "Hellish cruel world!".getBytes(); 46 | 47 | FileDiff fd = new FileDiff(File.createTempFile("filediff", ".tmp")); 48 | fd.store(Diffs.improved(Diffs.queue(orig, target))); 49 | 50 | byte[] result = Diffs.apply(fd.queue(), orig); 51 | 52 | fd.delete(); 53 | 54 | System.out.println("target:" + new String(target)); 55 | System.out.println("result:" + new String(result)); 56 | 57 | Assert.assertTrue(Arrays.equals(target, result)); 58 | 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /badiff/src/test/java/org/badiff/io/DefaultSerializationTest.java: -------------------------------------------------------------------------------- 1 | package org.badiff.io; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.ByteArrayOutputStream; 5 | import java.io.DataInputStream; 6 | import java.io.DataOutputStream; 7 | 8 | import org.junit.Assert; 9 | import org.junit.Test; 10 | 11 | public class DefaultSerializationTest { 12 | @Test 13 | public void testLongs() throws Exception { 14 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); 15 | DataOutputStream dout = new DataOutputStream(bout); 16 | 17 | DefaultSerialization serial = DefaultSerialization.newInstance(); 18 | 19 | serial.writeLong(dout, 1); 20 | serial.writeLong(dout, -1); 21 | serial.writeLong(dout, 159); 22 | serial.writeLong(dout, -159); 23 | serial.writeLong(dout, Integer.MAX_VALUE + 1L); 24 | serial.writeLong(dout, Integer.MIN_VALUE - 1L); 25 | 26 | DataInputStream din = new DataInputStream(new ByteArrayInputStream(bout.toByteArray())); 27 | Assert.assertEquals(1, serial.readLong(din)); 28 | Assert.assertEquals(-1, serial.readLong(din)); 29 | Assert.assertEquals(159, serial.readLong(din)); 30 | Assert.assertEquals(-159, serial.readLong(din)); 31 | Assert.assertEquals(Integer.MAX_VALUE + 1L, serial.readLong(din)); 32 | Assert.assertEquals(Integer.MIN_VALUE - 1L, serial.readLong(din)); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /badiff/src/test/java/org/badiff/q/RewindingOpQueueTest.java: -------------------------------------------------------------------------------- 1 | package org.badiff.q; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | 5 | import org.badiff.Diff; 6 | import org.badiff.imp.MemoryDiff; 7 | import org.badiff.io.DefaultSerialization; 8 | import org.badiff.io.RandomInputStream; 9 | import org.badiff.util.Serials; 10 | import org.junit.Assert; 11 | import org.junit.Test; 12 | 13 | public class RewindingOpQueueTest { 14 | @Test 15 | public void test() throws Exception { 16 | String orig = "12345678901234567890abcdefghjiklmnopqrstuvwxyz"; 17 | String target = "abcdefghjiklmnopqrstuvwxyz12345678901234567890"; 18 | 19 | OpQueue q = new ReplaceOpQueue(orig.getBytes(), target.getBytes()); 20 | q = new GraphOpQueue(q, Diff.DEFAULT_CHUNK); 21 | MemoryDiff md = new MemoryDiff(q); 22 | 23 | System.out.println(md + ": " + Serials.serialize(DefaultSerialization.newInstance(), MemoryDiff.class, md).length); 24 | 25 | 26 | q = md.queue(); 27 | q = new RewindingOpQueue(q); 28 | 29 | MemoryDiff rwd = new MemoryDiff(q); 30 | 31 | System.out.println(rwd + ": " + Serials.serialize(DefaultSerialization.newInstance(), MemoryDiff.class, rwd).length); 32 | 33 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); 34 | rwd.apply(new RandomInputStream(orig.getBytes()), bout); 35 | 36 | Assert.assertEquals(target, new String(bout.toByteArray())); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /badiff/src/test/java/org/badiff/util/Integers2Test.java: -------------------------------------------------------------------------------- 1 | package org.badiff.util; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | public class Integers2Test { 7 | @Test 8 | public void testNegative() { 9 | Assert.assertEquals(0L, Integers.negative(1L)); 10 | Assert.assertEquals(0L, Integers.negative(0L)); 11 | Assert.assertEquals(-1L, Integers.negative(-1L)); 12 | 13 | Assert.assertEquals(0, Integers.negative(1)); 14 | Assert.assertEquals(0, Integers.negative(0)); 15 | Assert.assertEquals(-1, Integers.negative(-1)); 16 | } 17 | 18 | @Test 19 | public void testMin() { 20 | Assert.assertEquals(-1L, Integers.min(-1L, -1L)); 21 | Assert.assertEquals(-1L, Integers.min(-1L, 0L)); 22 | Assert.assertEquals(-1L, Integers.min(-1L, 1L)); 23 | Assert.assertEquals(-1L, Integers.min(0L, -1L)); 24 | Assert.assertEquals(0L, Integers.min(0L, 0L)); 25 | Assert.assertEquals(0L, Integers.min(0L, 1L)); 26 | Assert.assertEquals(-1L, Integers.min(1L, -1L)); 27 | Assert.assertEquals(0L, Integers.min(1L, 0L)); 28 | Assert.assertEquals(1L, Integers.min(1L, 1L)); 29 | 30 | Assert.assertEquals(-1, Integers.min(-1, -1)); 31 | Assert.assertEquals(-1, Integers.min(-1, 0)); 32 | Assert.assertEquals(-1, Integers.min(-1, 1)); 33 | Assert.assertEquals(-1, Integers.min(0, -1)); 34 | Assert.assertEquals(0, Integers.min(0, 0)); 35 | Assert.assertEquals(0, Integers.min(0, 1)); 36 | Assert.assertEquals(-1, Integers.min(1, -1)); 37 | Assert.assertEquals(0, Integers.min(1, 0)); 38 | Assert.assertEquals(1, Integers.min(1, 1)); 39 | } 40 | 41 | @Test 42 | public void testMax() { 43 | Assert.assertEquals(-1L, Integers.max(-1L, -1L)); 44 | Assert.assertEquals(0L, Integers.max(-1L, 0L)); 45 | Assert.assertEquals(1L, Integers.max(-1L, 1L)); 46 | Assert.assertEquals(0L, Integers.max(0L, -1L)); 47 | Assert.assertEquals(0L, Integers.max(0L, 0L)); 48 | Assert.assertEquals(1L, Integers.max(0L, 1L)); 49 | Assert.assertEquals(1L, Integers.max(1L, -1L)); 50 | Assert.assertEquals(1L, Integers.max(1L, 0L)); 51 | Assert.assertEquals(1L, Integers.max(1L, 1L)); 52 | 53 | Assert.assertEquals(-1, Integers.max(-1, -1)); 54 | Assert.assertEquals(0, Integers.max(-1, 0)); 55 | Assert.assertEquals(1, Integers.max(-1, 1)); 56 | Assert.assertEquals(0, Integers.max(0, -1)); 57 | Assert.assertEquals(0, Integers.max(0, 0)); 58 | Assert.assertEquals(1, Integers.max(0, 1)); 59 | Assert.assertEquals(1, Integers.max(1, -1)); 60 | Assert.assertEquals(1, Integers.max(1, 0)); 61 | Assert.assertEquals(1, Integers.max(1, 1)); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /badiff/src/test/java/org/badiff/util/IntegersTest.java: -------------------------------------------------------------------------------- 1 | package org.badiff.util; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.junit.runners.Parameterized; 10 | import org.junit.runners.Parameterized.Parameters; 11 | 12 | @RunWith(Parameterized.class) 13 | public class IntegersTest { 14 | @Parameters 15 | public static Iterable params() { 16 | List p = new ArrayList(); 17 | p.add(new Object[] {0L}); 18 | for(long i = 1; i != 0; i = i << 1) 19 | p.add(new Object[] {i}); 20 | return p; 21 | } 22 | 23 | private long i; 24 | 25 | public IntegersTest(Long i) { 26 | this.i = i; 27 | } 28 | 29 | @Test 30 | public void testAny() { 31 | Assert.assertEquals(i == 0 ? 0 : -1, Integers.any(i)); 32 | Assert.assertEquals(((int) i) == 0 ? 0 : -1, Integers.any((int) i)); 33 | Assert.assertEquals(((short) i) == 0 ? 0 : -1, Integers.any((short) i)); 34 | } 35 | 36 | @Test 37 | public void testAll() { 38 | long i = ~this.i; 39 | Assert.assertEquals(i == -1 ? -1 : 0, Integers.all(i)); 40 | Assert.assertEquals(((int) i) == -1 ? -1 : 0, Integers.all((int) i)); 41 | Assert.assertEquals(((short) i) == -1 ? -1 : 0, Integers.all((short) i)); 42 | } 43 | } 44 | --------------------------------------------------------------------------------