munjyong
munjyong

Reputation: 75

Convert ArrayList<String> to byte[]

I want to be able to convert an ArrayList<String> that stores the contents of a file read from a BufferedReader, then convert the contents into a byte[] to allow it to be encrypted using Java's Cipher class.

I have tried using .getBytes() but it's not working since I think I need to convert the ArrayList first, and I'm having trouble on figuring out how to do that.

Code:

// File variable
private static String file;

// From main()
file = args[2];

private static void sendData(SecretKey desedeKey, DataOutputStream dos) throws Exception {
        ArrayList<String> fileString = new ArrayList<String>();
        String line;
        String userFile = file + ".txt";

        BufferedReader in = new BufferedReader(new FileReader(userFile));
        while ((line = in.readLine()) != null) {
            fileString.add(line.getBytes()); //error here
        }

        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, desedeKey);
        byte[] output = cipher.doFinal(fileString.getBytes("UTF-8")); //error here
        dos.writeInt(output.length);
        dos.write(output);
        System.out.println("Encrypted Data: " + Arrays.toString(output));
    }

Many thanks, in advance!

Upvotes: 2

Views: 5630

Answers (5)

Veselin Davidov
Veselin Davidov

Reputation: 7071

Why would you want to read it as string and the convert it to byte array? Since Java 7 you can do:

byte[] input= Files.readAllBytes(new File(userFile.toPath());

then pass that content to the Cipher.

byte[] output = cipher.doFinal(input);

Also you might consider using streams (InputStream and CipherOutputStream) instead of loading the whole file into the memory in case you need handle big files.

Upvotes: 6

Wisthler
Wisthler

Reputation: 587

So, the full ArrayList is in fact a single String?

One straightforward approach would be to merge all the Strings in it into a single one and then call the .getBytes() on it.

Upvotes: 0

Jeroen Steenbeeke
Jeroen Steenbeeke

Reputation: 4013

You could try to take advantage of Java's serialization functionality and use an ObjectOutputStream wrapped around a ByteOutputStream:

try (ByteArrayOutputStream bout = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bout)) {
  out.writeObject(list);
  out.flush();

  byte[] data = bout.toByteArray();
} catch (IOException e) {
  // Do something with the exception
}

The downside to this approach is that the contents of the byte array will be tied to the list implementation's serialized form, so reading it back into a List may give weird results in later Java versions.

Upvotes: -3

user2914191
user2914191

Reputation: 897

Either concatenate strings, or create a StringBuffer.

StringBuffer buffer = new StringBuffer();
String line;
String userFile = file + ".txt";

BufferedReader in = new BufferedReader(new FileReader(userFile));
while ((line = in.readLine()) != null) {
   buffer.append(line); //error here
}

byte[] bytes = buffer.toString().getBytes();

Upvotes: 6

vavasthi
vavasthi

Reputation: 952

Why use ArrayList. Just use a StringBuffer and keep the complete contents of the file into a single string.

Upvotes: 0

Related Questions