Oracle BLOB Writes the Bytes Twice to Output Stream

Hi,
I have a very strange problem when working with oracle.sql.BLOB; I cannot figure out what it's causing my BLOB stream output to double the amount of data inserted into the Oracle database. I have a table that contains two BLOB objects(image files) and the goal is to insert two images into each row by BLOB stream.
For example, if the image_bin size is 800k and image_thumbnail size is 100k, this code actually writes 1600k (double) and 200k (double) the amount of bytes to each BLOB column, respectively. The print method in insertBlob() indicates a correct number of bytes being written to the output stream (800k and 100k).
I know for the fact the retrieval method (not mentioned here) doesn't duplicate the bytes when it's read because I have written another test program that does not utilize oracle.sql.BLOB but instead uses PreparedStatement's setBinaryStream(index, InputStream, size_of_file) and it accurately writes the exact image size (no double sizing) to the database -- but not with BLOB. Here's a snippet of my code, note that the actual writing occurs in insertBlob():
private void insertBlob(java.sql.Blob lobImage, String imgName)
throws SQLException, IOException {
File imgFile = null;
FileInputStream imgOnDisk = null;
OutputStream imgToDB = null;
int bufferSize = 0;
oracle.sql.BLOB blobImage = (oracle.sql.BLOB) lobImage;
try {
int bytesRead = 0;
long bytesWritten = 0L;
byte[] byteBuffer = null;
bufferSize = blobImage.getBufferSize();
byteBuffer = new byte[bufferSize];
imgFile = new File(imgName);
// Stream to read the file from the local disk
imgOnDisk = new FileInputStream(imgFile);
// Stream to write to the Oracle database
imgToDB = blobImage.setBinaryStream(imgFile.length());
// Read from the disk file and write to the database
while ((bytesRead = imgOnDisk.read(byteBuffer)) != -1 ) {
imgToDB.write(byteBuffer, 0, bytesRead);
bytesWritten += bytesRead;
} // end of while
System.out.print("Done. " + bytesWritten + "-bytes inserted, buffer size: " +
bufferSize + "-bytes, chunk size: " +
blobImage.getChunkSize() + ".\n");
} catch (SQLException sqlEx) {
System.out.println("SQLException caught: JDBCOracleLOBBinaryStream.processBlob()");
connRollback();
throw sqlEx;
} catch (IOException ioe) {
System.out.println("IOException caught: JDBCOracleLOBBinaryStream.processBlob()");
throw ioe;
} finally {
try {
if (imgOnDisk != null ) {
imgOnDisk.close();
if (imgToDB != null ) {
imgToDB.close();
} catch (IOException ioeClosing) {
System.out.println("IOException caught: JDBCOracleLOBBinaryStream.processBlob() " +
"on closing stream.");
ioeClosing.printStackTrace();
} // end of finally
public void insertImageIntoOracleDB() throws SQLException, IOException {
PreparedStatement pstmt = null;
Statement stmt = null;
ResultSet rset = null;
try {
this.getConnection(_driver, host, port, database, user, _pass);
pstmt = conn.prepareStatement("INSERT INTO " +
" gallery_v (picture_id, picture_title, image_bin, image_thumbnail) " +
" VALUES (?, ?, EMPTY_BLOB(), EMPTY_BLOB())");
pstmt.setInt(1, picID);
pstmt.setString(2, picTitle);
pstmt.executeUpdate();
stmt = conn.createStatement();
rset = stmt.executeQuery("SELECT image_bin, image_thumbnail FROM gallery_v " +
" WHERE picture_id = " + picID + " FOR UPDATE");
int rsetCount = 0;
oracle.sql.BLOB imgBlob = null;
oracle.sql.BLOB imgThumbBlob = null;
while (rset.next()) {
imgBlob = ((OracleResultSet) rset).getBLOB("image_bin");
System.out.print("Inserting " + img + "... ");
insertBlob(imgBlob, img);
imgThumbBlob = ((OracleResultSet) rset).getBLOB("image_thumbnail");
System.out.print("Inserting " + imgThumb + "... ");
insertBlob(imgThumbBlob, imgThumb);
rsetCount++;
System.out.println("\nNumber of rows updated: " + rsetCount);
conn.commit();
} catch (SQLException sqlEx) {
System.out.println("SQLException caught: JDBCOracleLOBBinaryStream.insertImageIntoOracleDB()");
connRollback();
throw sqlEx;
} catch (IOException ioe) {
throw ioe;
} finally {
try {
if (rset != null) {
rset.close();
if (pstmt != null) {
pstmt.close();
if (stmt != null) {
stmt.close();
closeConnection();
} catch (SQLException closingSqlEx) {
System.out.println("SQLException caught: JDBCOracleLOBBinaryStream.insertImageIntoOracleDB() " +
"on closing ResultSet or PreparedStatement.");
closingSqlEx.printStackTrace();
} // end of finally
}

Make a lumpy mistake; the new BLOB#setBinaryStream() method takes a position of where the data is read from in the stream given to it. So the following code:
imgToDB = blobImage.setBinaryStream(imgFile.length());
Starts off from the end of the file. Now I don't understand how this position would result in the duplicated amount of bytes read from the binary file (an image here) to the output stream! The correct line should be:
imgToDB = blobImage.setBinaryStream(0L);
ARGH!!! Now everything works as it expected. I gotta read the API's more carefully as I was expecting the same semantic parameter as PreparedStatement#setBinaryStream() which takes the length of the stream as one of its parameters.

Similar Messages

  • How to  write the bytes out

    I have the working code to write the byte[] to the OutputStream, and it works
           ServletOutputStream out = null;
           BufferedInputStream in = null;
            try {
                byte[] bytes = new byte[1024];
                raf = new RandomAccessFile(restoreZipFile, "r");
                raf.seek(10);
                raf.readFully(bytes);
                out = resp.getOutputStream();
                 in = new BufferedInputStream(new ByteArrayInputStream(bytes));
                 int data;
                 while ((data = in.read()) != -1) {
                     out.write(data);
                 out.write(bytes);
                 out.close();
                 in.close();
             catch (Exception e) {
                 log.error("Failed to write.", e);
             }Then, I thought about it. I think it was silly to use the BufferedInputStream again snice the RandomAccessFile already reads to byte[]. Therefore, I skipped the BufferedInputStream and it works. Please correct me if I skipped the BufferedInputStream is a bad idea, thank!
           ServletOutputStream out = null;
            try {
                byte[] bytes = new byte[1024];
                raf = new RandomAccessFile(restoreZipFile, "r");
                raf.seek(10);
                raf.readFully(bytes);
                out = resp.getOutputStream();
                out.write(bytes);
                out.close();
             catch (Exception e) {
                 log.error("Failed to write.", e);
                 return false;
             }

    Your first code sample writes the data out twice, right?
    //once:
    in = new BufferedInputStream(new ByteArrayInputStream(bytes));
    int data;
    while ((data = in.read()) != -1) {
        out.write(data);
    //twice:
    out.write(bytes);And yes, the second way is simpler.

  • How to create a procedure in oracle to write the data into file

    Hi All,
    I am just wondered on how to create a procedure which will do following tasks:
    1. Concat the field names
    2. Union all the particular fields
    3. Convert the date field into IST
    4. Prepare the statement
    5. write the data into a file
    Basically what I am trying to achieve is to convert one mysql proc to oracle. MySQL Proc is as follows:
    DELIMITER $$
    USE `jioworld`$$
    DROP PROCEDURE IF EXISTS `usersReport`$$
    CREATE DEFINER=`root`@`%` PROCEDURE `usersReport`(IN pathFile VARCHAR(255),IN startDate TIMESTAMP,IN endDate TIMESTAMP )
    BEGIN
    SET @a= CONCAT("(SELECT 'User ID','Account ID','Gender','Birthdate','Account Registered On') UNION ALL (SELECT IFNULL(a.riluid,''),IFNULL(a.rilaccountid,''),IFNULL(a.gender,''),IFNULL(a.birthdate,''),IFNULL(CONVERT_TZ(a.creationDate,'+0:00','+5:30'),'') INTO OUTFILE '",pathFile,"' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '' LINES TERMINATED BY '\n' FROM account_ a where a.creationDate>='",startDate,"' and a.creationdate <='",endDate,"')");
    PREPARE stmt FROM @a;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt ;
    END$$
    DELIMITER ;
    Regards,
    Vishal G

    1. Concat the field names
    Double Pipe (||) is the concatenation operator in Oracle. There is also a function CONCAT for this purpose
    2. Union all the particular fields
    Not sure what do you mean by UNION ALL particular fields? UNION ALL is a set operation applied on two different result sets that have the same projection.
    3. Convert the date field into IST
    SQL> select systimestamp "Default Time"
      2       , systimestamp at time zone 'Asia/Calcutta' "IST Time"
      3    from dual;
    Default Time                                       IST Time
    05-05-15 03:14:52.346099 AM -04:00                 05-05-15 12:44:52.346099 PM ASIA/CALCUTTA
    4. Prepare the statement
    What do you mean by prepare the statement?
    5. write the data into a file
    You can use the API UTL_FILE to write to a file.

  • Is this true: "The pulldown is preserved in the HV20's SD output stream"?

    When you capture DV to Final Cut 6, through DV Lock from source footage HDV 24p.
    If so, how can I remove pulldown in SD?
    I've asked at the www.hv20.com forums, but I've gotten mixed answers.
    Message was edited by: marlerf

    If your delivery intent is web or computer files. ie footage to be shown on progressive displays then yes deinterlacing can help.
    for tape based delivery, tape is an interlaced format nothing to be gained by deinterlacing.
    avoid cutting on frames that are pulldown frames. if you are parked on a frame and it is flickering like it is comprised of 2 images, or shows excessive movement, don't cut on that frame cut on the one before or after. don't rely on your computer monitor ie the viewer, monitor through your camera or some device to an NTSC monitor. that will show you what you truly have.

  • MultiThreading with Input and Output Streams

    Hi,
    I have a problem and I think it's because I'm not coding correctly. Please help me if you can understand what I'm doing wrong.
    I have a server that spawns a separate thread to go off and collect data from a serial port. It also waits to accept connections from any client and if a connection is made it will send that data to the clients connected.
    There is data constantly coming in through the serial port. It is output through a DataOutputStream so when the thread is created in the server, I pipe it into a DataInputStream. I do does because I want the server to then read in the data from the inputstream and then send it out to all my clients.
    So far, the way I have it set up seems to do this. But my problem occurs when I try to close a client connection. Instead of removing the socket connection it gives me an error that it can't send data to the client, but it shouldn't be sending data to the client because I just closed it. I realize this is probably because I'm still constantly receiving data from my inputstream and the connection was closed so it can't send that data to the client. I know I need to either close the stream or close my socket but I don't know where this needs to be done. I'm stuck on the correct way to fix this.
    My second problem is the initial connection made to receive data from the inputstream. This is probably because I'm not very familiar with how input/output streams work. But instead of just sending the client the current data being received in real time, it'll send all the data that's buffered in the inputstream. I don't want all the data that's been collecting to go to that first client. I only want the recent data that is coming through while the client is connected. Does this make sense? Because after I make a second client connection I don't have this problem because the InputStream is no longer buffered up. Should I be using something else besides the DataInputStream?
    I feel like I'm going about this the wrong way. Please advise. I'm shy about showing the code but I've included the bulk of it here in hopes that someone will see what I'm doing wrong. The only part that's left out is the thread that reads from the serial port. I don't seem to have any problems with that thread.
    Thanks,
    kim
    ===
    import java.io.*;
    import java.net.*;
    import javax.comm.*;
    import java.util.*;
    // DataServer waits for a client connection
    class DataServer
         static final int PORT = 7;
         // The ServerSocket to use for accepting new connections
         private ServerSocket ss;
         // A mapping from sockets to DataOutputStreams. This will
         // help us avoid from having to create a DataOutputStream each time
         // we want to write to a stream.
         private Hashtable outputStreams = new Hashtable();
         // The inputstream that will receive serial port data through a
         // piped inputstream
         public DataInputStream datalogger;
         // Constructor and while-accept loop all in one.
         public DataServer() throws IOException
              try {
                   // Creating pipe to convert the outputstream from the
                   // RS232 Thread to an inputstream for the server to read
                   PipedOutputStream pout = new PipedOutputStream();
                   PipedInputStream pin = new PipedInputStream(pout);
                   // The inputstream that will receive data from the RS232Thread
                   datalogger = new DataInputStream(pin);
                   // Spawn the thread that will read data through from
                   // the TINI serial port
                   new RS232Thread( pout ).start();
                   // Begin listening for connections and send data
                   listen();
              } catch (IOException ioe) {
                   System.out.println("Error >> DataServer::DataServer()");
                   System.out.println(ioe.getMessage());
                   ioe.printStackTrace();
              } finally {
                   try     {
                        System.out.println( "Closing >> DataServer::DataServer()" );
                        datalogger.close();
                   } catch (IOException i ) {
                        System.out.println( "Error2 >> DataServer::DataServer()" );
                        System.out.println(i); }
         private void listen() throws IOException
              // Create the ServerSocket
              ss = new ServerSocket( PORT );
              // Inform that the server is ready to go
              System.out.println( "Listening on " + ss );
              // Keep accepting connections forever
              while (true) {
                   // Grab the next incoming connection
                   Socket s = ss.accept();
                   // Inform that connection is made
                   System.out.println( "Connection from " + s );
                   // Create a DataOutputStream for writing data to the
                   // other side
                   DataOutputStream dout = new DataOutputStream( s.getOutputStream() );
                   // Save this stream so we don't need to make it again
                   outputStreams.put( s, dout );
                   // Create a new thread for this connection, and then foret
                   // about it
                   new ServerThread( this, s );
         // Get an enumeration of all the OutputStreams, one for each client
         // connected to the server
         Enumeration getOutputStreams() {
              return outputStreams.elements();
         // Send a message to all clients (utility routine)
         void sendToAll( byte[] b ) {
              // synchronize on this because another thread might be
              // calling removeConnection() and this would screw things up
              // while it walks through the list
              synchronized( outputStreams ) {
                   // For each client...
                   for (Enumeration e = getOutputStreams(); e.hasMoreElements();) {
                        // ... get the output stream ...
                        DataOutputStream dout = (DataOutputStream)e.nextElement();
                        // ... and send the message
                        try {          
                             dout.write( b );
                        } catch(IOException ie) {                     
                             System.out.println( "Error >> ServerThread::sendToAll()" );
                             System.out.println( ie );
         // remove a socket, and it's corresponding output stream, from the
         // list. This is usually called by a connection thread that has
         // discovered that the connection to the client is dead.
         void removeConnection( Socket s ) {
              // Synchronize so it doesn't mess up sendToAll() while it walks
              // down the list of all output streams
              synchronized( outputStreams ) {
                   // Inform about removal
                   System.out.println( "Removing connection to " + s );
                   // Remove if from our hastable/list
                   outputStreams.remove( s );
                   // Make sure it's closed
                   try {
                        s.close();
                   } catch( IOException ie ) {
                        System.out.println( "Error closing " + s );
                        ie.printStackTrace();
         // main - Opens a server socket and spins off a new thread each time
         // a new client connection is accepted on this socket.
         public static void main(String[] args) throws Exception
              System.out.println("Starting DataServer version 1.0 ...");
              try     
                   new DataServer();
              catch (IOException ioe)
                   System.out.println( "Error >> DataServer::main()" );
                   System.out.println(ioe.getMessage());
                   ioe.printStackTrace();
    class ServerThread extends Thread
         //The Server that spawned this thread
         private DataServer server;
         // The Socket connected to the client
         private Socket socket;
         //Constructor
         public ServerThread( DataServer server, Socket socket )
              // save the parameters
              this.server = server;
              this.socket = socket;
              // Start up the thread
              start();
         // This runs in a separate thread when start() is called in the
         // constructor
         public void run() {
              try {
                   // The inputstream receiving data from the global inputstream
                   // that is piped to the RS232 Thread
                   // ???? is this where i'm messing up ???
                   DataInputStream in = new DataInputStream( server.datalogger );
                   int num = 0;
                   byte[] d = new byte[1];
                   // read from the inputstream over and over, forever ...
                   while( ( num = in.read(d) ) > 0 ) {
                        // ... and have the server send it to all clients
                        server.sendToAll( d );               
              } catch (IOException ioe) {
                   System.out.println( "Error >> ServerThread::run()" );
                   System.out.println(ioe.getMessage());
                   ioe.printStackTrace();
              } finally {
                   // The connection is closed for one reason or another,
                   // so have the server dealing with it
                   System.out.println( "Closing" );
                   server.removeConnection( socket );

    A couple of things to note...
    First, you are looping infinitely in your server's constructor. Since the constructor is never completing, your server object is never completely constructed - this may cause indeterminate behaviour when you pass a reference to the server to another thread.
    Second, I would recommend fixing your issues by modifying your design somewhat. The design I would recommend (read: The design I would use) is:
    A server object, with a public listen method. The constructor spawns a thread to constantly read from the serial port and forward the data read back to the server, via a multicast (sendToAll) method.
    The listen method sets up a server socket to accept connections, and in a loop opens client sockets and stores them in a set.
    The multicast method iterates through the list of open client sockets, and for each in turn confirms that it is still open. If open, send the data down the socket's output stream; if closed, remove the socket from the set.
    Note that this design includes only two threads - the main thread listens for and accepts new socket connections, while the extra thread collects data from the serial port, multicasts it to all of the open sockets, and removes all of the closed sockets. If you require to perform any other communication with the sockets, it may be necessary to create a thread for those sockets, to facilitate reading from their input streams, but in the given design, this is not necessary.
    I hope this helps,
    -Troy

  • Java Sockets and Output Streams

    Hi All,
    I am beginning sockets programming and I have a problem. If there is a server listening in the background for incoming connections and say for example 4 client programs programs which we shall call client1...client4 connect. How best can I capture the output streams associated with these newly created sockets so that the server can send back isome nformation to say clients1 and client4 only which is not seen by clients 2 and 3. Similarly I would like the server to send some infor to clients 2 and 3 only which is not seen by client1 and client 4.
    Currently I have the server listening part as shown below, but not too sure how to add DISTINCT output streams for 1 and 4 on one hand and 2 and 3 on the other.
    Thanks:
    // bind socket to a port number
    ServerSocket serverSocket = new ServerSocket(portNo);
    // create socket to listen to client connection
    while (true) {
    //listen to an incoming connection
    System.out.println("chatroom server waiting for incoming connections");
    Socket incomingSocket = serverSocket.accept();
    //launch new thread to take care of new connection
    chatRoomThread chatThread = new chatRoomThread(incomingSocket);
    chatThread.start();
    //go back and wait for next connection
    Please help.
    Thanks,
    Bleak

    HouseofHunger wrote:
    yes thats exactly the way I have my in and out streams, in the run method, but that doesn't help me in filtering traffic, in other words I am saying 2 clients, client1 and client4 for example should share a common in and out stream so that they will see eact other's messages... makes sense.....?No, doesn't make sense. That's the wrong design. Each socket should have its own input and output stream (yes, I know, that's been said several times before). If messages going to client1 should also be sent to client4, then whatever writes the messages to client1's output stream must also write them to client4's output stream. Trying to make those two output streams actually be the same output stream is the wrong way to do that. Just have the controller send the messages to whoever is supposed to get them.

  • Httpurlconnection chunked output stream?

    Hi,
    I'm developing a client that interacts with a comet server (comet allows http connections to be used for multiple inputs and outputs on same connection -- not single request, single response). I've found that I can't use HttpURLConnection because it doesn't allow writing to its output stream after any input has been read from its input stream. It seems that httpURLConn.getInputStream() basically closes the output stream. Is there any way around this? Any way to write to an httpurlconnection's output stream after a read on its input has occurred?
    For now I have to resort to using raw sockets and dealing with headers and chunked encoding manually. I'd sure love an easier way...
    Thanks for any help,
    Peter

    Get a new HTTPURLConnection. Java will cache it behind the scenes to reuse the same TCP connection.

  • IllegalStateException:output stream is already being used for this request

    How to read binary data in JSP. I know, in servlets it can be done and JSP can handle only character data. When i try to open res.getOutputStream(), it is throwing the following exception:"IllegalStateException:output stream is already being used for this request". Is there any workaround in JSP?

    Can you save the file to a location in your server ? Even if you are creating a dynamic image, all you get is a byte array which can be stored in your server. If you can do so, you can set the content type and display the content as mentioned above.

  • Separate thread for input stream and output stream.

    Hi Techies,
    In a socket connection, can we run the input stream and output stream in separate threads. actually in my case, the input stream will be getting the input regularly and output stream will send data very rare. so if i impelment them in one class then unless there is data to send output stream will be blocked. i was thinking to impelment both the streams in separate threads. is it a good way? so how to implement it. your guidance will be of great help.
    thanks in advance.

    JavaBreather wrote:
    Hi Techies,
    In a socket connection, can we run the input stream and output stream in separate threads.I would say this is the most common way of handling sockets and threads. esp pre-NIO.
    Iis it a good way? so how to implement it. your guidance will be of great help.Once you have a socket, create two threads, one which does the reading and one which does the writing.
    You could use BlockingQueues to abstract access to these threads. i.e. the reading thread reads something from the socket and adds it to the BlockingQueue. The writing thread take()s something froma second BlockingQueue and writes it to the Socket. This way you add things to write or get thing to process by looking at the BlockingQueues.

  • Runtime.exec() output streams

    Hi,
    I am using Runtime.exec() to execute a whole slew of commands - running batch files and executables. It is really important that I can see the output of my programs, and the fact that Runtime.exec() doesn't spawn its own Command-Prompts is a little disconcerting. In order to get around this, I am handling the stdout and stderr output streams from the processes synchronously with threads (as detailed in the Java Pitfall article - http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html). I call Runtime.exec(command[], env[], dir) where command contains "cmd.exe" "\C" command.
    My question is, if one of my processes crashes, will my threads receive ALL of the output from stdout and stderr just as a Command-Prompt would? If not, what is the workaround? Using Psexec to run the processes seems to bring up the Command-Prompts like I would like it to, but that doesn't always behave the way it should be, so I cannot rely on that just to see the command prompts.
    I don't want to have to switch to C# or C++ to do this at this point, but if there's no real answer, it looks like I might have to.

    cotton.m wrote:
    munky135 wrote:
    Hi,
    I am using Runtime.exec() to execute a whole slew of commands - running batch files and executables. It is really important that I can see the output of my programs, and the fact that Runtime.exec() doesn't spawn its own Command-Prompts is a little disconcerting.Hilarious. Oh. You were being serious there were you? :|Yes, I was being serious. I call runtime exec and I am not getting Command-Prompts to pop up, their output is being piped to my Java application. Reading directly from the API:
    The Runtime.exec methods may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (Process.getOutputStream(), Process.getInputStream(), Process.getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.
    >
    I don't want to have to switch to C# or C++ to do this at this point, but if there's no real answer, it looks like I might have to.Based on your attitude you should switch. You obviously hate Java and are ready to see faults that don't exist based on misconceptions that you hold. Fine, to each their own. Choose a language you like then. Which at a guess I would say is likely VB.Huh? I love Java. Any misconceptions I hold are from this guys article here - http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

  • How to write the output of a mapping into a file (OWB10.2

    Hi
    I am using Oracle 10.2.0.1 version of OWB.
    the task is to write the output of a mapping into a File (Flat File).
    Please help me out!
    Regards
    Abi

    Hi,
    Create the file format thru OWB and mention the location in OWB. Deploy the file and it will be ready to use for target load. Make sure the connection to the target loc is available.
    Regards
    Bharadwaj Hari

  • Most efficient way to write 4 bytes at the start of any file.

    Quick question: I want to write 4 bytes at the start of a file without overriding the current bytes in the file. E.g. push bytes 0-4 along... Is my only option writing the bytes into a new file then writing the rest of the file after? RAF is so close but overrides :(.
    Thanks Mel

    I revised the code to use a max of 8MB buffers for both the nio and stdio copies...
    Looks like NIO is a pretty clear winner... but your milage may vary, lots... you'd need to test this 100's of times, and normalize, to get any "real" metrics... and I for one couldn't be bothered... it's one of those things that's "fast enough"... 7 seconds to copy a 250 MB file to/from the same physical disk is pretty-effin-awesome really, isn't it? ... looks like Vista must be one of those O/S's (mentioned in the API doco) which can channel from a-to-b without going through the VM.
    ... and BTW, it took the program which produced this file 11,416 millis to write it (from an int-array (i.e. all in memory)).
    revised code
    package forums;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.nio.channels.FileChannel;
    class NioBenchmark1
      private static final double NANOS = Math.pow(10,9);
      private static final int BUFF_SIZE = 8 * 1024 * 1024; // 8
      interface Copier {
        public void copy(File source, File dest) throws IOException;
      static class NioCopier implements Copier {
        public void copy(File source, File dest) throws IOException {
          FileChannel in = null;
          FileChannel out = null;
          try {
            in = (new FileInputStream(source)).getChannel();
            out = (new FileOutputStream(dest)).getChannel();
            final int buff_size = Math.min((int)source.length(),BUFF_SIZE);
            long n = -1;
            int pos = 0;
            while ( (n=in.transferTo(pos, buff_size, out)) == buff_size ) {
              pos += n;
          } finally {
            if(in != null) in.close();
            if(out != null) out.close();
      static class NioCopier2 implements Copier {
        public void copy(File source, File dest) throws IOException {
          if ( !dest.exists() ) {
            dest.createNewFile();
          FileChannel in = null;
          FileChannel out = null;
          try {
            in = new FileInputStream(source).getChannel();
            out = new FileOutputStream(dest).getChannel();
            final int buff_size = Math.min((int)in.size(),BUFF_SIZE);
            long n = -1;
            int pos = 0;
            while ( (n=out.transferFrom(in, 0, buff_size)) == buff_size ) {
              pos += n;
          } finally {
            if(in != null) in.close();
            if(out != null) out.close();
      static class IoCopier implements Copier {
        private byte[] buffer = new byte[BUFF_SIZE];
        public void copy(File source, File dest) throws IOException {
          InputStream in = null;
          FileOutputStream out = null;
          try {
            in = new FileInputStream(source);
            out = new FileOutputStream(dest);
            int count = -1;
            while ( (count=in.read(buffer)) != -1) {
              out.write(buffer, 0, count);
          } finally {
            if(in != null) in.close();
            if(out != null) out.close();
      public static void main(String[] arg) {
        final String filename = "SieveOfEratosthenesTest.txt";
        //final String filename = "PrimeTester_SieveOfPrometheuzz.txt";
        final File src = new File(filename);
        System.out.println("copying "+filename+" "+src.length()+" bytes");
        final File dest = new File(filename+".bak");
        try {
          time(new IoCopier(), src, dest);
          time(new NioCopier(), src, dest);
          time(new NioCopier2(), src, dest);
        } catch (Exception e) {
          e.printStackTrace();
      private static void time(Copier copier, File src, File dest) throws IOException {
        System.gc();
        try{Thread.sleep(1);}catch(InterruptedException e){}
        dest.delete();
        long start = System.nanoTime();
        copier.copy(src, dest);
        long stop = System.nanoTime();
        System.out.println(copier.getClass().getName()+" took "+((stop-start)/NANOS)+" seconds");
    output
    C:\Java\home\src\forums>"C:\Program Files\Java\jdk1.6.0_12\bin\java.exe" -Xms512m -Xmx1536m -enableassertions -cp C:\Java\home\classes forums.NioBenchmark1
    copying SieveOfEratosthenesTest.txt 259678795 bytes
    forums.NioBenchmark1$IoCopier took 14.333866455 seconds
    forums.NioBenchmark1$NioCopier took 7.712665715 seconds
    forums.NioBenchmark1$NioCopier2 took 6.206867074 seconds
    Press any key to continue . . .Having said that... The NIO has lost a fair bit of it's charm... testing transferTo's return value and maintaining your own position in the file is "cumbsome" (IMHO)... I'm not even certain that mine is completely correct (?n+=pos or n+=pos+1?).... hmmm..
    Cheers. Keiths.

  • How do I get the byte size of a server file before sending output via HTTP?

    I need to get the byte size of the file prior to streaming it. I can't seem to find a class/method I need. Basically, I have the path c:\\tomcat\\webapps\\documents\\sample.pdf in the servlet, I was hoping I could just use something from the File class but I couldn't find anything that seems to do the trick?
    thanks, in advance,
    Chuck

    maybe the source of the problem will help...I am trying to stream a PDF to IE and a blank page is being generated although all other file type work.
    I have found a lot of answers in the forum but no specific code examples. Here's what I have so far from picking through threads in here (can someone please show me how to get the byte size of the file so that I can assign it to the method response.setContentLength();?):
    String CONTENT_TYPE = " ";
         String target = " ";
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
         StringBuffer buf = new StringBuffer();
         HttpSession session = request.getSession();
         String file = request.getParameter("filename");
         target = file;
         int end = file.length();
    int beg = end-2;
         String type = file.substring(beg, end);
         if (type.equals("DOC")){
              CONTENT_TYPE = "application//vnd.msword";
         }else if (type.equals("XLS")){
              CONTENT_TYPE = "application//vnd.x-excel";
         }else if (type.equals("PPT")){
              CONTENT_TYPE = "application//vnd.ms-powerpoint";
         }else if (type.equals("PDF")){
              CONTENT_TYPE = "application//vnd.x-pdf";
         }else if (type.equals("MPP")){
              CONTENT_TYPE = "application//vnd.ms-project";
         }else if (type.equals("ZIP")){
              CONTENT_TYPE = "application//ZIP";
         }else if (type.equals("TXT")){
              CONTENT_TYPE = "text//plain";
         }else {
              CONTENT_TYPE = "text//html";
         //File f = new File(file);
         //int l = f.length();
         response.setContentLength(l); <----- supposedly this fixes my problem but I don't know how to get the byte szie of the file in an integer??
         // reset the response
         response.reset();
         response.setContentType(CONTENT_TYPE);
         try{
         // Get streams
         FileInputStream fileInputStream = new FileInputStream(target);
         ServletOutputStream servletOutputStream = response.getOutputStream();
         // Init byte count and array
         int bytesRead = 0;
         byte byteArray[] = new byte[4096];
         // Read in bytes through file stream, and write out through servlet stream
         while((bytesRead = fileInputStream.read(byteArray)) != -1) {
         servletOutputStream.write(byteArray, 0, bytesRead);
              servletOutputStream.flush();
         // Flush and close streams
         servletOutputStream.flush();
         servletOutputStream.close();
         fileInputStream.close();
         } catch (Exception e) {
         System.out.println(e.toString());

  • Inserting a blob  in the table through java or Oracle ApplicationFramework

    I am new to Oracle framework. I have a requirement to insert one file(i.e blob) into the database from custom location.(ie.D:\sample.txt). through java or OAF. But i am unable to insert the file. Is there any specific function to insert blob fields in the database...........

    Did you take a look at
    <JDEV_CLIENT_INSTALL_DIR>\jdevdoc\devguide\feat\feat_fileload.htm

  • How to write the oracle data as XML format. (.XML file)

    create or replace procedure pro(p_number )
    is
    cursor c1 is select *from emp where empno=p_number;
    v_file utl_file.file_type;
    begin
    v_file := utl_file.fopen('dirc','filename.txt','w');
    for i in c1 loop
    utl_file.put_line(v_file,i.ename || i.empno ||i.job);
    end loop;
    closef(v_file);
    end;
    Now my client want instead of .txt file he need .xml files
    File should contains xml tags. can any one help regarding this.. with one example.
    How to write the oracle data as XML format. (.XML file)

    hi,
    hope this example will do something....
    SQL> select employee_id, first_name, last_name, phone_number
    2 from employees where rownum < 6
    EMPLOYEE_ID FIRST_NAME LAST_NAME PHONE_NUMBER
    100 Steven King 515.123.4567
    101 Neena Kochhar 515.123.4568
    102 Lex De Haan 515.123.4569
    103 Alexander Hunold 590.423.4567
    104 Bruce Ernst 590.423.4568
    SQL> select dbms_xmlgen.getxml('select employee_id, first_name,
    2 last_name, phone_number from employees where rownum < 6') xml
    3 from dual;
    *<?xml version="1.0"?>*
    *<ROWSET>*
    *<ROW>*
    *<EMPLOYEE_ID>100</EMPLOYEE_ID>*
    *<FIRST_NAME>Steven</FIRST_NAME>*
    *<LAST_NAME>King</LAST_NAME>*
    *<PHONE_NUMBER>515.123.4567</PHONE_NUMBER>*
    *</ROW>*
    *<ROW>*
    *<EMPLOYEE_ID>101</EMPLOYEE_ID>*
    *<FIRST_NAME>Neena</FIRST_NAME>*
    *<LAST_NAME>Kochhar</LAST_NAME>*
    *<PHONE_NUMBER>515.123.4568</PHONE_NUMBER>*
    *</ROW>*
    *<ROW>*
    *<EMPLOYEE_ID>102</EMPLOYEE_ID>*
    *<FIRST_NAME>Lex</FIRST_NAME>*
    *<LAST_NAME>De Haan</LAST_NAME>*
    *<PHONE_NUMBER>515.123.4569</PHONE_NUMBER>*
    *</ROW>*
    *<ROW>*
    *<EMPLOYEE_ID>103</EMPLOYEE_ID>*
    *<FIRST_NAME>Alexander</FIRST_NAME>*
    *<LAST_NAME>Hunold</LAST_NAME>*
    *<PHONE_NUMBER>590.423.4567</PHONE_NUMBER>*
    *</ROW>*
    *<ROW>*
    *<EMPLOYEE_ID>104</EMPLOYEE_ID>*
    *<FIRST_NAME>Bruce</FIRST_NAME>*
    *<LAST_NAME>Ernst</LAST_NAME>*
    *<PHONE_NUMBER>590.423.4568</PHONE_NUMBER>*
    *</ROW>*
    *</ROWSET>*
    ask if you want more assistance.
    thanks.

Maybe you are looking for

  • Update and move to next record

    Sorry if this is a simple question. I have a form that links to an access database. The page calls the fields and there is no problem. I want to be able to have users check the database information, and make any changes and then update and move to th

  • Page numbering in InDesign CS5.5 not working

    21 Chapters have been added to InDesign CS5.5's Book File feature. However, only the individual chapter numbers are shown, not the cumulative numbering system. Can anyone please help resolve this issue.

  • Delivery Qty should not cross order Qty

    Hi friends, When I am doing delivery for the qty more than the order qty, it is accepting. My scenario is delivery should not allow the qty more than the ordered qty. Delivery should be done only for the ordered qty.         I think it is possible. S

  • How to send database table

    how to send database table one sever to another server in the form of  tms request. i want send a particular database table one server to another as a request. give solution plz Message was edited by:         mahesh

  • Env vars for C XML parser

    I have had a lot of problems with the Oracle C XML parser. I have put a question in this forum before about SEGV errors when xmlinit is called but I suspects that somthing else (very mystery thing) is the real cause to this problem. For now is status