For a homework i try to send a file and some parameter corresponding to this file.
So, i send my file and after my string.
The problem is my paramater go to my file and not in my variable. I understand the problem, my loop while continue to write in the file as long as he receives something, but i want to stop it and have my parameter outside from my file.
Here my code client:
public static void transfert(InputStream in, OutputStream out) throws IOException{
PrintWriter printOut;
byte buf[] = new byte[1024];
int n;
while((n=in.read(buf))!=-1)
out.write(buf,0,n);
printOut = new PrintWriter(out);
printOut.println("add");
System.out.println("envoie !!!");
printOut.println("1");
printOut.println("3");
// out.write(getBytes("add"),0,0);
printOut.flush();
}
and here my server code :
public static void transfert(InputStream in, OutputStream out, boolean closeOnExit) throws IOException
{
byte buf[] = new byte[1024];
int n;
while((n=in.read(buf))!=-1)
out.write(buf,0,n);
buffIn = new BufferedReader (new InputStreamReader(in));
String nom_methode = buffIn.readLine();
String param1 = buffIn.readLine();
String param2 = buffIn.readLine();
System.out.println("methode:"+nom_methode+"param1:"+param1+"param2:"+param2);
if (closeOnExit)
{
in.close();
out.close();
}
}
edit:
I still miss something, now i have an error with my thread, i think the problem is from my loop wich read my file in input.
Moreover, actually param still go in my file and not in my param... Why the loop dont stop after EOF ?
error:
Exception in thread "Thread-0" java.lang.IndexOutOfBoundsException
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:318)
at serveurthread.AcceptClient.transfert(AcceptClient.java:45)
at serveurthread.AcceptClient.run(AcceptClient.java:84)
at java.lang.Thread.run(Thread.java:722)
client:
public static void transfert(InputStream in, OutputStream out) throws IOException{
PrintWriter printOut;
printOut = new PrintWriter(out);
byte buf[] = new byte[1024];
int n;
while((n=in.read(buf))!=-1)
out.write(buf,0,n);
printOut.print('\u0004');
printOut.flush();
printOut.println("add");
System.out.println("envoie !!!");
printOut.println("1");
printOut.println("3");
// out.write(getBytes("add"),0,0);
printOut.flush();
}
server:
public static void transfert(InputStream in, OutputStream out, boolean closeOnExit) throws IOException
{
byte buf[] = new byte[1024];
int n;
while((n=in.read(buf))!= (int)'\u0004'){
out.write(buf,0,n);
}
buffIn = new BufferedReader (new InputStreamReader(in));
String nom_methode = buffIn.readLine();
String param1 = buffIn.readLine();
String param2 = buffIn.readLine();
System.out.println("methode:"+nom_methode+"param1:"+param1+"param2:"+param2);
if (closeOnExit)
{
in.close();
out.close();
}
}
You are going to need some kind of marker in your stream that signifies the end of the file. I suggest '\u0004' which is the EOF character. Basically, write this character after writing the file, then adjust the loop in the server like this:
while((n=in.read(buf))!=(int)'\u0004')
out.write(buf,0,n);
Now, the server stops reading the file when necessary, then can get around to reading the string.
Also, read returns a count of bytes read, not a char. I would pass one byte at a time through, by declaring bytes instead of byte arrays.
Related
I have the following code for upload file from client to server tcp but when i try to open manually the file is empty why the good weight..
I have look lot of post on stackOverflow but nothing make change
Thx
(Sorry for my bad english)
Server:
public class ThreadServer extends Thread{
private Socket soc;
private FileOutputStream fos;
private BufferedOutputStream bos;
private InputStream in;
public ThreadServer (Socket soc) {
this.soc = soc;
}
public void run(){
try {
fos = new FileOutputStream("C:/Users/erwan/workspace/Word/server/text.txt");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
bos = new BufferedOutputStream(fos);
byte[] buffer = new byte[1024];
try {
in = soc.getInputStream();
int count = 0;
while((count= in.read(buffer, 0 , buffer.length)) != -1) {
System.out.println(count+" octets received...");
bos.write(buffer);
}
bos.flush();
bos.close();
in.close();
soc.close();
System.out.println("File sent succesfully!");
}catch(IOException e){
e.printStackTrace();
System.out.println("Une erreur est survenu");
}
}
}
client:
public class Client {
private static Socket as;
private static FileInputStream fis;
private static BufferedInputStream bis;
private static OutputStream out;
public static void main( String[] args ){
as = null;
try{
as = new Socket(InetAddress.getLocalHost(),4020);
File f = new File (args[0]);
byte [] buffer = new byte [(int) f.length()];
fis = new FileInputStream(f);
setBis(new BufferedInputStream(fis));
out = as.getOutputStream();
System.out.println("uploading...");
out.write(buffer,0,buffer.length);
out.flush();
out.close();
System.out.println("the file is uploaded.");
as.close();
}catch(IOException e){
e.printStackTrace();
}
}
The buffer in client does not seem to be populated with data. It is initialized as an array of bytes with the length of the file, but there are no read method calls done on the input stream. For the purpose of testing a fis.read(buffer) would probably quickly get some data into the buffer. Keep in mind that reads are not guaranteed to fill the whole length of the buffer. So especially if your file contains zeros then the lack of reading actual data into the buffer (of the client) is the likely culprit.
Other than that the server code also assumes that the read method fully populates the buffer, so the write method call should specify a length (count). So change bos.write(buffer) into bos.write(bos, 0, count). This will probably become apparent at the end of the file (if the file is more than 1024 bytes long), as the end of the file would become a repetition of some of the data from the previous chunk.
sldls slfjksdl slfjdsl
ldsfj, jsldjf lsdjfk
Those string lines are from a file called "input".
How to ouput those string lines in reverse order to a file called "ouput" by using input, output stream and recursion in Java?
I am not going to put the entire code here but I will put segments from each key areas and hopefully you will figure out to put everything together.
Here is how you should reverse the given string
public static String reverseString(InputStream is) throws IOException {
int length = is.available();
byte[] bytes = new byte[length];
int ch = -1;
while ((ch = is.read()) != -1) {
bytes[--length] = (byte) ch;
}
return new String(bytes);
}
This is how your main method should look like calling the above function.
InputStream is = new FileInputStream(f);
String reversedString = reverseString(is);
And finally hopefully you fill figure out how to write to a file by playing around with this.
try{
// Create file
FileWriter fstream = new FileWriter("/Users/anu/GroupLensResearch/QandA/YahooData/L16/out.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write(reverseRead(is));
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
This isn't exactly an example of Java best practices, but it works and should be enough to get you started
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException, IOException {
Scanner scanner = new Scanner(new File("infile.txt"), "UTF-8");
FileOutputStream fileOutputStream = new FileOutputStream("outfile.txt");
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
recurse(scanner, outputStreamWriter);
outputStreamWriter.close();
}
static void recurse(Scanner scanner, OutputStreamWriter outputStreamWriter) throws IOException {
String line = scanner.nextLine();
if (scanner.hasNext())
recurse(scanner, outputStreamWriter);
outputStreamWriter.write(line + "\n");
}
The second encoding argument to Scanner and OutputStreamWriter can be dropped if you're using your system's default encoding.
I'm trying to find a way to send files of different file types from a server to a client.
I have this code on the server to put the file into a byte array:
File file = new File(resourceLocation);
byte[] b = new byte[(int) file.length()];
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(file);
try {
fileInputStream.read(b);
} catch (IOException ex) {
System.out.println("Error, Can't read from file");
}
for (int i = 0; i < b.length; i++) {
fileData += (char)b[i];
}
}
catch (FileNotFoundException e) {
System.out.println("Error, File Not Found.");
}
I then send fileData as a string to the client. This works fine for txt files but when it comes to images I find that although it creates the file fine with the data in, the image won't open.
I'm not sure if I'm even going about this the right way.
Thanks for the help.
Don't put it into a string with a char cast. Just have your socket write the byte array you get from the file input stream.
If you're reading/writing binary data you should use byte streams (InputStream/OutputStream) instead of character streams and try to avoid conversions between bytes and chars like you did in your example.
You can use the following class to copy bytes from an InputStream to an OutputStream:
public class IoUtil {
private static final int bufferSize = 8192;
public static void copy(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[bufferSize];
int read;
while ((read = in.read(buffer, 0, bufferSize)) != -1) {
out.write(buffer, 0, read);
}
}
}
You don't give too much details of how you connect with the client. This is a minimal example showing how to stream some bytes to the client of a servlet. (You'll need to set some headers in the response and release the resources appropiately).
public class FileServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Some code before
FileInputStream in = new FileInputStream(resourceLocation);
ServletOutputStream out = response.getOutputStream();
IoUtil.copy(in, out);
// Some code after
}
}
I have a TCP socket client receiving messages (data) from a server.
messages are of the type length (2 bytes) + data (length bytes), delimited by STX & ETX characters.
I'm using a bufferedReader to retrieve the two first bytes, decode the length, then read again from the same bufferedReader the appropriate length and put the result in a char array.
most of the time, I have no problem, but SOMETIMES (1 out of thousands of messages received), when attempting to read (length) bytes from the reader, I get only part of it, the rest of my array being filled with "NUL" characters. I imagine it's because the buffer has not yet been filled.
char[] bufLen = new char[2];
_bufferedReader.read(bufLen);
int len = decodeLength(bufLen);
char[] _rawMsg = new char[len];
_bufferedReader.read(_rawMsg);
return _rawMsg;
I solved the problem in several iterative ways:
first I tested the last char of my array: if it wasn't ETX I would read chars from the bufferedReader one by one until I would reach ETX, then start over my regular routine. the
consequence is that I would basically DROP one message.
then, in order to still retrieve that message, I would find the first occurence of the NUL char in my "truncated" message, read & store additional characters one at a time until I reached ETX, and append them to my "truncated" messages, confirming length is ok.
it works also, but I'm really thinking there's something I could do better, like checking if the total number of characters I need are available in the buffer before reading it, but can't find the right way to do it...
any idea / pointer ?
thanks !
The InputStream read method may return short reads; you must check the return value to determine how many characters were read, and continue reading in a loop until you get the number you wanted. The method may block, but it only blocks until some data is available, not necessarily all the data you requested.
Most people end up writing a "readFully" method, like DataInputStream, which reads the amount of data expected, or throws an IOException:
static public int readFully(InputStream inp, byte[] arr, int ofs, int len) throws IOException {
int rmn,cnt;
for(rmn=len; rmn>0; ofs+=cnt, rmn-=cnt) {
if((cnt=inp.read(arr,ofs,rmn))==-1) {
throw new IOException("End of stream encountered before reading at least "+len+" bytes from input stream");
}
}
return len;
}
Here is a sample server that I have used for testing
The main rcv is structured like
while((chars_read = from_server.read(buffer)) != -1)
{
to_user.write(buffer,0,chars_read);
to_user.flush();
}
The actual whole server is below ...
public static void main(String[] args) throws IOException
{
try
{
if (args.length != 2)
throw new IllegalArgumentException("Wrong number of Args");
String host = args[0];
int port = Integer.parseInt(args[1]);
Socket s = new Socket(host,port);
final Reader from_server = new InputStreamReader(s.getInputStream());
PrintWriter to_server = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader from_user = new BufferedReader(new InputStreamReader(System.in));
final PrintWriter to_user = new PrintWriter(new OutputStreamWriter(System.out));
to_user.println("Connected to " + s.getInetAddress() + ":" + s.getPort());
Thread t = new Thread()
{
public void run()
{
char [] buffer = new char[1024];
int chars_read;
try
{
while((chars_read = from_server.read(buffer)) != -1)
{
to_user.write(buffer,0,chars_read);
to_user.flush();
}
}
catch(IOException e)
{
to_user.println(e);
}
to_user.println("Connection closed by server");
to_user.flush();
System.exit(0);
}
};
t.setPriority(Thread.currentThread().getPriority() + 1);
t.start();
String line;
while ((line = from_user.readLine()) != null)
{
to_server.println(line);
to_server.flush();
}
//t.stop();
s.close();
to_user.println("Connection closed by client");
to_user.flush();
}
catch(Throwable e)
{
e.printStackTrace();
System.err.println("Usage : java TCPClient <hostname> <port>");
}
}
Is there a way to read a ByteBuffer with a BufferedReader without having to turn it into a String first? I want to read through a fairly large ByteBuffer as lines of text and for performance reasons I want to avoid writing it to the disk. Calling toString on the ByteBuffer doesn't work because the resulting String is too large (it throws java.lang.OutOfMemoryError: Java heap space). I would have thought there would be something in the API to wrap a ByteBuffer in a suitable reader, but I can't seem to find anything suitable.
Here's an abbreviated code sample the illustrates what I am doing):
// input stream is from Process getInputStream()
public String read(InputStream istream)
{
ReadableByteChannel source = Channels.newChannel(istream);
ByteArrayOutputStream ostream = new ByteArrayOutputStream(bufferSize);
WritableByteChannel destination = Channels.newChannel(ostream);
ByteBuffer buffer = ByteBuffer.allocateDirect(writeBufferSize);
while (source.read(buffer) != -1)
{
buffer.flip();
while (buffer.hasRemaining())
{
destination.write(buffer);
}
buffer.clear();
}
// this data can be up to 150 MB.. won't fit in a String.
result = ostream.toString();
source.close();
destination.close();
return result;
}
// after the process is run, we call this method with the String
public void readLines(String text)
{
BufferedReader reader = new BufferedReader(new StringReader(text));
String line;
while ((line = reader.readLine()) != null)
{
// do stuff with line
}
}
It's not clear why you're using a byte buffer to start with. If you've got an InputStream and you want to read lines for it, why don't you just use an InputStreamReader wrapped in a BufferedReader? What's the benefit in getting NIO involved?
Calling toString() on a ByteArrayOutputStream sounds like a bad idea to me even if you had the space for it: better to get it as a byte array and wrap it in a ByteArrayInputStream and then an InputStreamReader, if you really have to have a ByteArrayOutputStream. If you really want to call toString(), at least use the overload which takes the name of the character encoding to use - otherwise it'll use the system default, which probably isn't what you want.
EDIT: Okay, so you really want to use NIO. You're still writing to a ByteArrayOutputStream eventually, so you'll end up with a BAOS with the data in it. If you want to avoid making a copy of that data, you'll need to derive from ByteArrayOutputStream, for instance like this:
public class ReadableByteArrayOutputStream extends ByteArrayOutputStream
{
/**
* Converts the data in the current stream into a ByteArrayInputStream.
* The resulting stream wraps the existing byte array directly;
* further writes to this output stream will result in unpredictable
* behavior.
*/
public InputStream toInputStream()
{
return new ByteArrayInputStream(array, 0, count);
}
}
Then you can create the input stream, wrap it in an InputStreamReader, wrap that in a BufferedReader, and you're away.
You can use NIO, but there's no real need here. As Jon Skeet suggested:
public byte[] read(InputStream istream)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024]; // Experiment with this value
int bytesRead;
while ((bytesRead = istream.read(buffer)) != -1)
{
baos.write(buffer, 0, bytesRead);
}
return baos.toByteArray();
}
// after the process is run, we call this method with the String
public void readLines(byte[] data)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(data)));
String line;
while ((line = reader.readLine()) != null)
{
// do stuff with line
}
}
This is a sample:
public class ByteBufferBackedInputStream extends InputStream {
ByteBuffer buf;
public ByteBufferBackedInputStream(ByteBuffer buf) {
this.buf = buf;
}
public synchronized int read() throws IOException {
if (!buf.hasRemaining()) {
return -1;
}
return buf.get() & 0xFF;
}
#Override
public int available() throws IOException {
return buf.remaining();
}
public synchronized int read(byte[] bytes, int off, int len) throws IOException {
if (!buf.hasRemaining()) {
return -1;
}
len = Math.min(len, buf.remaining());
buf.get(bytes, off, len);
return len;
}
}
And you can use it like this:
String text = "this is text"; // It can be Unicode text
ByteBuffer buffer = ByteBuffer.wrap(text.getBytes("UTF-8"));
InputStream is = new ByteBufferBackedInputStream(buffer);
InputStreamReader r = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(r);