All pastes #508099 Raw Edit

Untitled

public text v1 · immutable
·published 2007-05-24 21:26 UTC
rendered paste body
package org.semanticdesktop.nepomuk.psew.sandbox;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class ApertureUtil {

	public static void main(String[] args) {

		String path = "/home/sebastocha/Workspace2/org.semanticdesktop.aperture/output.trix";
		InputStream fis = null;
		try {
			fis = new FileInputStream(new File(path));
			fis = cleanStream(fis);
			FileOutputStream fos = new FileOutputStream(new File(path + ".clean"));
			copy(fis, fos);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static void copy(InputStream fis, FileOutputStream fos) throws IOException {
		try {
			try {
				byte[] buf = new byte[1024 * 10];
				int len;
				while ((len = fis.read(buf)) > 0) {
					fos.write(buf, 0, len);
				}
			} finally {
				fos.close();
			}
		} finally {
			fis.close();
		}

	}

	public static InputStream cleanStream(final InputStream is) {
		return new InputStream() {
			byte[] buf = null;

			int pos;

			@Override
			public int read() throws IOException {
				int result = -1;
				while (true) {
					if (buf != null) {
						if (pos < buf.length) {
							result = buf[pos++];
							break;
						}
					}
					pos = 0;
					int b = is.read();
					
					if (b== 13 || b==10 || b==9 ) {
						result = b;
						break;
					} 
					if (b>=0 && b<=31) {
						result = 32;
						break;
					 }
					if (b > 127)
						buf = ("&#" + b + ";").getBytes();
					else {
						result = b;
						buf = null;
						break;
					}
				}
				return result;
			}
		};
	}
}