<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MacLovin &#187; java</title>
	<atom:link href="http://www.maclovin.de/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.maclovin.de</link>
	<description>An Apple a day keeps the Windows away</description>
	<lastBuildDate>Tue, 17 Aug 2010 21:45:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to patch binary files with Java</title>
		<link>http://www.maclovin.de/2010/02/how-to-patch-binary-files-with-java/</link>
		<comments>http://www.maclovin.de/2010/02/how-to-patch-binary-files-with-java/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 21:09:36 +0000</pubDate>
		<dc:creator>Dennis</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://www.maclovin.de/?p=500</guid>
		<description><![CDATA[Recently, I wanted to create an &#8220;intelligent&#8221; binary patcher, which not only replaces some chunk of binary data at a file&#8217;s predefined offset, but instead performs search and replace. Here is the Java class I came up with. import java.io.*; import java.math.BigInteger; public class BinaryPatcher...]]></description>
			<content:encoded><![CDATA[<p>Recently, I wanted to create an &#8220;intelligent&#8221; binary patcher, which not only replaces some chunk of binary data at a file&#8217;s predefined offset, but instead performs search and replace. Here is the Java class I came up with.</p>
<pre class="brush: java">import java.io.*;
import java.math.BigInteger;

public class BinaryPatcher {
	private String content;

	private final String filename;
	private final String searchPattern;
	private final String replacePattern;

	public BinaryPatcher(String filename, String searchPattern,
			String replacePattern) throws IOException {

		this.filename = filename;
		this.searchPattern = convertHexStringToBinaryString(searchPattern);
		this.replacePattern = convertHexStringToBinaryString(replacePattern);

		initializeContent();
	}

	public boolean isPatchable() {
		return getContent().contains(searchPattern);
	}

	public void patch() throws IOException {
		if (!isPatchable()) {
			throw new IOException("Search pattern not found");
		}

		String originalContent = getContent();
		String patchedContent = originalContent.replace(searchPattern,
				replacePattern);

		saveStringToFile(patchedContent, filename);
	}

	public void createBackup(String filename) throws IOException {
		saveStringToFile(getContent(), filename);
	}

	private static String convertHexStringToBinaryString(String theHexString) {
		byte[] byteSequence = new BigInteger(theHexString, 16).toByteArray();
		return new String(byteSequence);
	}

	private void initializeContent() throws IOException {
		byte[] buffer = new byte[(int) new File(filename).length()];
		FileInputStream in = new FileInputStream(filename);
		in.read(buffer);
		in.close();
		content = new String(buffer);
	}

	private void saveStringToFile(String content, String filename)
			throws IOException {
		FileOutputStream out = new FileOutputStream(filename);
		out.write(content.getBytes());
		out.close();
	}

	private String getContent() {
		return content;
	}
}</pre>
<p>The usage is pretty straight forward. Let&#8217;s have a look at an basic example:</p>
<pre class="brush: java">class Tester {
	public static void main(String[] args) {
		try {
			String filename = "patchme";
			BinaryPatcher p = new BinaryPatcher(filename, "AB00FF14", "AB11FF14");

			if (p.isPatchable()) {
				p.createBackup(filename + ".org");
				p.patch();
			}
		} catch(IOException e) {
			System.out.println("ERROR: " + e.getMessage());
		}
	}
}</pre>
<p>We try to patch the file <em>patchme</em>. The search and replace binary data is given by hex sequences, in this example <em>AB00FF14</em> is replaced by <em>AB11FF14</em>. If the file is patchable, i.e. it contains the search pattern, a backup named <em>patchme.org</em> is created an finally, the original file is modified.</p>
<p>That&#8217;s all. Pretty simple but useful. You can download the BinaryPatcher class <a href="http://www.maclovin.de/wp-content/uploads/2010/02/BinaryPatcher.java">here</a>.</p>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://www.maclovin.de/2010/02/how-to-patch-binary-files-with-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
