DataOutputStream how make wirte dev/null

R

Rafal\(sxat\)

Hi

How create DataOutputStream for writing all data to NULL because I have make
simulate write because I have calc Content-Length for make POST to server

Rf
 
M

Mark Space

Rafal(sxat) said:
How create DataOutputStream for writing all data to NULL because I have make
simulate write because I have calc Content-Length for make POST to server


I'm not sure exactly what you mean by "write to NULL" but if you just
need a sink for data, this will do:


package fubar;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io_OutputStream;

public class WriteSink extends OutputStream {


@Override
public void write(int b) {
}

@Override
public void write(byte[] b) {
}

@Override
public void write(byte[] b, int offset, int len ) {
}

public static void main(String[] args) throws IOException {
DataOutputStream dos = new DataOutputStream( new WriteSink() );

System.out.println("Start write sink...");
final int WRITES = 1*1000*1000;
for( int i =0; i < WRITES; i++ ) {
dos.write( i );
}
dos.flush();
dos.close();
System.out.println("Done.");
}
}
 
J

John B. Matthews

Mark Space said:
I'm not sure exactly what you mean by "write to NULL" but if you just
need a sink for data, this will do:

This is a good approach, as it may save a round trip through the
operating system [1, 2]. Saving the length looks like a straightforward
addition, although it seems like the data model should already know it:

package fubar;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io_OutputStream;

public class WriteSink extends OutputStream {

private int contentLength;

public int getContentLength() {
return contentLength;
}

@Override
public void write(int b) {
contentLength++;
}
// etc.
}

[Not tested.]
[1]<http://mindprod.com/jgloss/discardingoutput.html>
[2]<http://groups.google.com/group/comp.lang.java.help/browse_frm/thread/
9bb9c81384afc634/bba6a7b626b2e932>
 
M

Mark Space

John said:
public class WriteSink extends OutputStream {

private int contentLength;

public int getContentLength() {
return contentLength;
}


While I didn't show it in my example, DataOutputStream implements a
"size()" method that returns the bytes written, up to Integer.MAX_VALUE,
so there's no need to re-invent the wheel here.
 
A

Arne Vajhøj

Rafal(sxat) said:
How create DataOutputStream for writing all data to NULL because I have make
simulate write because I have calc Content-Length for make POST to server

Don't.

Wrap DataOutputStream around a ByteArrayOutputStream, write your stuff,
find length and then write the created bytes when you need to do the
actual write.

Arne
 
K

Kevin McMurtrie

"Rafal\(sxat\) said:
Hi

How create DataOutputStream for writing all data to NULL because I have make
simulate write because I have calc Content-Length for make POST to server

Rf

HTTP 1.1 chunked encoding is an excellent substitute for Content-Length.
It's simple to implement and it eliminates potentially severe
performance problems with the Content-Length header.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top