最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is it possible to dump a Java byte array to a file from the IntelliJ debugger? - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Feb 7 at 19:22 AnomalyAnomaly 1,0711 gold badge11 silver badges21 bronze badges 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. – Robert 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 – g00se 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. – Stephen C Commented Feb 8 at 8:45
Add a comment  | 

1 Answer 1

Reset to default 0

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

org.apache.commons.io.FileUtils.writeByteArrayToFile(
        new File("newfile.jpg"), bytearrayofdata);    
发布评论

评论列表(0)

  1. 暂无评论