Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Unable to merge two byte arrays

807589Nov 26 2008 — edited Nov 27 2008
Hi there,

I am trying to merge two byte arrays into one byte array
and write into a new word document using OutputStream object.
But in the new word document that is created only the contents
of the first byte array are being shown and not that of the second one.

I have pasted the code below.
Any pointers will be of great help.

Thanks and Regards,
Partha.




package com.cisco.asit.icn.common.servlets;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class TestPOC {

public static void main(String[] args) {
try
{
System.out.println("I am here...");



System.out.println("I am in example 4...");

byte[] byteAr1 = getBytesFromFile(new File("c:\\Testing_POC\\Not_blank.doc"));
System.out.println(byteAr1.toString());
System.out.println();
byte[] byteAr2 = getBytesFromFile(new File("c:\\Testing_POC\\Quest.doc"));

byte[] finalOne = concatenate(byteAr1, byteAr2);

System.out.println("byte array 1..."+ byteAr1.length);
System.out.println("byte array 2..."+ byteAr2.length);
System.out.println("Final byte array ..."+ finalOne.length);

InputStream is = new ByteArrayInputStream(finalOne);

POIFSFileSystem fss = new POIFSFileSystem(is);
OutputStream out1 = new FileOutputStream("c:\\Testing_POC\\Result.doc");
fss.writeFilesystem(out1);

System.out.println("Stored to file system...");

try {
Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL \"" + "c:\\Testing_POC\\Result.doc");
}
catch (IOException e1){
e1.printStackTrace();
}

System.out.println("I am fine till here...");
out1.flush();
out1.close();
}
catch (Throwable t) {
t.printStackTrace();
}
}


byte[] res = new byte[l.length + r.length];
System.arraycopy(l, 0, res, 0, l.length);
System.arraycopy(r, 0, res, l.length ,r.length);

return res;
}


public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);

// Get the size of the file
long length = file.length();

// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.
if (length > Integer.MAX_VALUE) {
// File is too large
}

// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];

// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}

// Close the input stream and return bytes
is.close();
return bytes;
}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 25 2008
Added on Nov 26 2008
6 comments
1,132 views