Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
I am learning GoF Java Design Patterns and I want to see some real life examples of them. Can you guys point to some good usage of these Design Patterns.(preferably in Java's core libraries). Thank you
I know the question has been discussed before, but it seems always under the assumption that inheritance is at least sometimes preferable to composition. I'd like to challenge that assumption in hopes of gaining some understanding. My question is this: Since you can accomplish anything with object composition that you can with classical inheritance and since classical inheritance is very often...
As shown below, there are two straightforward ways I could make a stream copier (bar introducing Apache Commons or similar). Which one should I go for, and why ? public class StreamCopier { private int bufferSize; public StreamCopier() { this(4096); } public StreamCopier(int bufferSize) { this.bufferSize = bufferSize; } public long copy(InputStream in , OutputStream out ) throws IOExcepti...
I have generated many pdf files in memory and I want to compress them into one zip file before sending it as a email attachment. I have looked at Rubyzip and it does not allows me to create a zip file without saving it to disk (maybe I am wrong). Is there any way I can compress those file without creating a temp file ?
I am writing an OutputStream, just noticed this in the OutputStream interface, public abstract void write(int b) throws IOException; This call write one byte to the stream but why it takes integer as an argument?
I set up a server with a ServerSocket, connect to it with a client machine. They're directly networked through a switch and the ping time is <1ms. Now, I try to push a "lot" of data from the client to the server through the socket's output stream. It takes 23 minutes to transfer 0.6Gb. I can push a much larger file in seconds via scp. Any idea what I might be doing wrong? I'm basically...
I have come across these two terms and my understanding of them seem to overlap with each other. Flush is used with buffers and sync is used to talk about persisting changes of file to disk. In C , fflush(stdin) makes sure that the buffer is cleared. And fsync to persist changes file to disk. If these concepts are not universally defined, would prefer a linux, java explanation. found a relat...
I'm trying to upload a file via URLConnection, but I need to read/write it as a binary file without any encoding changes. So i've tried to read byte[] array from a FileInputStream, but now i have an issue. The PrintWriter object I use for outputing to the server does not allow me to do writer.write(content) (where content is of type byte[]). How can i fix this? Or is there another way to quick...
I know the general idea but I'm just not sure if it has an effect since the Android api states following: "Flushes this stream. Implementations of this method should ensure that any buffered data is written out. This implementation does nothing." This implementation does nothing <-- does that mean its useless to do or am I missing something?
What is a stream in the programming world ? Why do we need it ? Kindly explain with the help of an analogy, if possible.
Why some methods that write bytes/chars to streams takes int instead of byte/char?? Someone told me in case of int instead of char: because char in java is just 2 bytes length, which is OK with most character symbols already in use, but for certain character symbols (chines or whatever), the character is being represented in more than 2 bytes, and hence we use int instead. How far this explan...
I am currently attempting to modify some open source software in JSP and am unaware of the syntax. How does one dump a complex variable to the browser using JSP?
We have a system where a client makes an HTTP GET request, the system does some processing on the backend, zips the results, and sends it to the client. Since the processing can take some time, we send this as a ZipOutputStream wrapping the response.getOutputStream(). However, when we have an exceptionally small amount of data in the first ZipEntry, and the second entry takes a long time, the...
I need to convert numerical values into byte arrays. For example, to convert a long to a byte array, I have this method: public static byte[] longToBytes(long l) { ByteBuffer buff = ByteBuffer.allocate(8); buff.order(ByteOrder.BIG_ENDIAN); buff.putLong(l); return buff.array(); } It's pretty straightforward - take a long, allocate an array that can hold it, and throw it in there. R...
which is the right way to download a file using JSF?, just putting a link to the file ?? in that case how do i get the file URL?? i have seen one example using BufferedInputStream: http://www.winstonprakash.com/articles/jsf/file_download_link.htm What are the differences? Thanks
In Really force file sync/flush in Java, the author writes in the summary of the answers: Use c.force(true) followed by s.getFD().sync() for Java NIO My question is: do you really need both? Isn't force enough? Aren't force and sync just different interfaces for doing the same thing? I can't find anyplace where this is confirmed.
Our app sends the contents of the derby.log file to our server whenever Apache Derby throws a SQLException in our app. In order to get detailed logs, we are setting the 'derby.infolog.append' property to true. However, we are noticing enormously large logs files since the logs also contain bootup output each time a connection is made to the database. NOTE: we are using Derby in embedded mode...
Two quotes: All of the remaining messages in the protocol take the form of <length prefix><message ID><payload>. The length prefix is a four byte big-endian value. The message ID is a single decimal byte. The payload is message dependent. request: <len=0013><id=6><index><begin><length> The request message is fixed length, and is used t...
If I have a file, and I want to literally write '42' to it (the value, not the string), which for example is 2a in hex, how do I do it? I want to be able to use something like outfile.write(42) or outfile.write(2a) and not write the string to the file. (I realize this is a simple question but I can't find the answer of google, probably because I don't know the correct search terms)
I am working on sound processing with the use of Java now. Within my project, I have to deal with the stream. So I have a lot of staffs to do with DataLine and OutputStream or InputStream. But to me, they are too similar:( Is there someone who can help me with this question? Thanks in advance! Here are some code I used : TargetDataLine line; ByteArrayOutputStream out = new By...
I'am a big fan of http://php.net/manual/en/function.flush.php Can I do the same thing using Java? thanks for your help
  /*
   * Copyright 1994-2004 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;

This abstract class is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink.

Applications that need to define a subclass of OutputStream must always provide at least a method that writes one byte of output.

 
 public abstract class OutputStream implements CloseableFlushable {
    
Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Subclasses of OutputStream must provide an implementation for this method.

Parameters:
b the byte.
Throws:
IOException if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
 
     public abstract void write(int bthrows IOException;

    
Writes b.length bytes from the specified byte array to this output stream. The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length).

Parameters:
b the data.
Throws:
IOException if an I/O error occurs.
See also:
write(byte[],int,int)
 
     public void write(byte b[]) throws IOException {
         write(b, 0, b.length);
     }

    
Writes len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.

The write method of OutputStream calls the write method of one argument on each of the bytes to be written out. Subclasses are encouraged to override this method and provide a more efficient implementation.

If b is null, a NullPointerException is thrown.

If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.

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. In particular, an IOException is thrown if the output stream is closed.
    public void write(byte b[], int offint lenthrows IOException {
        if (b == null) {
            throw new NullPointerException();
        } else if ((off < 0) || (off > b.length) || (len < 0) ||
                   ((off + len) > b.length) || ((off + len) < 0)) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return;
        }
        for (int i = 0 ; i < len ; i++) {
            write(b[off + i]);
        }
    }

    
Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

The flush method of OutputStream does nothing.

Throws:
IOException if an I/O error occurs.
    public void flush() throws IOException {
    }

    
Closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.

The close method of OutputStream does nothing.

Throws:
IOException if an I/O error occurs.
    public void close() throws IOException {
    }
New to GrepCode? Check out our FAQ X