inputstream

Socket not closing serverside when calling socket.close() in client JAVA

五迷三道 提交于 2020-01-01 09:43:41
问题 I'm having problems with sockets in java. I have a ServerSocket that is listening with accept() and spawns threads for each client-request. Communication between clients and the server works fine. I am using an inputstream to read data from clients in the serverthreads, like: inputStream = mySocket.getInputStream(); bytes = inputStream.read(buffer); My problem is that if I call socket.close() from the clients, nothing happens to the blocking call of bytes = inputStream.read(buffer); , it

Android InputStream Internet Disconnect

半世苍凉 提交于 2020-01-01 05:12:25
问题 In my Android program, I have some code that downloads a file. This works fine, but since on a cell phone, you can be disconnected at any time, I need to change it do it reconnects and resumes the download when you are halfway through and somebody calls/you lose cell reception/etc. I cannot figure out how to detect the InputStream has stopped working. See the code below: InputStream in = c.getInputStream(); byte[] buffer = new byte[8024]; int len1 = 0; while ( (len1 = in.read(buffer)) > 0 ) {

java reader vs. stream

心已入冬 提交于 2019-12-31 00:46:12
问题 I was reading about Java I/O and found some interesting areas like streams, readers etc. InputStream input = new FileInputStream("input-file.txt"); int data = input.read(); while(data != -1){ data = input.read(); } I can do the same thing by using Readers as follows: Reader reader = new FileReader("input-file.txt"); int data = reader.read(); while(data != -1){ char dataChar = (char) data; data = reader.read(); } As I know, Streams are used to retrieve input from continuously flowing data. Now

Android HttpUrlConnection getInputStream throws NullPointerException

血红的双手。 提交于 2019-12-30 07:10:12
问题 I am trying to download the image from this url: https://hme_player_pictures.s3.amazonaws.com/test-512813ed3b83286c72f376c7-thumb100.jpg here is stack trace: 03-21 12:58:04.040: W/System.err(7084): java.lang.NullPointerException 03-21 12:58:04.040: W/System.err(7084): at libcore.net.http.HttpConnection$Address.hashCode(HttpConnection.java:343) 03-21 12:58:04.045: W/System.err(7084): at java.util.HashMap.get(HashMap.java:298) 03-21 12:58:04.050: W/System.err(7084): at libcore.net.http

Getting Garbage Values while reading struct data from a binary file

≯℡__Kan透↙ 提交于 2019-12-30 05:32:07
问题 Hi guys in my previous question, I was able to get the data of a struct to be loaded on a file, but now the issue is I'm getting garbage value while retrieving it. File Contents: settings.bin 110#NormalCompression Level210#NormalCompression Level310#NormalCompression Level410#NormalCompression Level510#NormalCompression Level Code #include<cstdlib> #include<iostream> #include<string> #include<iomanip> #include<fstream.h> using namespace std; const char* ErrorLogFilePath = "resources\\error

How to read drawable bits as InputStream

安稳与你 提交于 2019-12-30 05:13:06
问题 There's say some ImageView object. I want to read bits/raw data of this object as InputStream. How to do that? 回答1: First get Background image of the imageview as an object of Drawable iv.getBackground(); Then convert Drwable image into bitmap using BitmapDrawable bitDw = ((BitmapDrawable) d); Bitmap bitmap = bitDw.getBitmap(); Now use ByteArrayOutputStream to get the bitmap into the stream and get bytearray[] convert bytearray into ByteArrayInputStream you can use the following code to get

How to create a Java non-blocking InputStream from a HttpsURLConnection?

浪尽此生 提交于 2019-12-30 04:45:06
问题 Basically, I have a URL that streams xml updates from a chat room when new messages are posted. I'd like to turn that URL into an InputStream and continue reading from it as long as the connection is maintained and as long as I haven't sent a Thread.interrupt(). The problem I'm experiencing is that BufferedReader.ready() doesn't seem to become true when there is content to be read from the stream. I'm using the following code: BufferedReader buf = new BufferedReader(new InputStreamReader(ins)

Using FileChannel to write any InputStream?

和自甴很熟 提交于 2019-12-30 04:15:39
问题 Can I write any InputStream into a FileChannel? I'm using java.nio.channels.FileChannel to open a file and lock it, then writing a InputStream to the output file. The InputStream may be opened by another file, URL, socket, or anything. I've write the following codes: FileOutputStream outputStream = new FileOutputStream(outputFile); FileChannel outputChannel = outputStream.getChannel(); FileLock lock = outputChannel.lock(); try { outputChannel.transferFrom(???); } finally { lock.release();

closing nested streams [duplicate]

人盡茶涼 提交于 2019-12-29 08:54:06
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Best way to close nested streams in Java? How we close nested streams? Closing all of them? If yes what is the order? FileOutputStream out = new FileOutputStream("data.txt", true); PrintWriter pout = new PrintWriter(out); /* do some I/O */ pout.close(); out.close(); or closing the the out most stream will close all of them. pout.close(); // Is this enough? 回答1: When closing chained streams, you only need to

Create mediaplayer with inputstream in android

元气小坏坏 提交于 2019-12-29 07:06:22
问题 How to I create mediaplayer instance with inputstream? I see only 4 function for setDataSource. And there is no function getting inputstream ? is it a must to use FileDescriptor to mediaplayer ? It seems so basic. but, I couldnot find a way. In j2me, there is a function that Manager.createPlayer(InputStream) . And you can use inputstream to create a media player. Is there a way to create a mediaplayer like j2me ? 回答1: One approach can be to write your stream to a File and then give it to the