AKIWEB
AKIWEB

Reputation: 19622

How to return the contents of jar files as an array of bytes?

I am trying to get the contents of the file as an array of bytes.

public static void main(String[] args) {

    final IServiceClient client = StorageConsumerProvider.getStorageServiceClient("dev");

    String name = new File("C:\\Storage\\Model-1.0.0.jar").getName();

    StorageObjectIdentifier objIdentifier = new StorageObjectIdentifier("Model", "1.0.0", name);

// I need to pass the bytes here
    client.createObject(objIdentifier, name.getBytes());

}

Interface is like this-

public void createObject(StorageObjectIdentifier objIdentifier, byte[] obj)

createObject method accepts two parameters, one of them is - the contents of the file as an array of bytes

I am not sure how should I pass that as an array of bytes? Can anybody help me with this? The file is a jar file in my case.

Upvotes: 1

Views: 2572

Answers (4)

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51721

You can use BufferedInputStream to load a file as a byte stream with buffering.

File iFile = new File("C:\\Storage\\Model-1.0.0.jar");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(iFile));

int read = 0;
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[1024]; // buffer size
while ((read = bis.read(buffer)) != -1) {
    os.write(buffer, 0, read);
}
bis.close();

StorageObjectIdentifier objIdentifier =
                        new StorageObjectIdentifier("Model", "1.0.0", iFile.getName());
client.createObject(objIdentifier, os.toByteArray());

Or, using FileUtils from Apache Commons IO

client.createObject(objIdentifier, FileUtils.readFileToByteArray(iFile));

Upvotes: 1

user2679791
user2679791

Reputation: 49

You are passing a byte array containing the name of the file to your createObject method. To pass a byte array containing the contents of the file first you have to read the file into a byte array.

Use FileReader or FileInputStream, create an instance passing either the name of file or the File object, then use one of the read() methods to read bytes into an array.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1502406

Well currently you're just passing a byte array representation of the name of the file. You need to open the file and read it. You can do that with FileInputStream etc, but if you're happy to use Guava, that makes it easy with Files.toByteArray():

File file = new File("C:\\Storage\\Model-1.0.0.jar");
byte[] data = Files.toByteArray(file);
StorageObjectIdentifier objIdentifier =
    new StorageObjectIdentifier("Model", "1.0.0", name);

client.createObject(objIdentifier, data);

Upvotes: 1

Luca Basso Ricci
Luca Basso Ricci

Reputation: 18413

You have to load all file content in bytearray manually using a function like this:

public final static byte[] load(FileInputStream fin) throws Exception
{
      ByteArrayOutputStream baos = new ByteArrayOutputStream();

      int readCnt = fin.read(readBuf);
      while (0 < readCnt) {
        baos.write(readBuf, 0, readCnt);
        readCnt = fin.read(readBuf);
      }

      fin.close();

      return bout.toByteArray();
  }

but works if file is small else, for large files, you will run to NPE.
Better choice is to change your interface passing InputStream instead of byte[] and let intf implementor decide how to operate.

Upvotes: 1

Related Questions