6 Replies Latest reply on Mar 4, 2004 11:03 PM by 843841

    ByteArrayInputStream splitting

    843841
      This post is a follow up to the following post
      http://forum.java.sun.com/thread.jsp?forum=33&thread=498009&tstart=45&trange=15

      I would like to find a way to split the input stream into 600k pieces and insert each piece int to the db..

      TIA
        • 1. Re: ByteArrayInputStream splitting
          843841
          Use the read(byte[] b, int off, int len) method of ByteArrayInputStream. You'll have to loop until all of the bytes have been stored and while looping maintain your current position in the buffer.




          • 2. Re: ByteArrayInputStream splitting
            843841
            Thats the problem.. I'm very new at this and every which way i tried it just craps out on me.. I know i'm doing something wrong in the loop.. I'm thinking i'm not maintaining the current position right.
            • 3. Re: ByteArrayInputStream splitting
              843841
              Post your code and the error message.
              • 4. Re: ByteArrayInputStream splitting
                843841
                Here's the code although the more i read through the specs and the more i look at the code i realize that i'm waayyyyy off base....

                ByteArrayInputStream bais =
                          new ByteArrayInputStream(baos.toByteArray());
                               
                               int releasedID= 200;
                               int sofar = 0;
                               int sz = 0;
                               int length = 20 * 1024;
                               
                               byte[] data = new byte[(int)length];
                               
                               System.out.println ("file size= " + size);
                               System.out.println ("length = " + length);               
                               
                               while (sofar <= (int)size) {
                          sofar = bais.read(data, sofar, (length + 1));
                               sofar += length;
                               sz = (int)size - sofar;
                               
                               System.out.println ("bytes available: " + sz);
                               
                               //baisOffset = sofar + 1;
                               
                               System.out.println ("bytes copied: " + sofar);
                               
                               
                               String sql =
                          "INSERT INTO attachment (user_ID, name, data, size, type, modified_by, released_by) " +
                          "VALUES (?, ?, ?, ?, ?, ?, ?)";
                               
                                    PreparedStatement ps = con.prepareStatement(sql);
                                    ps.setString(1, userID);
                                    ps.setString(2, filename);
                                    ps.setBytes(3, data);
                                    ps.setLong(4, size);
                                    ps.setString(5, type);
                                    ps.setString(6, modifiedID);
                                    ps.setInt(7, releasedID);
                                    ps.executeUpdate();
                                    ps.close();
                                    con.commit();
                               }
                • 5. Re: ByteArrayInputStream splitting
                  843841
                  btw i get an
                  java.lang.IndexOutOfBoundsException
                  at java.io.ByteArrayInputStream.read(ByteArrayInputStream.java:159)

                  error
                  • 6. Re: ByteArrayInputStream splitting
                    843841
                    I think I led you a little wrong but here is a sample that will read a file into a byte[] buffer and then split the buffer:
                     import java.io.*;
                    
                     public class r {
                    
                     public static void main(String[] args) {
                      try {
                         FileInputStream in = new FileInputStream("PrefsTest.java");
                         BufferedInputStream bis = new BufferedInputStream(in);
                         ByteArrayOutputStream baos = new ByteArrayOutputStream ();
                    
                         byte[] buf = new byte[100];
                         int bytesread = bis.read(buf);
                         
                         while (bytesread != -1) {
                              baos.write(buf, 0, bytesread);
                              bytesread = bis.read(buf);
                         }
                         baos.flush();
                         
                    
                         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
                         
                         int releasedID= 200;
                         int sofar = 0;
                         int sz = 0;
                         //int length = 20 * 1024;
                    
                         int size = (baos.toByteArray()).length;
                         System.out.println ("file size= " + size);
                         int linesRead = 0;
                         byte[] data = new byte[20];
                    
                         while (linesRead > -1) { 
                    
                             linesRead = bais.read(data, 0, 20);
                             System.out.println(linesRead); 
                             if (linesRead > -1) {
                                   sofar += linesRead;
                                   sz = size - sofar;
                                   System.out.println ("bytes available: " + sz);
                                   System.out.println ("bytes copied: " + sofar);
                                  //String sql = 
                                   //"INSERT INTO attachment (user_ID, name, data, size, type, modified_by, released_by) " +
                                   //"VALUES (?, ?, ?, ?, ?, ?, ?)";
                    
                                 //PreparedStatement ps = con.prepareStatement(sql);
                                   //ps.setString(1, userID);
                                   //ps.setString(2, filename);
                                  //ps.setBytes(3, data);
                                   //ps.setLong(4, size);
                                   //ps.setString(5, type);
                                  //ps.setString(6, modifiedID);
                                  //ps.setInt(7, releasedID);
                                  //ps.executeUpdate();
                                  //ps.close();
                                 //con.commit();
                              }
                          } 
                      } catch(Exception e) { e.printStackTrace(); }
                    }//End main
                    
                    } //End class