datainputstream

After sending byte arrays via dataoutputstream, the received byte arrays are not equal to original byte arrays

拈花ヽ惹草 提交于 2019-12-12 04:46:46
问题 For some reasons, I need to send multiple byte arrays separately via server socket, and client socket will receive these byte arrays. After sending byte arrays, I found byte arrays received in client socket are not equal to those in server socket. If I use ObjectOutputStream and ObjectInputStream, then everything works fine, but for my need, I can't use ObjectOutputStream & ObjectInputStream, because my server need to connect more than 2 sockets. Here is Server and Client code: Server(Sender)

read a text file android

怎甘沉沦 提交于 2019-12-12 01:31:38
问题 I am trying to make the computer read a text file full of words and add it to an ArrayList. I made it work on a regular Java application, but can't get it to work on Android. Can someone help me out? try { FileInputStream textfl = (FileInputStream) getAssets().open("test.txt"); DataInputStream is = new DataInputStream(textfl); BufferedReader r = new BufferedReader(new InputStreamReader(is)); String strLine; while ((strLine = r.readLine()) != null) { tots.add(strLine); //tots is the array list

java DataOutputStream getOutputStream() getInputStream()

半城伤御伤魂 提交于 2019-12-11 09:06:47
问题 one question in the case for example of DataOutputStream output= new DataOutputStream(clientSocket.getOutputStream()) ; or DataInputStream in = new DataInputStream(clientSocket.getInputStream()); must these objects to be created each time i need an I/O operation or just invoke a read or a write on them each time i need??? ( plus some flushing after each operaration) 回答1: You must create these objects only once, that is, after your socket has been initialized. 回答2: Both variants are possible,

Java: InputStreams and OutputStreams being shared

落花浮王杯 提交于 2019-12-11 04:18:47
问题 Can I share an InputStream or OutputStream ? For example, let's say I first have: DataInputStream incoming = new DataInputStream(socket.getInputStream())); ... incoming being an object variable. Later on I temporarily do: BufferedReader dataReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); I understand that the stream is concrete and reading from it will consume its input, no matter from where it's done... But after doing the above, can I still access both incoming

Is there a class that exposes an unbuffered readLine method in Java?

两盒软妹~` 提交于 2019-12-10 13:31:54
问题 I'm cleaning up some chunks of our codebase at work, and one of the older classes is used to read and write data. This data is a mixture of US-ASCII encoded Strings and binary encoded primitives. The current implementation uses DataInputStream, but as you can see in the documentation the readLine() method is deprecated because of an issue related to converting bytes to characters. While this encoding issue hasn't really popped up for us, the deprecation is an issue since it already doesn't

Swift converting a byte array to integers

南笙酒味 提交于 2019-12-10 10:37:58
问题 Hello I am new to swift and would like to convert a byte array to several integers. I have written working code in Java but I am not quite sure how to take it to swift byte[] codeData = Base64.decode(codeDataBase64, 0); ByteArrayInputStream bais = new ByteArrayInputStream(codeData); DataInputStream dis = new DataInputStream(bais); byte version = dis.readByte(); if (version == 1) { int anID = dis.readInt(); int anotherID = dis.readInt(); byte[] TK = new byte[512]; int readTK = dis.read(TK); if

Reading a .dat file into an array in Java

徘徊边缘 提交于 2019-12-08 08:15:02
问题 The code that I'm writing has two classes: writeInts and readInts. I wrote writeInts to randomly generate 100 numbers between 0 and 1000 and output them to a data.dat file. readInts is supposed to open a DataInputStream object and read in the "raw" data from the data.dat file and store the 100 integers in an array. My problem is that I can't seem to read the data correctly. Any help with this would be greatly appreciated. Thanks! writeInts: import java.io.*; public class WriteInts { public

Java: Prevent Socket DataInputStream from throwing EOFException

老子叫甜甜 提交于 2019-12-06 20:08:33
My client/server application currently keeps opening and closing new connections every time it wants to send/receive data. I'm trying to change it so it will have one persistent connection. The problem I'm having is the socket's DataInputStream on the server keeps throwing EOFException's when I just want it to block until it receives the next batch of data. I thought about just simply writing the server like this... while socket is open { while at socket's DataInputStream's EOF { wait a second } //If we're here, then we have some data do stuff } ... but this is extremely ugly and not the

sending a file using DataOutputStream in java

痴心易碎 提交于 2019-12-06 12:02:49
问题 I am trying to build a client that sends the file size and contents to server. I am trying to use DataOutputStream. I am assuming that I need to open the file and and get size of file and read the contents and send it. But I am not sure how to implement those because I am really new to java... Can anyone help me about this? Thank you! 回答1: It's quite simple, but the code is a bit long to write it all and sounds like homework. I can give you some indications. Just open the file, use the long

Unknown buffer size to be read from a DataInputStream in java

半城伤御伤魂 提交于 2019-12-05 07:40:54
I have the following statement: DataInputStream is = new DataInputStream(process.getInputStream()); I would like to print the contents of this input stream but I dont know the size of this stream. How should I read this stream and print it? It is common to all Streams, that the length is not known in advance. Using a standard InputStream the usual solution is to simply call read until -1 is returned. But I assume, that you have wrapped a standard InputStream with a DataInputStream for a good reason: To parse binary data. (Note: Scanner is for textual data only.) The JavaDoc for DataInputStream