I'm building a swing interface in Java, I build a middleware which keeps reading the serial port and saving what's coming to String, here it's how I'm doing this:
public class RFID {
private static RFIDReader rReader;
private static boolean state;
public RFID(RFIDReader rReader) {
this.rReader = rReader;
this.state = true;
}
public void connect(String portName) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned()) {
System.out.println("Error: Port is currently in use");
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();
(new Thread(new SerialReader(in))).start();
//(new Thread(new SerialWriter(out))).start();
} else {
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}
public static class SerialReader implements Runnable {
InputStream in;
public SerialReader(InputStream in) {
this.in = in;
}
public void run() {
byte[] buffer = new byte[1024];
int len = -1;
String code;
try {
while (state == true && (len = this.in.read(buffer)) > -1) {
code = new String(buffer, 0, len);
if (code.length() > 1)
rReader.setCode(code);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void finish(){
state = false;
}
public static class SerialWriter implements Runnable {
OutputStream out;
public SerialWriter(OutputStream out) {
this.out = out;
}
public void run() {
try {
int c = 0;
while ((c = System.in.read()) > -1) {
this.out.write(c);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
So when I try to print what code is storing, it shows like this:
AC000F9
3
BB
What actually should look like this:
AC000F93BB
What am I doing wrong here ? This conversion from byte[] to String is not right ?
EDIT:
I need to read a string with 10 characters at total.
Don't use an InputStream to read from - use a Reader of some description. If you need to start with an InputStream, wrap it in an InputStreamReader. I would change your constructor to accept a Reader though, and allow clients to pass whatever reader they want in. That will make it easier to test (as you can just use a StringReader).
EDIT: Note - if you do need to create a reader from a binary InputStream, be explicit in your choice of Charset (encoding). So use the InputStreamReader constructor overload which specifies one. Even if you want to use the platform default, I'd be explicit about that to make it clear what you're doing.
EDIT: Beyond "where the string conversion happens" it's unclear what you're expecting to read. You're dealing with streaming APIs - what determines how much you really want to read before calling setCode? Is it a whole line? Is it some fixed number of characters? Is it characters before a particular delimiter?
EDIT: Now we know a bit more, I suggest you write a helper method like this:
// Assuming you now have a field of type Reader, called reader
private String readEntry() throws IOException {
char[] buffer = new char[10];
int index = 0;
while (index < buffer.length) {
int charsRead = reader.read(buffer, index, buffer.length - index);
if (charsRead == -1) {
throw new IOException("Couldn't read entry - end of data");
}
index += charsRead;
}
return new String(buffer);
}
Related
i am sending files from two java applications here the source code of the server and the client
public class FileClient {
private Socket s;
public FileClient(String host, int port, String file) {
try {
s = new Socket(host, port);
sendFile(file);
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendFile(String file) throws IOException {
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[4096];
while ((fis.read(buffer) > 0)) {
dos.write(buffer);
}
fis.close();
dos.close();
}
public static void main(String[] args) {
FileClient fc = new FileClient("192.168.0.167", 1988, "C:/Users/mhattabi/Desktop/fileData.txt");
}
}
and here the source code of the server
public class FileServer extends Thread {
private ServerSocket ss;
public FileServer(int port) {
try {
ss = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
try {
Socket clientSock = ss.accept();
saveFile(clientSock);
// ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void saveFile(Socket clientSock) throws IOException {
DataInputStream dis = new DataInputStream(clientSock.getInputStream());
FileOutputStream fos = new FileOutputStream("fileData.txt");
byte[] buffer = new byte[4096];
int filesize = 15123; // Send file size in separate msg
int read = 0;
int totalRead = 0;
int remaining = filesize;
while((read = dis.read(buffer)) > 0) {
totalRead += read;
System.out.println("read " + totalRead + " bytes.");
fos.write(buffer, 0, read);
}
fos.close();
dis.close();
}
public static void main(String[] args) {
FileServer fs = new FileServer(1988);
fs.start();
}
}
the problem that in the server i received the file but there is extra character in it look like this.Any help will be appreciated thanks
Usual problem.
while ((fis.read(buffer) > 0)) {
dos.write(buffer);
}
You're ignoring the count returned by the read, and assuming that it filled the buffer. It should be:
while ((count = fis.read(buffer) > 0)) {
dos.write(buffer, 0, count);
}
Curiously enough you have this right in the server. NB You don't need a DataOutputStream here.
byte[] buffer = new byte[4096];
I think the" extra character" come from this place;every time you write 4096 byte
,at last time it happened less than 4096 byte.and then you get extra character
I am starting with a response from a HTTP request:
InputStream responseInputStream = response.getEntityInputStream()
I need to gzip that response so I can upload it to s3 and save it compressed:
this.s3.putObject(new PutObjectRequest(bucketName, key, gzippedResponseInputStream, meta));
I am aware that I can get the byte[] array out of responseInputStream and then gzip them into a new InputStream. However, that can be very inefficient with a large amount of data.
I know that there have been similar questions asked on SO, but I have not found anything that seems to address the specific need of starting with an InputStream and finishing with a gzipped InputStream.
Thanks for any help!
I think you're looking for a PipedInputStream
Here's how it can be done.
public InputStrema getGZipStream() {
final PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream();
try (final InputStream responseInputStream = response.getEntityInputStream();
){
pis.connect(pos);
Thread thread = new Thread() {
public void run () {
startWriting(pos, responseInputStream);
}
};
thread.start();
} catch(Exception e) {
e.printStackTrace();
}
return pis;
}
public void startWriting(OutputStream out, InputStream in) {
try (GZIPOutputStream gOut = GZIPOutputStream(out);) {
byte[] buffer = new byte[10240];
int len = -1;
while ((len = in.read(buffer)) != -1) {
gOut.write(buffer, 0, len);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
out.close();
} catch( Exception e) {
e.printStackTrace();
}
}
}
I haven't tested this code, please let me know if this works.
public final class Example {
public static void main(String[] args) throws IOException, InterruptedException {
final PipedInputStream inputStream = new PipedInputStream();
final PipedOutputStream outputStream = new PipedOutputStream(inputStream);
Thread compressorThread = new Thread() {
#Override
public void run() {
try (FileInputStream dataSource = new FileInputStream(args[0])) {
try (GZIPOutputStream sink = new GZIPOutputStream(outputStream)) {
final byte[] buffer = new byte[8 * 1024];
for (int bytesRead = dataSource.read(buffer); bytesRead >= 0; bytesRead = dataSource.read(buffer)) {
sink.write(buffer, 0, bytesRead);
}
}
} catch (IOException ex) {
//TODO handle exception -> maybe use callable + executor
}
}
};
compressorThread.start();
try (FileOutputStream destination = new FileOutputStream(args[1])) {
final byte[] buffer = new byte[8 * 1024];
for (int bytesRead = inputStream.read(buffer); bytesRead >= 0; bytesRead = inputStream.read(buffer)) {
destination.write(buffer, 0, bytesRead);
}
}
compressorThread.join();
}
}
You are right, my previous example was wrong. You can use piped streams. The catch here is that you cannot use the input and output stream from the same thread. Also don't forget to join() on the writing thread. You can test my example by supplyng two parameters:
args[0] -> the source file
args[1] -> the destination to write the compressed content
PS: #11thdimension was a few minutes faster with his piped stream solutions, so if you find this helpful please accept his answer
I'm trying to post an InputStream to a RESTful service. For normal files, this is fine.
In another place I'm trying to write a number of files to a piped zip stream on the fly. To do this I have a class that extends InputStream and when read() is called it writes the next file to the pipe. When the first file has been written I call ZipOutputStream.closeEntry() but it hangs. Why??
When I test this class in a unit test it works fine. When I try to post this object it hangs. The debugger is telling me its waiting for lock on SocketWrapper.
Note, I also tried setting media type to application/octet-stream. Also, the RESTful method is never called.
The stream class...
static class MultiStreamZipInputStream extends InputStream {
private final Iterator<InputStream> streams;
private final byte[] buffer = new byte[4096];
private InputStream inputStream;
private ZipOutputStream zipOutputStream;
private InputStream currentStream;
private int counter = 0;
public MultiStreamZipInputStream(List<InputStream> streamList) {
streams = streamList.iterator();
currentStream = streams.next();
try {
PipedOutputStream out = new PipedOutputStream();
inputStream = new PipedInputStream(out);
zipOutputStream = new ZipOutputStream(out);
ZipEntry entry = new ZipEntry(String.valueOf(counter++)); // Use counter for random name
zipOutputStream.putNextEntry(entry);
} catch (IOException ex) {
ex.printStackTrace();
}
}
#Override
public int read()
throws IOException {
if (inputStream.available() != 0)
return inputStream.read();
if (currentStream == null)
return -1;
int bytesRead = currentStream.read(buffer);
if (bytesRead >= 0) {
zipOutputStream.write(buffer, 0, bytesRead);
zipOutputStream.flush();
} else {
currentStream.close();
zipOutputStream.closeEntry();
if (!streams.hasNext()) {
currentStream = null;
return -1;
}
currentStream = streams.next();
zipOutputStream.putNextEntry(new ZipEntry(String.valueOf(counter++)));
}
return read();
}
}
The posting code...
MultiStreamZipInputStream myStream = ...
Client client = Client.create();
Builder webResource = client.resource("some URL").type("application/x-zip-compressed");
webResource.post(ClientResponse.class, myStream);
The REST method on the other end...
#POST
#Path("/somemethod")
#Produces(MediaType.APPLICATION_JSON)
#Consumes({"application/x-zip-compressed"})
public Response someMethod(InputStream data) {...
hello I am using this method to read a message:
public String readMessage() {
int read = -1;
byte[] buffer = new byte[5*1024];
byte[] redData;
try {
while ((read = this.session.getInputStream().read(buffer)) > -1) {
redData = new byte[read];
System.arraycopy(buffer, 0, redData, 0, read);
return new String(redData,"UTF-8");
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
And when I write something like "hello how are you today?"
Response (Exact format, including these new lines):
[/127.0.0.1:54930]:
[/127.0.0.1:54930]: are
[/127.0.0.1:54930]: you
[/127.0.0.1:54930]: today?
Thats how I read chat messages, first I check which packet was requested, if the packet type was 0, then I get instance of packethandler, and pass the client object to the Chat handling packet which will read the message here, like this:
public void startClientService() throws IOException {
while(true) {
int packetType = this.in.read();
packets.getPacket(packetType);
}
}
public void getPacket(int packetType) {
switch (packetType) {
case 0:
chat.processPacket(this.client);
break;
}
}
And the chat packet:
#Override
public void processPacket(Session c) {
String clientMessage = c.readMessage();
System.out.println("[" + c.getStream().getRemoteSocketAddress() + "]: " + clientMessage.toString());
}
And there the print message happens.
Why does it print parts of the messages, in new lines? not even the full message.
This is my client:
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket = new Socket("127.0.0.1", 43594);
Scanner r = new Scanner(System.in);
PrintWriter out = new PrintWriter(socket.getOutputStream());
String input;
while(true) {
input = r.next();
if (input != null) {
sendMessage(input, out);
}
}
}
public static void sendMessage(String message, PrintWriter out) {
out.write(0);
out.flush();
out.write(message + "\n");
out.flush();
}
Thanks.
Session:
public class Session extends Thread implements Runnable {
private Socket session;
private Client client;
private PrintWriter out;
private BufferedReader in;
private PacketHandler packets;
public Session(Socket session) {
this.session = session;
this.client = new Client(this);
try {
this.setStream();
} catch (IOException e) {
e.printStackTrace();
}
this.packets = new PacketHandler(this);
System.out.println("[New session created]: " + session.getRemoteSocketAddress());
}
public void run() {
try {
this.startClientService();
} catch (IOException e) {
e.printStackTrace();
}
}
public Socket getStream() {
return this.session;
}
public void setStream() throws IOException {
this.out = new PrintWriter(this.session.getOutputStream());
this.in = new BufferedReader(new InputStreamReader(this.session.getInputStream()));
}
public Client getClient() {
return this.client;
}
public String readMessage() {
int read = -1;
byte[] buffer = new byte[5*1024];
byte[] redData;
try {
while ((read = this.session.getInputStream().read(buffer)) > -1) {
redData = new byte[read];
System.arraycopy(buffer, 0, redData, 0, read);
return new String(redData,"UTF-8");
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void startClientService() throws IOException {
while(true) {
int packetType = this.in.read();
packets.getPacket(packetType);
}
}
public void destruct() throws IOException {
this.session.close();
System.out.println("Session killed");
}
}
looks like you are returning as soon as you get some data from stream.
while ((read = this.session.getInputStream().read(buffer)) > -1) {
redData = new byte[read];
System.arraycopy(buffer, 0, redData, 0, read);
return new String(redData,"UTF-8");
}
Read the data completely and make a string object out of it and return it
BufferedReader br = new BufferedReader(new InputStreamReader(this.session.getInputStream()));
String msg = br.readLine();
br.close();
return msg;
try this way. This will give you entire data to a buffer and can return as line of string.No need of loop
The the amount of data returned from one call to read has no relationship to how the
data divided when sent. One send can result in any number of reads, and multiple sends
may be combined into one read.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Code adapted from http://rxtx.qbang.org/wiki/index.php/Event_Based_Two_Way_Communication
I'm trying to read to a common buffer with the SerialReader class and send that buffer out over the Serial via the SerialWriter class, however the buffer is showing as null every time Writer is called. The code is initialised using the connect method of TwoWaySerialCommTest(pasted below for reference)
public SerialWriter ( OutputStream out )
{
this.out = out;
}
public SerialWriter ( OutputStream out, byte[] buffer)
{
this.out = out;
this.buffer = buffer;
}
public void run ()
{
while(true)
{
lock.lock();
try
{
dataAvailable.await();
System.out.println("Waking up");
int i = 0;
if (this.buffer != null)
{
System.out.println("Buffer isn't empty");
while(buffer[i] != ((byte)'\n') && i < buffer.length - 1)
{
this.out.write(buffer[i]);
}
}
else
{
System.out.println("Buffer is null");
System.out.println(this.buffer.toString());
}
}
catch ( IOException e )
{
e.printStackTrace();
System.exit(-1);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
lock.unlock();
}
}
}
}
Serial Reader class
public static class SerialReader implements SerialPortEventListener
{
private InputStream in;
byte[] buffer;
public SerialReader ( InputStream in )
{
this.in = in;
}
public SerialReader (InputStream in, byte[] buffer)
{
this.in = in;
this.buffer = buffer;
}
public void serialEvent(SerialPortEvent arg0) {
lock.lock();
int data;
if (buffer != null)
{
for(int i = 0; i < buffer.length; i++)
{
if (buffer[i] != 0)
{
System.out.print((char)buffer[i]);
}
}
}
buffer = new byte[1024];
try
{
int len = 0;
while ( ( data = in.read()) > -1 )
{
if ( data == '\n' ) {
break;
}
buffer[len++] = (byte) data;
}
System.out.println(new String(buffer,0,len));
for(int i = 0; i < buffer.length; i++)
{
if (buffer[i] != 0)
{
System.out.print((char)buffer[i]);
}
}
System.out.println();
dataAvailable.signal();
}
catch ( IOException e )
{
e.printStackTrace();
System.exit(-1);
}
finally
{
lock.unlock();
}
}
}
TwoWaySerialCommTest (truncated)
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.locks.*;
/**
* This version of the TwoWaySerialComm example makes use of the
* SerialPortEventListener to avoid polling.
*
*/
public class TwoWaySerialCommTest
{
static Lock lock = new ReentrantLock();
static Condition dataAvailable = lock.newCondition();
public volatile byte[] buffer;
public TwoWaySerialCommTest()
{
super();
}
void connect ( String portName ) throws Exception
{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if ( portIdentifier.isCurrentlyOwned() )
{
System.out.println("Error: Port is currently in use");
}
else
{
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
if ( commPort instanceof SerialPort )
{
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();
(new Thread(new SerialWriter(out , buffer))).start();
serialPort.addEventListener(new SerialReader(in , buffer));
serialPort.notifyOnDataAvailable(true);
}
else
{
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}
One problem seems to be this line:
buffer = new byte[1024];
I don't think you want to be assigning the local buffer to a new byte array there. This causes the writer to write into its own array and not the one passed in that is shared by the reader. Assigning the buffer only affects the local variable inside of the writer.
Also, what stops the input stream from overwriting the buffer? If the line length is more that 1024 you are going to get an array out of bounds exception.
Lastly, you are not writing the \n character into the buffer. So the reader is going to print a number of \0 null characters to the output.