Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I was making a file-transfer program and it takes forever to transmit this 800kb test picture I made. Clearly I don't want to spend 30 minutes for every mb of data, so I was curious if there was any way to speed up my code or Java in general. I had heard about allocating extra RAM, but never looked into it.
If anyone can find a better algorithm for my code that would be appreciated, otherwise I may have to step to C++ or C and try again.
Server code block:
if(file.exists()){
for(long l=0;l<file.length();l++){
out.writeByte(fIS.read());
}
}
where fIS is the FileInputStream and out is the outgoing DataOutputStream. The rest of the code is basically sending lengths and a flag, but I can add if requested.
Client Code:
b = new byte[len];
for(int x=0;x<len;x++){
b[x] = dIS.readByte();
}
fOS.write(b);
where len is the read length of the file expected from the Server, b is the array of bytes that make up the file, dIS is the input stream from the server, and fOS is the FileOutputStream.
I also just attempted using Readers and readLine() with writeChars() and readChar(), but this seems to equate to about the same thing.
Reading a file a byte at a time and sending over the network one byte at a time is going to be slow in any language :)
Consider using a BufferedInputStream for reading and BufferedOutputStream for writing.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I was trying to convert a ".jpg" image to binary and then change its binary value to hide some data. But couldn't find anything. Any ideas anyone?
If I understand the question correctly, you want to get the single bytes of the jpg-file, which can be read with a DataInputStream:
File imageFile;
DataInputStream dis = new DataInputStream(new FileInputStream(imageFile));
int input = dis.read();
dis.close();
input then holds the first byte of the file, if you invoke read again (before dis.close()), you can read the subsequent bytes. Next, you would have to manipulate them and finally, you can write them to this or another file with a DataOutputStream that works just like the corresponding input stream. Just do NOT forget to close the streams after you are done reading or writing, so that system resources are freed and the files are closed. Otherwise the written data could be lost.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I realize that writes are slower in general. But I want to know by how much.
I have some code, in server class, that writes to a log file:
if (inputLine.equals("y")){
System.out.println("\nThe input was Yes!\n");
newPosition++;
logPosition();
}
The problem is that, on the other side in my client class I am running through an array pretty quickly. It is a regular for-loop :
for (int i=0; i<KKJokes.length; i++) {
out.println(KKJokes[i%KKJokes.length]);
}
Variable out is for the socket (out = new PrintWriter(kkSocket.getOutputStream(), true);).
I want to know a way to slow down the Client sending to the server (via array). So that it can only start outputting again once we finished writing to the log file.
Also if anyone wants to see, full Client code & full Server code.
You can't rely on timing for this. You will have to have the server send an acknowledgement of every message, which the client must read before it sends again.
Normally the server would accept in a loop and let the the socket processing be handled in another thread from a thread pool. Then it could "throttle" accepting (=wait a bit when very busy) when to many requests come in.
Also use a real logger. Besides more functionality like rotating logs, they are built for speed too.
Furthermore use a specified character encoding, so not to be surprised if client and server operate with another encoding:
new InputStreamReader(inputStream, "UTF-8")
Consider using the Java's Thread.Sleep() function to temporarily pause execution of the running thread. So your code might look something like this,
for (int i = 0; i < KKJokes.length; i++ ) {
out.println(KKJokes[ i % KKJokes.length ]);
Thread.sleep(2000); // Time in milliseconds
}
See the docs for more details.
Hope this helps.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to read the content of a large file. For that I Googled on it and found so many methods and resources. But I'm still confused which is the method to read the large files (Factors need to be consider in my case are memory allocation, performance, large file )
Using FileChannel
using Files.readAllLines
using BufferedReader
Can anyone guide on this?
Your best option is to read the file lazily. Fetch each line one at a time and process.
Example:-
Stream<String> lines = Files.lines(Paths.get("C:/files", "yourfile.txt"));
Then process the lines afterwords.
From the official documentation:-
public static Stream<String> lines(Path path, Charset cs) throws IOException
Read all lines from a file as a Stream. Unlike readAllLines, this
method does not read all lines into a List, but instead populates
lazily as the stream is consumed.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I always get confused when to process my input data how, which process. Different times i find different solutions. I am also not clear about their Hierarchy.
InputStream is parent class of all input streams and readers. Classes that have Stream keyword will work with bytes whereas classes which have Reader keyword will work with characters.
Buffer is wrapper around these streams to decrease the system calls and increase performance and speed of reading. Non buffered streams return single byte each time whereas Bufferd stream will not return until the buffer gets full. For example if you take BufferedReader you can read a whole line using readLine() but in non buffered stream you must read single character using read() method.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I learned recently that the standard basic networking in Java goes like this:
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
I just want to clarify, so for the output we don't use a BufferedWriter because that's the job on the server's side? Would it be wrong to do something like this:
out = new BufferedWriter(PrintWriter(echoSocket.getOutputStream(), true));
From the Java documentation:
http://docs.oracle.com/javase/tutorial/essential/io/buffers.html
Most of the examples we've seen so far use unbuffered I/O. This means each read or write request is handled directly by the
underlying OS. This can make a program much less efficient, since each
such request often triggers disk access, network activity, or some
other operation that is relatively expensive.
To reduce this kind of overhead, the Java platform implements buffered I/O streams. Buffered input streams read data from a memory
area known as a buffer; the native input API is called only when the
buffer is empty. Similarly, buffered output streams write data to a
buffer, and the native output API is called only when the buffer is
full.
Yes, "buffered" input AND output is usually a Good Idea.
IMHO...
PS:
I don't see anything wrong with PrintWriter. Especially if I wanted to do "printf()" style text I/O directly to the socket.
I wouldn't call that example "standard". It's a simple tutorial. It uses BufferedReader not for buffering, but for the BufferedReader.readLine() method.
For a serious application, yes, the output should be buffered. You should not write many pieces of small data. If nothing else, the overhead of system call for each write() is a killer.