274 lines
6 KiB
Java
274 lines
6 KiB
Java
import java.io.BufferedReader;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileOutputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.io.PrintStream;
|
|
import java.*;
|
|
import java.io.*;
|
|
import java.util.*;
|
|
|
|
|
|
public class Datei {
|
|
int ib = 0;
|
|
int iLength = 0;
|
|
byte buffer[] = new byte[1000000];
|
|
byte searchBuffer[] = new byte[1000000];
|
|
String dateiname = "";
|
|
String pfad= "";
|
|
|
|
public Datei () {
|
|
dateiname = "unbenannt.txt";
|
|
}
|
|
public Datei (File file) {
|
|
try {
|
|
buffer = new byte[(int) file.length()+100000];
|
|
searchBuffer = new byte[(int) file.length()+100000];
|
|
FileInputStream inFile = new FileInputStream(file);
|
|
//BufferedReader inData = new BufferedReader(new InputStreamReader(inFile));
|
|
|
|
//if (inData.ready()) {
|
|
iLength = inFile.read(buffer);
|
|
for (int i=0; i< iLength; i++) searchBuffer[i]=0;
|
|
|
|
//inData.close();
|
|
inFile.close();
|
|
dateiname = file.getName();
|
|
pfad = file.getAbsolutePath();
|
|
|
|
//} else {
|
|
//System.out.println("Fehler beim Dateiöffnen");
|
|
//}
|
|
}
|
|
catch (Exception ex) {
|
|
System.out.println("Fehler beim Dateiöffnen");
|
|
}
|
|
|
|
}
|
|
|
|
public void saveFile(File file) {
|
|
try {
|
|
FileOutputStream outFile = new FileOutputStream(file);
|
|
//BufferedReader inData = new BufferedReader(new InputStreamReader(inFile));
|
|
|
|
//if (inData.ready()) {
|
|
outFile.write(buffer,0,iLength);
|
|
//inData.close();
|
|
outFile.close();
|
|
dateiname = file.getName();
|
|
pfad = file.getAbsolutePath();
|
|
|
|
//} else {
|
|
//System.out.println("Fehler beim Dateiöffnen");
|
|
//}
|
|
}
|
|
catch (Exception ex) {
|
|
System.out.println("Fehler beim Dateiöffnen");
|
|
}
|
|
|
|
}
|
|
|
|
public String getFileName()
|
|
{
|
|
return dateiname;
|
|
}
|
|
|
|
public String getPathName()
|
|
{
|
|
return dateiname;
|
|
}
|
|
|
|
public String getByteBinary(int i)
|
|
{
|
|
String s = "";
|
|
if (i<this.iLength) {
|
|
byte c = buffer[i];
|
|
String p = "00000000";
|
|
s = Integer.toBinaryString(c);
|
|
if (s.length()==32) {
|
|
s = s.substring(24);
|
|
}
|
|
s = p.substring(0,8-s.length())+s;
|
|
}
|
|
return s;
|
|
}
|
|
|
|
public void setByteBinary(int i, String s)
|
|
{
|
|
if(i<iLength) buffer[i]=(byte) Integer.parseInt(s,2);
|
|
}
|
|
|
|
public String getByteHex(int i)
|
|
{
|
|
String s = "";
|
|
if (i<this.iLength) {
|
|
byte c = buffer[i];
|
|
s = Integer.toHexString(c);
|
|
s = s.toUpperCase();
|
|
if (s.length()<2) {
|
|
s = "0"+s;
|
|
}
|
|
if (s.length()==8) {
|
|
s = s.substring(6);
|
|
}
|
|
}
|
|
return s;
|
|
}
|
|
|
|
public void setByteHex(int i, String s)
|
|
{
|
|
if (i<this.iLength) {
|
|
buffer[i]=(byte) Integer.parseInt(s,16);
|
|
}
|
|
}
|
|
|
|
public String getByteChar(int i)
|
|
{
|
|
String s="";
|
|
if (i<this.iLength) {
|
|
int j = (0x000000FF & ((int)buffer[i]));
|
|
s = ""+(char) j;
|
|
}
|
|
return s;
|
|
}
|
|
|
|
public void setByteChar(int i, char c)
|
|
{
|
|
if (i<this.iLength) {
|
|
buffer[i] = (byte) c;
|
|
}
|
|
}
|
|
|
|
public byte getByte(int i)
|
|
{
|
|
if (i<this.iLength) {
|
|
return buffer[i];
|
|
} else {
|
|
return 0;
|
|
} // end of if-else
|
|
|
|
}
|
|
|
|
public byte getSearchByte(int i)
|
|
{
|
|
return searchBuffer[i];
|
|
}
|
|
|
|
public String getBinary()
|
|
{
|
|
String s = "";
|
|
|
|
for (int i = 0; i< iLength; i++)
|
|
{
|
|
s = s + getByteBinary(i);
|
|
|
|
}
|
|
return s;
|
|
}
|
|
|
|
public String getHex()
|
|
{
|
|
String s = "";
|
|
|
|
for (int i = 0; i< iLength; i++)
|
|
{
|
|
s = s + getByteHex(i);
|
|
}
|
|
return s;
|
|
}
|
|
|
|
public int getLength()
|
|
{
|
|
return iLength;
|
|
}
|
|
|
|
public void insertByte(int pos) {
|
|
for (int i=iLength; i>= pos; i--) buffer[i+1]=buffer[i];
|
|
buffer[pos]=0;
|
|
iLength++;
|
|
}
|
|
public void deleteByte(int pos) {
|
|
for (int i=pos; i< iLength; i++) buffer[i]=buffer[i+1];
|
|
buffer[iLength]=0;
|
|
iLength--;
|
|
}
|
|
|
|
|
|
public int search(String s, int modus) {
|
|
byte searchBytes[] = new byte[50];
|
|
int searchLength = 0;
|
|
int i, treffer, laenge=0;
|
|
|
|
for (i=0; i< iLength;i++) searchBuffer[i]=0;
|
|
i = 0;
|
|
|
|
if (modus==1) laenge = 8;
|
|
if (modus==2) laenge = 2;
|
|
if (modus==3) laenge = 1;
|
|
|
|
while(s.length() >= laenge) {
|
|
if (modus==1) {
|
|
searchBytes[i] = (byte) Integer.parseInt(s.substring(0,8),2);
|
|
}
|
|
if (modus==2) {
|
|
searchBytes[i] = (byte) Integer.parseInt(s.substring(0,2),16);
|
|
}
|
|
if (modus==3) {
|
|
searchBytes[i] = (byte) s.charAt(0);
|
|
}
|
|
if (s.length()>laenge) {
|
|
s = s.substring(laenge);
|
|
} else s = "";
|
|
i++;
|
|
}
|
|
searchLength = i;
|
|
|
|
if (s.length()==0) {
|
|
treffer=0;
|
|
for(int j = 0; j< iLength-searchLength; j++)
|
|
{
|
|
int gefunden = 1;
|
|
for (i = 0; (i < searchLength) && (gefunden==1); i++)
|
|
{
|
|
if (buffer[j+i]!= searchBytes[i]) gefunden = 0;
|
|
}
|
|
|
|
if (gefunden==1) {
|
|
for (i=0; i<searchLength; i++) searchBuffer[j+i] = 1;
|
|
treffer++;
|
|
}
|
|
}
|
|
}
|
|
else treffer = -1;
|
|
return treffer;
|
|
}
|
|
|
|
public int compare (File file) {
|
|
int cLength = 0;
|
|
byte cbuffer[];
|
|
int unterschiede=0;
|
|
|
|
for (int i=0; i< iLength;i++) searchBuffer[i]=0;
|
|
try {
|
|
FileInputStream inFile = new FileInputStream(file);
|
|
cbuffer = new byte[(int) file.length()];
|
|
cLength = inFile.read(cbuffer);
|
|
inFile.close();
|
|
if (iLength < cLength) cLength = iLength;
|
|
for (int i=0; i <cLength; i++) {
|
|
if (buffer[i] != cbuffer[i]) {
|
|
searchBuffer[i] = 1;
|
|
unterschiede++;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex) {
|
|
System.out.println("Fehler beim Dateiöffnen");
|
|
unterschiede = -1;
|
|
}
|
|
return unterschiede;
|
|
}
|
|
|
|
|
|
}
|