2

I need to get last 22 Bytes as end of central directory in self extracting .exe file in java (No command line, No terminal solutions please). I tried to read content of .exe file using bufferInputStream and got successful but when trying to get last 22 bytes using

BufferInputStream.read(byteArray, 8170, 22);

java is raising exception saying it is closed stream.Any help in this regard would be much appreciated. Thanks.

user2771151
  • 411
  • 1
  • 7
  • 18

3 Answers3

5

I haven't tried this, but I suppose you could use a MappedByteBuffer to read just the last 22 bytes.

File file = new File("/path/to/my/file.bin");

long size = file.length();
FileChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.READ);
MappedByteBuffer buffer = channel.map(MapMode.READ_ONLY, size-22, 22);

Then simply flush your buffer into an array and that's it.

byte[] payload = new byte[22];
buffer.get(payload);
Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
0

You first need to create an FileInputStream from the file.

File exeFile = new File("path/to/your/exe");
long size = exeFile.length();
int readSize = 22;
try {
    FileInputStream stream = new FileInputStream(exeFile);
    stream.skip(size - readSize);
    byte[] buffer = new byte[readSize];
    if(stream.read(buffer) > 0) {
        // process your data
    }
    else {
        // Some errors
    }
    stream.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
Manus Reload
  • 1
  • 2
  • 2
-1

example of code that gives java.io.IOException: Stream Closed You must check the inputstream before

    InputStream fis = new FileInputStream("c:/myfile.exe");
    fis.close(); // only for demonstrating

    // correct but useless
     BufferedInputStream bis = new BufferedInputStream(fis);        

    byte x[]=new byte[100];

    // EXCEPTION: HERE: if fis closed
    bis.read(x,10,10);