Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
I'm trying to do local IPC using Sockets and Object streams in Java however I'm seeing poor performance. I am testing the ping time of sending an object via an ObjectOutputStream to receiving a reply via an ObjectInputStream over a Socket. Here's the Requestor: public SocketTest(){ int iterations = 100; try { Socket socket = new Socket("localhost", 1212); ObjectInpu...
How buffered streams are working on the background and how it actually differs and what is the real advantage of using the same? Another Query,.. Since DataInputSytream is also Byte based, but it is having methods to readLine().. Whats the point in here
I have a problem, which I do not seem to be able to solve... I do a http download of a file, but the CRC32 of the file on the server and on the client do not match. Also, the file has different size, so obviously I must be doing something wrong... when I download via Firefox, the filesize is ok... so I guess it is somewhere in the client code. I already found http://stackoverflow.com/questions...
I need to write(append) huge string to flat file using java nio. The encoding is ISO-8859-1. Currently we are writing as shown below. Is there any better way to do the same ? public void writeToFile(Long limit) throws IOException{ String fileName = "/xyz/test.txt"; File file = new File(fileName); FileOutputStream fileOutputStream = new FileOutputStream(file, true); ...
I have a piece of code that reads hell of a lot (hundreds of thousand) of relatively small files (couple of KB) from the local file system in a loop. For each file there is a java.io.FileInputStream created to read the content. The process its very slow and take ages. Do you think that wrapping the FIS into java.io.BufferedInputStream would make a significant difference?
I am wondering the purpose of BufferedOutputStream, performance gain when using it?
How do I append to large files efficiently. I have a process that has to continually append to a file and as the file size grows the performance seems to slow down as well. Is there anyway to specify a large buffer size with the append
I've seen lots of examples of sending serialized data over sockets in Java, but all I want is to send some simple integers and a string. And, the problem is I'm trying to communicate these to a binary written in C. So, bottom line: how can I just send some bytes over a socket in Java?
Hallo, you can give the BufferedOutputStream constructor a int parameter for the buffer size. I my szenario I have one process writing to the disk and on process reading from the disk. Having a default buffer of 8192 bytes causes high fragmentation of big files. Now I was wondering if I could reduce the fragmentation if I lift the buffer size to 1 Mb. I think this should work. But does the B...
I am using a BufferedOutputStream to write to a file. While debugging I noticed that the flush operation takes very long even if the OS shows that the file has got the final size. My theory: The BufferedOutputStream tell the OS to reserve space in advance? So the OS already shows the file with the full filesize even if the BufferedOutputStream hasn't flushed completly? Is that correct?
Is there a way to force the temporary files created in a java program in memory? Since I use several large xml file, I would have advantages in this way? Should I use a transparent method that allows me to not upset the existing application. UPDATE: I'm looking at the source code and I noticed that it uses libraries (I can not change) which requires the path of those files ... Thanks
I need to write blocks of data (characters) and I don't care about the sequence of those blocks. I wonder what kind of OutputStream I should use to achieve high performance?
  /*
   * Copyright 1994-2003 Sun Microsystems, Inc.  All Rights Reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
  *
  * This code is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 package java.io;

The class implements a buffered output stream. By setting up such an output stream, an application can write bytes to the underlying output stream without necessarily causing a call to the underlying system for each byte written.

Author(s):
Arthur van Hoff
Since:
JDK1.0
 
 public
 class BufferedOutputStream extends FilterOutputStream {
    
The internal buffer where data is stored.
 
     protected byte buf[];

    
The number of valid bytes in the buffer. This value is always in the range 0 through buf.length; elements buf[0] through buf[count-1] contain valid byte data.
 
     protected int count;

    
Creates a new buffered output stream to write data to the specified underlying output stream.

Parameters:
out the underlying output stream.
 
     public BufferedOutputStream(OutputStream out) {
         this(out, 8192);
     }

    
Creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size.

Parameters:
out the underlying output stream.
size the buffer size.
Throws:
java.lang.IllegalArgumentException if size <= 0.
 
     public BufferedOutputStream(OutputStream outint size) {
         super(out);
         if (size <= 0) {
             throw new IllegalArgumentException("Buffer size <= 0");
         }
          = new byte[size];
     }

    
Flush the internal buffer
 
     private void flushBuffer() throws IOException {
         if ( > 0) {
             .write(, 0, );
              = 0;
         }
     }

    
Writes the specified byte to this buffered output stream.

Parameters:
b the byte to be written.
Throws:
IOException if an I/O error occurs.
 
     public synchronized void write(int bthrows IOException {
         if ( >= .) {
             flushBuffer();
         }
         [++] = (byte)b;
     }

    
Writes len bytes from the specified byte array starting at offset off to this buffered output stream.

Ordinarily this method stores bytes from the given array into this stream's buffer, flushing the buffer to the underlying output stream as needed. If the requested length is at least as large as this stream's buffer, however, then this method will flush the buffer and write the bytes directly to the underlying output stream. Thus redundant BufferedOutputStreams will not copy data unnecessarily.

Parameters:
b the data.
off the start offset in the data.
len the number of bytes to write.
Throws:
IOException if an I/O error occurs.
    public synchronized void write(byte b[], int offint lenthrows IOException {
        if (len >= .) {
            /* If the request length exceeds the size of the output buffer,
               flush the output buffer and then write the data directly.
               In this way buffered streams will cascade harmlessly. */
            flushBuffer();
            .write(bofflen);
            return;
        }
        if (len > . - ) {
            flushBuffer();
        }
        System.arraycopy(bofflen);
         += len;
    }

    
Flushes this buffered output stream. This forces any buffered output bytes to be written out to the underlying output stream.

Throws:
IOException if an I/O error occurs.
See also:
FilterOutputStream.out
    public synchronized void flush() throws IOException {
        flushBuffer();
        .flush();
    }
New to GrepCode? Check out our FAQ X