FAQ Database Discussion Community


Reusing ObjectInputStream throws StreamCorruptedException

java,sockets,networking,inputstream,outputstream
I have a client-server application. Where the client sends an object and the server sends the same object back, a simple echo server. The code below works fine if I am making a new instance of Client for every new request. However, if I use an existing instance of the...

How to correctly close OutputStream?

java,file,outputstream
Below you find the constructor of a server in order to setup it. In a few words, it restore its state from the last time that it was up reading a List of objects (called log) and two int (called currentTerm and votedFor). Any time that any of these "volatile"...

Java stream of byte. Read operation sometimes doesn't read what was written

java,stream,byte,inputstream,outputstream
I'm trying to implement a communication system with an automatic repeat request strategy. I use three classes: Transmitter, Channel, Receiver. I have a maximum number of byte for message (window). But when I receive the byte sent, sometimes I receive less bytes than window. Why? My code is this: Transmitter...

Writing to a text file within a jar file

java,jar,outputstream
I was recently trying to export my game to a jar file, and I ran into a problem with converting files to input and output streams. With the regular code that was not exported, I was able to read and write to text files by simply calling the Scanner and...

Why do I have to close the ZipOutputStream in a certain way in this situation?

java,file,outputstream,zipoutputstream
I have two examples : Example 1: try (ByteArrayOutputStream baous = new ByteArrayOutputStream(); FileOutputStream fouscrx = new FileOutputStream(new File(output, "example"))) { try (ZipOutputStream zous = new ZipOutputStream(baous)) { for (File file: files) { try (FileInputStream fis = new FileInputStream(file)) { ZipEntry zipEntry = new ZipEntry(file.getPath().substring(output.getPath().length() + 1)); zous.putNextEntry(zipEntry); byte[] bytes...

How to convert a Blob object into a PDF file in Java?

java,pdf,blob,inputstream,outputstream
I have the following situation into a Java application. From the database a retrieve this Blob object that is the representation of a PDF file on the DB: Blob blobPdf = cedolinoPdf.getAllegatoBlob(); Now I have to convert it into a PDF file. How can I do this task? Tnx...

Java BufferedWriter and OutputStream .close() method

java,outputstream,bufferedwriter
I'm new in java and there is a question about BufferedWriter and OutputStream closing. I have some logic, where it is inconvenient to use try-with-resources: public static void writeFile(String fileName, String encoding, String payload) { BufferedWriter writer = null; OutputStream stream = null; try { boolean needGzip = payload.getBytes(encoding).length >...

The values of int fmt in format()

c++,printing,outputstream,opencv3.0
Currently using OpenCV3, but in OpenCV2, there was a way to format matrix outputs. It is used in a tutorial here. The format function seems to have changed from format(mat, str) in OpenCV2 to format(mat, int) OpenCV3. Making this call: cout << "4x4: " << endl << format(fourByFour, "python") <<...

Why does writing to the stream in this test work in IntelliJ's “Run”, but not when I use Maven “test”?

java,outputstream,fileoutputstream,java-6,xmlstreamwriter
In my application I have a problem where the the OutputStream does not write anything to the file when I run "mvn clean test", but when I run it in IntelliJ by right-clicking on run testWritingToFile(), it works fine and outputs to the file. I have the following test: @Test...

How to put two jasperReports in one zip file to download?

jsf-2,jasper-reports,outputstream,zipoutputstream
public String generateReport() { try { final FacesContext facesContext = FacesContext.getCurrentInstance(); final HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse(); response.reset(); response.setHeader("Content-Disposition", "attachment; filename=\"" + "myReport.zip\";"); final BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream()); final ZipOutputStream zos = new ZipOutputStream(bos); for (final PeriodScale periodScale : Scale.getPeriodScales(this.startDate,...

Android Studio open failed: EROFS (Read-only file system) when creating a File

android,file,outputstream
I know this question has been asked before, but I can't make it to work. Please help. I added the external storage permission, why is it still read-only? public class MainActivity extends Activity { public static final String LOGTAG="EXPLORECA"; public static final String USERNAME="pref_username"; public static final String VIEWIMAGE="pref_viewimages"; private...

write multiple images to outputstream in java

java,outputstream
I have a folder called images under resources folder in eclipse. And I have multiple images in images folder. I want to display all the images in my browser by calling a web-service. I have tried the following code.I am able to retrieve only one image.I want this for multiple...

Python IO: disable standard output stream and print what we want [duplicate]

python,io,output,outputstream
This question already has an answer here: Temporarily Redirect stdout/stderr 6 answers Say I have a python program composed of three lines. if __name__=“__main__”: foo( ) print “Hello” Here, foo( ) is a third-part function that outputs things to the standard output. E.g, foo( ) has only one line...

How do I write Objects through a ObjectOutputStream, when the Objects are created in a different class?

java,encryption,inputstream,outputstream
I've tried to sum up my question, but really there's more behind it. For a task at uni we are supposed to create two constructors (English isn't my first language) 1) public SecureInputStream(InputStream base, OutputStream remote) 2) public SecureOutputStream(OutputStream base, InputStream remote) 1) is supposed to create BigIntegers g,n,k then...

Why getProperty method doesn't print out the working directory?

java,sockets,client-server,inputstream,outputstream
I am trying to build a simple client-server aplication. Now I want to print out the current working directory of the server. I have tried by using String workingDir= System.getProperty("user.dir"); method. But it doesn't actually print out the directory. I don't know why. I need help. package clint; import java.io.*;...

Is it necessary to close each nested OutputStream separately?

java,outputstream
I am writing a piece of code: OutputStream outputStream = new FileOutputStream(createdFile); GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(gzipOutputStream)); Do I need to close every stream or writer like the following? gzipOutputStream.close(); bw.close(); outputStream.close(); Or will just closing the last stream be fine? bw.close(); ...

I just send parts of the image through socket using Java

java,image,sockets,outputstream
I used the code from Here as below and got only half of the picture sent, could anybody tell me the reason? The code to get the size and read the size is too complex to me so I couldn't figure it out. public class Send { public static void...

FileOutputStream vs OutputStream, why and when?

java,outputstream
So, you can save a form file through several methods, I guess, I use 2, but I never really know when to use which. I have these 2 pieces of codes that do the same: 1-This writes my form file to specified path. FormFile archivo = myForm.getArchivo(); File newFile =...

JUnit asserting two Strings whether they're equal or not

java,junit,outputstream
So I looked up this question and tried it, but with no success. My code should be testing if the method is correctly outputing the text onto the console by reading it back in using Streams. ByteArrayOutputStream outStream = new ByteArrayOutputStream(); PrintStream myStream = new PrintStream(outStream); System.setOut(myStream); o.doSomething(); //printing out...

ObjectOutputSteam Sending Multiple Objects Before Receiving

java,oop,inputstream,outputstream
I am sending 2 objects using an ObjectOutputSteam. Can I send the second one before receiving the first one with an ObjectInputStream? Thanks!...

When OutputStream is stored as a member, it doesn't seem to work when writing to it using an XmlStreamWriter, can it not be passed as a member?

java,xml,outputstream,fileoutputstream,class-members
I am trying to store an OutputStream as a member of a class so that I can write to it from multiple methods. I put together this jUnit test to demonstrate the problem I have. public class XmlStreamWriterTest { private OutputStream outputStream; @Before public void setUp() throws Exception { File...

copying files in android with input/output stream: the good and the bad way

java,android,inputstream,outputstream
i have made this two routines to copy files using inputstream and outpustream. they are quite the same however the second one rise ArrayIndexOutOfBoundsException while the first one works flawlessly and i don't know why: public void CopyStream(long size, InputStream is, OutputStream os) { final int buffer_size = 4096; byte[]...

Show values of different array types

java,arrays,outputstream
I have a question about the output of Arrays in Java. So, when I have this code: char[] chars = new char[] {'a', 'b', 'c'}; System.out.println(chars); Then the output is: abc. I know that an Array is an Object in Java. So I think the toString() method will be called...

OutputStream class is used for writing into files. How is it possible?

java,io,outputstream
The below code is quoted from : http://examples.javacodegeeks.com/core-java/io/fileoutputstream/java-io-fileoutputstream-example/ Although the OutputStream is an abstract method, at the below code, OutputStream object is used for writing into the file. Files.newOutputStream(filepath)) returns OutputStream. Then, the type of out is OutputStream, and out references OutputStream. How can this be possible while OutputStream is...