1

I am debugging some code that involves transferring files over a network. I have received the binary data and want to confirm that the file is correct by downloading it an opening it in a viewer relevant to the file format.

In modern development environments, we may not have easy access directly to the file system of the development machine, due to containers, orchestration, sandboxing & security measures, etc. If viewed in the debugger as as string and copied into a file from there, the variable contents may be corrupted in the process.

Is it possible to save the binary contents of a byte[] variable directly from the IntelliJ debugger to a file?

Note I am not asking how to do this from within the Java code of the application I am debugging itself (due to difficulties mentioned above), but asking about ways to do this from the IntelliJ debugger, perhaps via a plugin if necessary.

3
  • Beakpoints have this nice "Condition" attribute which allows to define when it should trigger. That condition allows any Java code, so who says that a breakpoint condition can not access a field and save it's content to a file? Just make sure that in the end it returns a true or false result. Never did this in Intellij but in other IDEs this have already worked. Commented Feb 7 at 21:04
  • I have received the binary data and want to confirm that the file is correct The best way to do that is to take a checksum before and after Commented Feb 7 at 21:12
  • Indeed, if you are sending / receiving files over a network connection, there is a small risk of corruption due to transmission errors that are not detected by layer 1 / 2 / 3 CRCs etcetera. (See examples in en.wikipedia.org/wiki/Data_corruption) Therefore, there is a case for doing checksums and other checks in your application in production ... rather than just when you are debugging the application. Commented Feb 8 at 8:45

1 Answer 1

1

Use the Evaluate Expression feature while debugging, with this code:

org.apache.commons.io.FileUtils.writeByteArrayToFile(
        new File("newfile.jpg"), bytearrayofdata);    
Sign up to request clarification or add additional context in comments.

1 Comment

org.apache.commons.io is a third party library, which is not present in every program. And since Java 8 it is no longer necessary is there is the built-in java.nio.file.Files.write(..) command.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.