duc_bit_imp9/Quellcodes/duc_bit_bmphexeditor/Datei.java
2024-12-29 13:43:45 +01:00

291 lines
No EOL
6.3 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[10000000];
byte searchBuffer[] = new byte[10000000];
String dateiname = "";
String pfad= "";
public Datei () {
dateiname = "unbenannt.txt";
}
public Datei (File file) {
try {
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 byte[] getByteArray()
{
return buffer;
}
public String getByteBinary(int i)
{
String s = "";
if (i<this.iLength) {
byte c = buffer[i];
s = Integer.toBinaryString(c);
String p = "00000000";
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 int getUnsignedInt(int i)
{
return (buffer[i] & 0x7F) + (buffer[i] < 0 ? 128 : 0);
}
public int getDWORD(int i)
{
return getUnsignedInt(i)+256*(getUnsignedInt(i+1)+256*(getUnsignedInt(i+2)+256*getUnsignedInt(i+3)));
}
public int getWORD(int i)
{
return getUnsignedInt(i)+256*(getUnsignedInt(i+1));
}
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[] = new byte[10000000];
int unterschiede=0;
for (int i=0; i< iLength;i++) searchBuffer[i]=0;
try {
FileInputStream inFile = new FileInputStream(file);
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;
}
}