creating a java wrapper for an inputstream function - java

I am trying to read a fileinputstream that then gets passed to a calling function which is then played back in a webserver application viewer. The download works fine but the problem is that if I close the stream (which is recommended) within the same function the calling function receives a null stream for some reason. Someone suggested creating an inputstream wrapper and this will allow me to close the stream, delete the file and still pass the stream to the calling function.
public InputStream exportVideo(Youtube connection, String videoID) {
InputStream is = null;
....//file retrieval code above
is = new FileInputStream(ndirectory + "\\" + this.videoID.toString()
+ ".avi");
return is;
....//trace handling code below
finally{
is.close();
}
}
the calling function:
stream = FetchVideo.exportVideo(econnection, videoID);
what I am thinking the suggestion meant was have some class:
public class StreamConverter extends InputStream {
be the wrapper, but I have no idea how to do this.
Any advice or ideas / links on how to do this effectively. Again the problem is closing the stream but being able to pass it on to the calling function.

You should move the is.close() call out of the method and close it where you get it e.g
try {
stream = FetchVideo.exportVideo(econnection, videoID);
//do something with the stream
}
finally {
if (stream != null) {
stream .close();
}
}
or it's better to use try with resource
try (InputStream stream = FetchVideo.exportVideo(econnection, videoID)) {
//do something with the stream
}

You could use composition to implement this InputStream.
Just override all methods so they call your delegate object.
But I am not sure this is the right way to do what you want. I think the best option here is to add an output stream to the function. The function will then write the content it reads to the output stream and the caller is now in charge of closing the stream. (Easy way to write contents of a Java InputStream to an OutputStream)

Related

Reading InputStream from webservice response

What I am trying to do seems to be a quite simple thing, get an InputStream from a Jersey webservice which is returned from a class RestResponse. But I don't get the stream to my client:
public class RestResponse {
private InputStream responseStream;
public RestResponse(InputStream responseBodyStream) throws IOException{
this.responseStream = responseBodyStream;
//here I can get the stream contents from this.responseStream
}
public InputStream getResponseStream() throws IOException {
//here stream content is empty, if called from outside
//only contains content, if called from constructor
return this.responseStream;
}
}
public class HttpURLConnectionClient{
public RestResponse call(){
try{
....
InputStream in = httpURLConnection.getInputStream();
RestResponse rr = new RestResponse(in);
}finally{
in.close(); <- this should be the suspect
}
}
}
RestResponse rr = httpURLConnectionClient.call()//call to some url
rr.getResponseStream(); //-> stream content is empty
Any ideas, what I am missing? Is is not possible to just pipe the stream through?
Certain types of InputStream can only be read once in Java. Based on your comment above, it appears that you are using the InputStream when you pipe it to System.out. Try commenting out the call to System.out and see if you can access your InputStream. Also make sure that the stream is not being consumed anywhere else in the code before the point where you need it.
Update:
It appears that your actual problem was being caused by closing the InputStream before you got a chance to use it. So the solution is to keep the stream open until you need it, and close it afterwards.
Typically, it is not a good design practice to open a stream and keep it open for a long time, because then the underlying resource won't be available to anyone else who needs it. So you should open the stream, and consume it only when you actually need it.

Is there a way to determine which OutputStream is currently sending through to my sockets inputstream?

This is my client right now:
#Override
public void run()
{
try
{
#SuppressWarnings("resource")
InputStream socketIn = socket.getInputStream();
Scanner in = new Scanner(socketIn);
PrintWriter out = new PrintWriter(socket.getOutputStream());
ObjectInputStream oin = new ObjectInputStream(socketIn);
gui = new GuiController(out);
while (true)
{
System.out.println(socketIn);
if(in.hasNext()){
gui.updateResponse(in.nextLine());
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
As you can see, I have both an ObjectInputStream as well as a Scanner. This is because I am both sending simple Strings from my Server, which should be caught by the string, as well as Hashtable-Objects, which are dealt by the ObjectInputStream.
However, now I need to determine whether my Servers PrintWriter or ObjectOutputStream sent the data, so I can react to it. If I don't make any check, my Scanner will just grab the object and print it as a string, which is obviously very wrong.
I need something like this:
if(socketIn.origin == ObjectOutputStream){
}
else{
}
Note that this is just some pseudo code, but I think you get the idea.
Disclaimer: This is certainly not the recommended way of doing this for that look at EJP's answer.
But what you could do:
Before you send anything through your object stream or string stream, send a string through the string stream telling the client what to expect. After that you send the actual data via either the string stream or the object stream. So in pseudocode this would be:
read string with string input stream
if string==object then
read data with object input stream
handle object
else if string==string
read data with string input stream
handle string
This would work as you first receive what is coming, and then you read the actual data.
For the sake of completeness here's the pseudo code for the sender:
if the data to send==object
print 'object' to the string output stream
print object to the object ouput stream
else if data to send==string
print 'string' to the string output stream
print data string to the string ouput stream
I hope this helps you :)
NOTE: Be very carefull doing this when your socket is used by multiple threads.
NOTE: You could also wrap your string in an object and just send it through the object stream.
No. There is only one data stream. This will never work, for a number of reasons. Use a single stream, or reader/writer pair, or DataInput/OutputStream pair, or ObjectInput/OutputStream pair.

Query on proper usage of OutputStream

Basically attempting to send video data and trying to understand how this whole process works, not sure whether I've put this together properly. Any help would be greatly appreciated.
public void OutputStream(BufferedOutputStream out) throws MalformedURLException {
URL url = new URL("http://www.android.com//");
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
out = new BufferedOutputStream(urlConnection.getOutputStream());
out = new BufferedOutputStream(new FileOutputStream(String.valueOf(mVideoUri)), 8 * 1024);
} catch (IOException e) {
e.printStackTrace();
} finally {
assert urlConnection != null;
urlConnection.disconnect();
}
}
You aren't technically using the output stream at all in this case, merely reassigning it several times. The input parameter out is being reassigned within the method but never used prior which doesn't seem like what you want to do at all since whatever existing output stream instance reference passed to this method is simply discarded.
You reassign out once more and discard the buffered of the socket connection on this line:
new BufferedOutputStream(urlConnection.getOutputStream());
which is mostly harmless in that there isn't any resource leakage (given that disconnect() is called) but once again doesn't seem like what you want to do.
Your code also has resource leakage on the last out given that it is not closed anywhere within the try-catch-finally block which is a serious flaw. Additionally, the usage of assertions to check for nulls on out needs to be promoted to a if-statement to handle the very real possibility that out is null in case of a failed URL resolution/open. Assertion tests can be turned off, in which you'd get a NPE (and when turned on, you'll get an AssertionError, nether of which is better).
Whilst it's hard to anticipate exactly what your project structure is, the general contract of output stream usage can be seen as follows:
public void foo(){
OutputStream out = null;
byte[] data = ... // Populated from some data source
try{
out = ... // Populated from some source
out.write(data); // Writes the data to the output destination
}catch(IOException ex){
// Handle exception here
}finally{
// Only attempt to close the output stream if it was actually opened successfully
if(out != null){
try{
out.close();
}catch(IOException closeEx){
// Handle, propogate upwards or log it
}
}
}
}
The output stream is used within the try block such that any exceptions will result in the finally block closing the stream as appropriate, removing the resource leakage. Note the sample write() method in the try block, illustrating in the most basic form how OutputStreams can be used to put data into some destination.
Under java 7 (and above), the above example is more compact:
public void foo(){
byte[] data = ... // Populated from some data source
try(OutputStream out = ...){
out.write(data); // Writes the data to the output destination
}catch(IOException ex){
// Handle exception here
}
}
Utilizing try-with-resources, resource safety can be assured thanks to the AutoClosable interface and java 7 (and above's) new syntax. There is one small difference in that exceptions from closing the stream are also bunched into the same catch block instead of being separate as in the first example.

How to append CharStreams?

When I use the object OutputSupplier, adding the first line (CharStreams.write) is executed correctly. But then calling CharStreams.write again throws an IOException.
Is this the correct behavior of the object? If so, how can you append a String to the supplier object without closing the inline stream?
...
final Process process = Runtime.getRuntime().exec("su");
OutputSupplier<OutputStreamWriter> writerSupplier = CharStreams.newWriterSupplier(new OutputSupplier<OutputStream>() {
#Override
public OutputStream getOutput() throws IOException {
return process.getOutputStream();
}
}, Charsets.UTF_8);
// ok
CharStreams.write(someCommand, writerSupplier);
...
// IOException
CharStreams.write(otherCommand, writerSupplier);
This is definitely expected behavior.
CharStreams.write with an OutputSupplier opens the output stream, writes, and closes it. That's part of the point. Presumably, Process.getOutputStream() doesn't let you open and close more than once.
Either do all the writes at once, or more likely, don't use CharStreams.write and deal with closing the stream yourself.

How / when to delete a file in java?

The problem is, user clicks a button in JSP, which will export the displayed data. So what i am doing is, creating a temp. file and writing the contents in it [ resultSet >> xml >> csv ], and then writing the contents to ServletResponse. After closing the respons output stream, i try to delete the file, but every time it returns false.
code;
public static void writeFileContentToResponse ( HttpServletResponse response , String fileName ) throws IOException{
ServletOutputStream responseoutputStream = response.getOutputStream();
File file = new File(fileName);
if (file.exists()) {
file.deleteOnExit();
DataInputStream dis = new DataInputStream(new FileInputStream(
file));
response.setContentType("text/csv");
int size = (int) file.length();
response.setContentLength(size);
response.setHeader("Content-Disposition",
"attachment; filename=\"" + file.getName() + "\"");
response.setHeader("Pragma", "public");
response.setHeader("Cache-control", "must-revalidate");
if (size > Integer.MAX_VALUE) {
}
byte[] bytes = new byte[size];
dis.read(bytes);
FileCopyUtils.copy(bytes, responseoutputStream );
}
responseoutputStream.flush();
responseoutputStream.close();
file.delete();
}
i have used 'file.deleteOnExit();' and file.delete(); but none of them is working.
file.deleteOnExit() isn't going to produce the result you want here - it's purpose is to delete the file when the JVM exits - if this is called from a servlet, that means to delete the file when the server shuts down.
As for why file.delete() isn't working - all I see in this code is reading from the file and writing to the servlet's output stream - is it possible when you wrote the data to the file that you left the file's input stream open? Files won't be deleted if they're currently in use.
Also, even though your method throws IOException you still need to clean up things if there's an exception while accessing the file - put the file operations in a try block, and put the stream.close() into a finally block.
Don't create that file.
Write your data directly from your resultset to your CSV responseoutputStream.
That saves time, memory, diskspace and headache.
If you realy need it, try using File.createTempFile() method.
These files will be deleted when your VM stops normaly if they haven't been deleted before.
I'm assuming you have some sort of concurrency issue going on here. Consider making this method non-static, and use a unique name for your temp file (like append the current time, or use a guid for a filename). Chances are that you're opening the file, then someone else opens it, so the first delete fails.
as I see it, you are not closing the DataInputStream dis - this results to the false status, when you do want to delete file. Also, you should handle the streams in try-catch-finally block and close them within finally. The code is a bit rough, but it is safe:
DataInputStream dis = null;
try
{
dis = new DataInputStream(new FileInputStream(
file));
... // your other code
}
catch(FileNotFoundException P_ex)
{
// catch only Exceptions you want, react to them
}
finally
{
if(dis != null)
{
try
{
dis.close();
}
catch (IOException P_ex)
{
// handle exception, again react only to exceptions that must be reacted on
}
}
}
How are you creating the file. You probably need to use createTempFile.
You should be able to delete a temporary file just fine (No need for deleteOnExit). Are you sure the file isn't in use, when you are trying to delete it? You should have one file per user request (That is another reason you should avoid temp files and store everything in memory).
you can try piped input and piped output stream. those buffers need two threads one to feed the pipe (exporter) and the other (servlet) to consume data from the pipe and write it to the response output stream
You really don't want to create a temporary file for a request. Keep the resulting CSV in memory if at all possible.
You may need to tie the writing of the file in directly with the output. So parse a row of the result set, write it out to response stream, parse the next row and so on. That way you only keep one row in memory at a time. Problem there is that the response could time out.
If you want a shortcut method, take a look at Display tag library. It makes it very easy to show a bunch of results in a table and then add pre-built export options to said table. CSV is one of those options.
You don't need a temporary file. The byte buffer which you're creating there based on the file size may also cause OutOfMemoryError. It's all plain inefficient.
Just write the data of the ResultSet immediately to the HTTP response while iterating over the rows. Basically: writer.write(resultSet.getString("columnname")). This way you don't need to write it to a temporary file or to gobble everything in Java's memory.
Further, most JDBC drivers will by default cache everything in Java's memory before giving anything to ResultSet#next(). This is also inefficient. You'd like to let it give the data immediately row-by-row by setting the Statement#setFetchSize(). How to do it properly depends on the JDBC driver used. In case of for example MySQL, you can read it up in its JDBC driver documentation.
Here's a kickoff example, assuming that you're using MySQL:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/csv");
response.setCharacterEncoding("UTF-8");
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
PrintWriter writer = response.getWriter();
try {
connection = database.getConnection();
statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
statement.setFetchSize(Integer.MIN_VALUE);
resultSet = statement.executeQuery("SELECT col1, col2, col3 FROM tbl");
while (resultSet.next()) {
writer.append(resultSet.getString("col1")).append(',');
writer.append(resultSet.getString("col2")).append(',');
writer.append(resultSet.getString("col3")).println();
// Note: don't forget to escape quotes/commas as per RFC4130.
}
} catch (SQLException e) {
throw new ServletException("Retrieving CSV rows from DB failed", e);
} finally {
if (resultSet != null) try { resultSet.close; } catch (SQLException logOrIgnore) {}
if (statement != null) try { statement.close; } catch (SQLException logOrIgnore) {}
if (connection != null) try { connection.close; } catch (SQLException logOrIgnore) {}
}
}
That's it. This way effectlvely only one database row is been kept in the memory all the time.

Categories

Resources