Skip to content

Instantly share code, notes, and snippets.

@hankliu5
Last active February 19, 2016 05:53
Show Gist options
  • Save hankliu5/f0631293d52d88442f1f to your computer and use it in GitHub Desktop.
Save hankliu5/f0631293d52d88442f1f to your computer and use it in GitHub Desktop.
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
public class Recover {
public static void main(String[] args) throws IOException{
int[] test1 = {0xff, 0xd8, 0xff, 0xe0};
int[] test2 = {0xff, 0xd8, 0xff, 0xe1};
int[] check = new int[4];
int image = 0;
int counts = 0;
String path = "/home/linux/ieng6/cs11wb/public/HW5/card.raw";
FileInputStream infs = new FileInputStream(path);
BufferedInputStream inbfs = new BufferedInputStream(infs);
while (!checkNewFile(check, test1, test2)) { // to locate the start point.
for (int i = 0; i < 4; i++) {
check[i] = inbfs.read();
}
}
while (check[0] != -1) {
counts++;
FileOutputStream outfs = new FileOutputStream(counts + ".jpg");
BufferedOutputStream outbfs = new BufferedOutputStream(outfs);
do {
for (int i = 0; i < 4; i++) {
outbfs.write(check[i]);
}
for (int i = 0; i < 508; i++) {
outbfs.write(inbfs.read());
}
for (int i = 0; i < 4; i++) {
check[i] = inbfs.read();
}
if (check[0] == -1) {
outbfs.close();
inbfs.close();
break;
}
} while (!checkNewFile(check, test1, test2));
outbfs.close();
}
}
public static boolean checkNewFile(int[] check, int[] test1, int[] test2) {
boolean result = false;
for (int i = 0; i < check.length; i++) {
if (check[i] == test1[i] || check[i] == test2[i]) {
result = true;
}
else {
result = false;
break;
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment