How  to append/merger 2 byte[] ?

is there a better (faster) way to append 2 byte[] ? (other then using System.arrayCopy and loop)
for example
String s = "just a text";
File f = new File("c:a.exe");
BufferedInputStream bis = new BufferedInputStream(new new FileInputStream(f));
byte[] b2 = new byte[f.length()];
bis.read(b);
byte[] b1 = new byte[s.getBytes().length()];
b1 = s.getBytes();How to append/merger b1 and b2 (using better/faster way)?

actually i want upload a file via socket, but before it is sent, i want add an headerCode to the bytearray
this is snip of my code
public boolean runUpload(String  filetoUpload , JProgressBar progressBar){
          try {
            int MAX_UPLOAD_BYTE = 1024*10;
            File f = new File(filetoUpload);
            SocketInfo socketinfo  = new SocketInfo();
            Socket socket = new Socket(socketinfo.Host, socketinfo.HostPort);
            //Socket socket = new Socket(InetAddress.getLocalHost(), 8080);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
            FileInputStream fs = new FileInputStream(f);
            BufferedInputStream bis = new BufferedInputStream(fs);
            long size = f.length() ;
            long uploaded = 0;
            int progres = 0;
            String ss = "headerCode";
            byte[] buffer = new byte[MAX_UPLOAD_BYTE-ss.getBytes().length];
            byte[] trans_buffer;
            int len = 0;
                while ((len = bis.read(buffer)) != -1) {
                    trans_buffer = new byte[MAX_UPLOAD_BYTE];
                    System.arraycopy(ss.getBytes(), 0, trans_buffer, 0, ss.getBytes().length);
                    System.arraycopy(buffer, 0, trans_buffer, ss.getBytes().length, len);
                    //transferLen = len + ss.getBytes().length;
                    out.write(trans_buffer, 0, len+ss.getBytes().length);
                    //out.write(trans_buffer, 0, MAX_UPLOAD_BYTE);
                    out.flush();
                    uploaded +=len;
                    progres = (int)Math.ceil(((double)uploaded/(double)size) * 100);
                    System.out.println("#" + (len+ss.getBytes().length));
                    progressBar.setValue(progres);
                    trans_buffer = null;
                System.out.println("lennnn" + len);   
                bis.close();
                out.close();
                socket.close();
            return true;
        } catch (IOException ex) {
            Logger.getLogger(UploadFiles.class.getName()).log(Level.SEVERE, null, ex);
            return false;
    }

Similar Messages

  • How do I merge 2 pdf files into one file?

    How do I merge 2 pdf files into one file?

    I use PrimoPDF (free download) to create pdf files. To merge two docs/pdfs/etc., print the first to a new pdf using Primo. Close it, then print the second and save it to the same new pdf. Primo gives you the option to 'append' or 'overwrite' the original file. By using 'append', you are just adding it to the back of the existing file. You can do this repeatedly to resort or combine existing pages into a new pdf.
    Also, I'm using Adobe Reader X.

  • I have just used migration assistant to import photos (using an ethernet cable) from my PC. It has worked but they have gone into a new user on my MacPro. How can I merge them into the one user account.

    I have just used migration assistant to import photos (using an ethernet cable) from my PC. It has worked but they have gone into a new user on my MacPro. How can I merge them into the one user account.

    How many pictures (or how many bytes) are we talking?  Now that the pictures are imported one way to cross users is to use a removable drive (although large volumes of files preclude this).
    The other is to login as the user where the pictures are, then copy them into the public drop box of the user where you want the picture.
    The arrow is showing (in ths example) copy files from the Pictures folder for my user to the guest user accounts public dropbox.  When you do this you may have to authenticate and you won't be able to see the results of the copy until you log in as the other user.
    All users are in the folder "/Users/"

  • How to Append two  word documents into single  using   java

    How to Append two word documents into single using java
    we tried this but it's not append the one word document to other
    source code:public class AppendTwoWordFiles {
         public static void main(String []arg)throws IOException
              FileInputStream fi=null;
              FileOutputStream fo=null;
              try {
                   System.out.println("Enter the source file name u want to append");
                   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                   File f1=new File(br.readLine().toString());
                   System.out.println("Enter the Destination file name ");
                   File f2=new File(br.readLine().toString());
                   fi = new FileInputStream(f1);
                   fo = new FileOutputStream(f2,true);
                   byte b[]=new byte[2];
                   while((fi.read(b))!=-1);
              fo.write(b);
    System.out.println("Successfully append the file");
              } catch (FileNotFoundException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
              } catch (IOException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
              finally{
              fi.close();
              fo.close();
    plz reply me quickly ,,,what can i follow

    Use this code ..
    and give the path of the both file like this.....
    source file ---- C:/workspace/Practice/src/com/moksha/ws/test/practice.text
    destination file ---- C:/workspace/City/src/com/moksha/ws/test/practice1.text
    import java.io.*;
    public class AppendTwoWordFiles {
         public static void main(String[] arg) throws IOException {
              FileInputStream fi = null;
              FileOutputStream fo = null;
              try {
                   System.out.println("Enter the source file name u want to append");
                   BufferedReader br = new BufferedReader(new InputStreamReader(
                             System.in));
                   File f1 = new File(br.readLine().toString());
                   System.out.println("Enter the Destination file name ");
                   File f2 = new File(br.readLine().toString());
                   fi = new FileInputStream(f1);
                   fo = new FileOutputStream(f2, true);
                   byte b[] = new byte[2];
                   int len = 0;
                   while ((len = fi.read(b)) > 0) {
                        fo.write(b, 0, len);
                   System.out.println("Successfully append the file");
              } catch (FileNotFoundException e) {
                   e.printStackTrace();
              } catch (IOException e) {
                   e.printStackTrace();
              } finally {
                   fi.close();
                   fo.close();
    }

  • How do I alter the bytes of a Class file to add calls to the methods?

    If i had the bytes of a class file, and I wanted to alter the bytes that constitute each method for the class so that it included a call to the security manager, how would i do it?
    1. How would I know which bytes were the opening of a method?
    2. how would I know what the name of the method is?
    3. How would I create bytes for something like:
       SecurityManager sm = System.getSecurityManager().checkPermission(thismeth, subject);
    4. I assume that if by some miracle I can do the above, then all I have to do is call defineClass(...) in ClassLoader and send it the new bytes, right?
    Thanks to all!

    OK, if it will help anyone get me the answers here, I found a class on the internet that can read a class file and tell you where in the bytes a method occurs and what its name is, and how long it is. What I need now is how to convert a call into the correct manner of bytes.
    For example, so I could add the bytes that would do:
       System.out.println("Added!");
    The class that reads a class file:
    /* Inspector.java by Mark D. LaDue */
    /* June 24, 1997 */
    /* Copyright (c) 1997 Mark D. LaDue
       You may study, use, modify, and distribute this example for any purpose.
       This example is provided WITHOUT WARRANTY either expressed or implied.  */
    /* This Java application analyzes the entries in the constant pool and locates
       the code arrays in a Java class file. Each entry in the constant pool
       yields the following information:
       Index     Tag     Reference(s)/Value(s)
       where "Index" is its position within the class file's constant pool,
       "Tag" is the official tag number for that type of entry, and
       "Reference(s)/Value(s)" contains the constant pool information
       according to the entry's type.  (See Lindholm and Yellin's "The Java
       Virtual Machine Specification" for details.)  For each code array in
       the class file, its starting byte, its total length, and the name of
       the method in which it occurs are given.  Combining this information
       with the information yielded by the humble "javap" utility gives one
       sufficient information to hack the code arrays in Java class files. */
    import java.io.*;
    class Inspector {
        public static void main(String[] argv) {
            int fpointer = 8; // Where are we in the class file?
            int cp_entries = 1; // How big is the constant pool?
            int Code_entry = 1; // Where is the entry that denotes "Code"?
            int num_interfaces = 0; // How many interfaces does it use?
            int num_fields = 0; // How many fields are there?
            int num_f_attributes = 0; // How many attributes does a field have?
            int num_methods = 0; // How many methods are there?
            int num_m_attributes = 0; // How many attributes does a method have?
            int[] tags; // Tags for the constant pool entries
            int[] read_ints1; // References for some constant pool entries
            int[] read_ints2; // References for some constant pool entries
            long[] read_longs; // Values for some constant pool entries
            float[] read_floats; // Values for some constant pool entries
            double[] read_doubles; // Values for some constant pool entries
            StringBuffer[] read_strings; // Strings in some constant pool entries
            int[] method_index;
            long[] code_start;
            long[] code_length;
    // How on earth do I use this thing?
            if (argv.length != 1) {
                System.out.println("Try \"java Inspector class_file.class\"");
                System.exit(1);
    // Start by opening the file for reading
            try {
                RandomAccessFile victim = new RandomAccessFile(argv[0], "r");
    // Skip the magic number and versions and start looking at the class file
                victim.seek(fpointer);
    // Determine how many entries there are in the constant pool
                cp_entries = victim.readUnsignedShort();
                fpointer += 2;
    // Set up the arrays of useful information about the constant pool entries
                tags = new int[cp_entries];
                read_ints1 = new int[cp_entries];
                read_ints2 = new int[cp_entries];
                read_longs = new long[cp_entries];
                read_floats = new float[cp_entries];
                read_doubles = new double[cp_entries];
                read_strings = new StringBuffer[cp_entries];
    //Initialize these arrays
                for (int cnt = 0; cnt < cp_entries; cnt++) {
                    tags[cnt] = -1;
                    read_ints1[cnt] = -1;
                    read_ints2[cnt] = -1;
                    read_longs[cnt] = -1;
                    read_floats[cnt] = -1;
                    read_doubles[cnt] = -1;
                    read_strings[cnt] = new StringBuffer();
    // Look at each entry in the constant pool and save the information in it
                for (int i = 1; i < cp_entries; i++) {
                    tags[i] = victim.readUnsignedByte();
                    fpointer++;
                    int skipper = 0;
                    int start = 0;
                    int test_int = 0;
                    switch (tags) {
    case 3: read_ints1[i] = victim.readInt();
    fpointer += 4;
    break;
    case 4: read_floats[i] = victim.readFloat();
    fpointer += 4;
    break;
    case 5: read_longs[i] = victim.readLong();
    fpointer += 8;
    i++;
    break;
    case 6: read_doubles[i] = victim.readDouble();
    fpointer += 8;
    i++;
    break;
    case 7:
    case 8: read_ints1[i] = victim.readUnsignedShort();
    fpointer += 2;
    break;
    case 9:
    case 10:
    case 11:
    case 12: read_ints1[i] = victim.readUnsignedShort();
    fpointer += 2;
    victim.seek(fpointer);
    read_ints2[i] = victim.readUnsignedShort();
    fpointer += 2;
    break;
    // This is the critical case - determine an entry in the constant pool where
    // the string "Code" is found so we can later identify the code attributes
    // for the class's methods
    case 1: skipper = victim.readUnsignedShort();
    start = fpointer;
    fpointer += 2;
    victim.seek(fpointer);
    for (int cnt = 0; cnt < skipper; cnt++) {
    int next = victim.readUnsignedByte();
    switch (next) {
    case 9: read_strings[i].append("\\" + "t");
    break;
    case 10: read_strings[i].append("\\" + "n");
    break;
    case 11: read_strings[i].append("\\" + "v");
    break;
    case 13: read_strings[i].append("\\" + "r");
    break;
    default: read_strings[i].append((char)next);
    break;
    victim.seek(++fpointer);
    victim.seek(start);
    if (skipper == 4) {
    fpointer = start + 2;
    victim.seek(fpointer);
    test_int = victim.readInt();
    if (test_int == 1131373669) {Code_entry = i;}
    fpointer = fpointer + skipper;
    else {fpointer = start + skipper + 2;}
    break;
    victim.seek(fpointer);
    // Skip ahead and see how many interfaces the class implements
    fpointer += 6;
    victim.seek(fpointer);
    num_interfaces = victim.readUnsignedShort();
    // Bypass the interface information
    fpointer = fpointer + 2*(num_interfaces) + 2;
    victim.seek(fpointer);
    // Determine the number of fields
    num_fields = victim.readUnsignedShort();
    // Bypass the field information
    fpointer += 2;
    victim.seek(fpointer);
    for (int j=0; j<num_fields; j++) {
    fpointer += 6;
    victim.seek(fpointer);
    num_f_attributes = victim.readUnsignedShort();
    fpointer = fpointer + 8*(num_f_attributes) + 2;
    victim.seek(fpointer);
    // Determine the number of methods
    num_methods = victim.readUnsignedShort();
    fpointer += 2;
    // Set up the arrays of information about the class's methods
    method_index = new int[num_methods];
    code_start = new long[num_methods];
    code_length = new long[num_methods];
    //Initialize these arrays
    for (int cnt = 0; cnt < num_methods; cnt++) {
    method_index[cnt] = -1;
    code_start[cnt] = -1;
    code_length[cnt] = -1;
    // For each method determine the index of its name and locate its code array
    for (int k=0; k<num_methods; k++) {
    fpointer += 2;
    victim.seek(fpointer);
    method_index[k] = victim.readUnsignedShort();
    fpointer += 4;
    victim.seek(fpointer);
    // Determine the number of attributes for the method
    num_m_attributes = victim.readUnsignedShort();
    fpointer += 2;
    // Test each attribute to see if it's code
    for (int m=0; m<num_m_attributes; m++) {
    int Code_test = victim.readUnsignedShort();
    fpointer += 2;
    // If it is, record the location and length of the code array
    if (Code_test == Code_entry){
    int att_length = victim.readInt();
    int next_method = fpointer + att_length + 4;
    fpointer += 8;
    victim.seek(fpointer);
    code_length[k] = victim.readInt();
    code_start[k] = fpointer + 5;
    fpointer = next_method;
    victim.seek(fpointer);
    // Otherwise just skip it and go on to the next method
    else {
    fpointer = fpointer + victim.readInt() + 4;
    victim.seek(fpointer);
    // Print the information about the Constant Pool
    System.out.println("There are " + (cp_entries - 1) + " + 1 entries in the Constant Pool:\n");
    System.out.println("Index\t" + "Tag\t" + "Reference(s)/Value(s)\t");
    System.out.println("-----\t" + "---\t" + "---------------------\t");
    for (int i = 0; i < cp_entries; i++) {
    switch (tags[i]) {
    case 1: System.out.println(i + "\t" + tags[i] + "\t" + read_strings[i].toString());
    break;
    case 3: System.out.println(i + "\t" + tags[i] + "\t" + read_ints1[i]);
    break;
    case 4: System.out.println(i + "\t" + tags[i] + "\t" + read_floats[i]);
    break;
    case 5: System.out.println(i + "\t" + tags[i] + "\t" + read_longs[i]);
    break;
    case 6: System.out.println(i + "\t" + tags[i] + "\t" + read_doubles[i]);
    break;
    case 7:
    case 8: System.out.println(i + "\t" + tags[i] + "\t" + read_ints1[i]);
    break;
    case 9:
    case 10:
    case 11:
    case 12: System.out.println(i + "\t" + tags[i] + "\t" + read_ints1[i] + " " + read_ints2[i]);
    break;
    System.out.println();
    // Print the information about the methods
    System.out.println("There are " + num_methods + " methods:\n");
    for (int j = 0; j < num_methods; j++) {
    System.out.println("Code array in method " + read_strings[method_index[j]].toString() + " of length " + code_length[j] + " starting at byte " + code_start[j] + ".");
    System.out.println();
    // All the changes are made, so close the file and move along
    victim.close();
    } catch (IOException ioe) {}

  • How to append to zip files

    I would like to know how to append to a zip file, I know how to write a zip file and I can put as many entries in the first time but if I run my program again and try to add a file to the same zip file it erases what was already in it. I use ZipOutputStream(new FileOutputStream), I have tried the FileOutputStream append but it does not seem to work for zip files, I have tried RandomAccessFile on my zip file but that just seems to add the bytes to the file that already in the zip file, and makes the file unopenable, corrupt. If you can help, please tell me how.

    You actually have to open the previous zipped file, read it's entries and their data and then rewrite a new zip file with these entries and their data. You can then delete your first file and rename the new one so the effect is that you have "appended" zip files to the existing archive.
    Here is an example which I pieced together from a larger bit of code that I have. This would need to be in a try/catch but this should give you a start.
    /** This method adds a new file to an existing zip file. It * includes all the zipped entries previously written.
    * @param file The old zip archive
    * @param filename The name of the file to be Added
    * @param data The data to go into the zipped file
    * NOTE: This method will write a new file and data to an archive, to
    * write an existing file, we must first read the data frm the file,
    * then you could call this method.
    public void addToArchive(File file, String filename, String data)
         ZipOutputStream zipOutput = null;
    ZipFile zipFile = null;
    Enumeration zippedFiles = null;
    ZipEntry currEntry = null;
    ZipEntry entry = null;
    zipFile = new ZipFile( file.getAbsolutePath() );
         //get an enumeration of all existing entries
    zippedFiles = zipFile.entries();
         //create your output zip file
    zipOutput = new ZipOutputStream( new FileOutputStream ( new File( "NEW" + file.getAbsolutePath() ) ) );
    //Get all the data out of the previously zipped files and write it to a new ZipEntry to go into a new file archive
    while (zippedFiles.hasMoreElements())
    //Retrieve entry of existing files
    currEntry = (ZipEntry)zippedFiles.nextElement();
    //Read data from existing file
    BufferedReader reader = new BufferedReader( new InputStreamReader( zipFile.getInputStream( currEntry ) ) );
    String currentLine = null;
    StringBuffer buffer = new StringBuffer();
    while( (currentLine = reader.readLine() ) != null )
    buffer.append( currentLine);
    //Commit the data
    zipOutput.putNextEntry(new ZipEntry(currEntry.getName()) ) ;
    zipOutput.write (buffer.toString().getBytes() );
    zipOutput.flush();
    zipOutput.closeEntry();
    //Close the old zip file
    zipFile.close();
    //Write the 'new' file to the archive, commit, and close
    entry = new ZipEntry( newFileEntryName );
    zipOutput.putNextEntry( entry );
    zipOutput.write( data.getBytes() );
    zipOutput.flush();
    zipOutput.closeEntry();
    zipOutput.close();
         //delete the old file and rename the new one
    File toBeDeleted = new File ( file.getAbsolutePath() );
    toBeDeleted.delete();
         File toBeRenamed = new File ( "NEW" + file.getAbsolutePath() );
    toBeRenamed.rename( file );
    Like I said I haven't tested this code as it is here but hopefully it can help. Good luck.

  • I brought my itunes library onto a mac from a windows machine and now i have two separate libraries.  How can I merge them?

    I moved to Mac from a Windows machine where I had downloaded all of my CDs into iTunes.  When I got my Mac it only put my purchased items into my library.  So I merged my windows library and it created a whole new user account with that on it.  I now have two libraries and can only sync music with one account or I lose all of my imported CDs.  Help!  How do I merge them. 

    The easiest way is to get both computers on your home network and copy the files over the network. It's easier than you think
    Check this earlier thread:
    [How do I move my itunes library to new computer|http://discussions.apple.com/thread.jspa?messageID=7937172&#7937172]
    That thread mentions Target Disk Mode, which won't help moving from a PC to a Mac. But you can use file sharing to copy the files.
    As the articles all say: don't forget to de-authorize your PC before you get rid of it.

  • I have two apple Id accounts how do I merge them into one account

    I have two apple Id accounts how do I merge them into one account

    you can't merge Apple ID's
    From the FAQ: http://support.apple.com/kb/HT5622
    I have multiple Apple IDs. Is there a way for me to merge them into a single Apple ID?
    Apple IDs cannot be merged. You should use your preferred Apple ID from now on, but you can still access your purchased items such as music, movies, or software using your other Apple IDs.

  • I have 3 itunes accounts.  How can I merge them into one?

    I have 3 itunes accounts.  How can I merge them into one?

    Sorry, but the only answer is that you can't.

  • How do I merge my Itunes account with my Icloud account

    Hi Readers!
    I bought a Macbook Pro Retina today but I am running into a registration issue with Itunes and Icloud.
    When I try to register the Macbook I have to put in my Itunes account (name: mflentge), because of the use of Icloud I have to put in an e-mailadress to register Icloud to my Itunes account.
    Unfortunately my main e-mailadress already has an Icloud account attached to it, so I'm not able to register this e-mailadress.
    I would very much like to use I-cloud and Itunes from the same Itunes account (mflentge), and it being connected to my main e-mailadress.
    My question:
    How can I merge my Itunes and Icloud account?
    There are no important purchases or data on either of the accounts, so I don't mind deleting one if that means I can use my Apple ID + my main e-mailadress as one account.
    Thank you for your help! It is much appreciated.
    Best Regards,
    Max

    Thanks for your reply Winston Churchill,
    My main problem with dropping my Icloud account is that my Icloud account is registered under my main e-mail adres.
    Can I use my current ICloud account to be my Itunes account aswell?
    Also i've been thinking to switch to the @me.com e-mailadress, are there any known limitations on the use of this e-mail (I have a Android Phone for instance, will I be able to set up an Apple e-mail account on it?).
    Best regards,
    Max

  • HT204053 I setup an apple id using my gmail email address from my iphone and then I decided to change my email to icloud.  So I setup an icloud email account. Now I want to change my apple id to my icloud email account, but it wont let me.  How can I merg

    I just got an iphone recently and I started my apple id using my gmail email address. I decided to change email from gmail to icloud so I setup an icloud email account and now I have two apple id accounts.  All my primary information is in the gmail apple id account and i just want to merge the two together.  I've tried changing the apple id on the main account to the icloud email, but it says it already exists.  How can I merge these two accounts without deleting one or the other?
    Thanks
    pallhues

    You can't merge iCloud accounts.
    You could move all your calendars contacts etc over from the account you don't want to the one you do, it's not simple but about all I can suggest.

  • How do I merge one user ID with another on my mac

    I set up my Mac prior to migrating from my PC and this created one log-in for me. When I migrated info from my PC, another log in was created. How do I merge the two, so all information is in one place? Thank you!

    Open the Users folder on the internal drive and drag one account's home folder to the other's desktop.
    (92088)

  • TS3988 how do i merge two icloud accounts - on on my computer and ipad, a different icloud id on my iphone?

    How do I merge two iCloud accounts into one?  One account is on my mac and ipad, the other on my iphone.

    Welcome to the Apple community.
    You cannot merge accounts, you will need to choose one and use it.

  • How do I merge my itunes folders from two user accounts on one computer?

    I've made purchases from audible.com and on itunes from two different user accounts on my home computer.  When I try to sync my itouch with my library I am missing the purchases I made from the other user account.  How can I merge these together so i can have everything at once that I have purchased?

    Copy or move the song files from one account to a shared location and then drag them into the open iTunes window when logged into the other account.
    (60844)

  • How do you merge multiple songs into one album?

    When I added a downloaded album to iTunes, it created one album for each track. How do I merge all of these albums/tracks into one album?

    Some albums will fracture into a million pieces if you have different artist names. How the columns are sorted can effect this.
    If it is a multiple artists on one album select 'Compilation' in the options section for all of the songs on the album. 
    NOTE: You need to have multiple items selected to set this flag!

Maybe you are looking for

  • Need comments/opinion

    Need some comments/opinion regarding essbase implementation:1. our prospect client wants to analyze/report their financial statement using their G/L system (they use Platinum software). And they want it in such detail level as their g/l account / cha

  • Excise in GR

    Hi Friends,                         I have created gr,and captured and posted the excise values.now my query is 1. While desplaying the materal document, excise details, i am not getting. 2. for sec & hsec cess, the values are not flowing to part2 ce

  • ADS: com.adobe.ProcessingException: PDF is not interactive

    Hi All, I have a ECC 6.0 ABAP SP12Stack installation and a seperate NW04s SP13 Portal installation. I have done all the configuration steps of ADS integration on those 2 systems. I developed a WebDynpro application and used the interactive form objec

  • Fp11 run video rendering much faster even without stageVideo

    hi adobe, first..thank you very much that you have heard our dreams about GPU optimizing. I cant remember how many request, forms and feature requests we at onlinelib has fill out to get this feature. anyway, the first demo works pretty amazing. i al

  • Activated iTunes Match, songs on iPod that were not in computer library deleted

    A few days ago I activated iTunes match on my iPod touch, iPhone 4s, iPad and my computer. I wasn't exactly sure of the full potential of iTunes match and was hoping for an easy way to get all of my music onto my new iPad. I have synced music to my i