What type is the best to manage binary data in Java?

后端 未结 5 1466
时光说笑
时光说笑 2021-02-13 03:30

I\'m getting some documents from the web and many of them are binary files (executables, PDF, etc.). In Java, what is the correct type to hold the binary data until save it in d

相关标签:
5条回答
  • 2021-02-13 04:12

    For small amount of data use a byte[] but for binary files (to be stored in database BLOBs) you should use a temporary file as InputStream. JavaEE also does this for uploaded files. It is not good for the server performance to waste memory for byte[]. Imagine a webapp delivering ten PDF file each about 200MB. The server will need more than 2GB of RAM just for the webapp.

    Also using an InputStream allows JDBC to stream the data to the database (for most JDBC drivers, not for MySql, which will hold the data in memory two more times for client and server).

    You may have a look on Apache Commons FileUpload and setBlob() of PreparedStratement.

    0 讨论(0)
  • 2021-02-13 04:13

    Don't save files directly to a database. Save them to a filesystem instead and save the path to the file in the database. That's what file systems are for. You will get performance problems if you stuff binary files into your database.

    0 讨论(0)
  • 2021-02-13 04:27

    Use a byte[] or a ByteBuffer if you need simplified byte-level manipulation.

    Strictly speaking you could use a string but there are many pitfalls related to character encoding, so you shouldn't use strings without a really good reason to do so.

    0 讨论(0)
  • 2021-02-13 04:29

    Use a byte array (byte[]) or InputStream (e.g. ByteArrayInputStream). Java Strings are not a good container for generic binary data.

    The Apache library commons-io has some nice utility classes for dealing with bytes and streams.

    e.g. IOUtils.toByteArray(InputStream)


    ByteBuffer was introduced as part of Java NIO, available in Java 4 (1.4) and later. In specialized scenarios, it can have performance benefits over using a byte[]. It also has some helpful convenience methods. I still usually use byte[], though, since it is more widely known, more common in APIs, and almost always performs well enough.

    0 讨论(0)
  • 2021-02-13 04:33

    An array of bytes

    byte[]
    

    Which is why you see it being used in FileInputStream

    0 讨论(0)
提交回复
热议问题