Click here to Skip to main content
15,886,873 members
Home / Discussions / Java
   

Java

 
AnswerRe: How to convert string to double with trailing zeros after decimal Pin
Dave Kreskowiak8-Nov-23 2:23
mveDave Kreskowiak8-Nov-23 2:23 
QuestionHow to use JNI without setting Environment Variables Pin
Valentinor16-Oct-23 0:54
Valentinor16-Oct-23 0:54 
AnswerRe: How to use JNI without setting Environment Variables Pin
Valentinor16-Oct-23 1:42
Valentinor16-Oct-23 1:42 
AnswerRe: How to use JNI without setting Environment Variables Pin
jschell16-Oct-23 4:58
jschell16-Oct-23 4:58 
GeneralRe: How to use JNI without setting Environment Variables Pin
Valentinor16-Oct-23 5:41
Valentinor16-Oct-23 5:41 
GeneralRe: How to use JNI without setting Environment Variables Pin
jschell17-Oct-23 5:47
jschell17-Oct-23 5:47 
GeneralRe: How to use JNI without setting Environment Variables Pin
Valentinor17-Oct-23 19:26
Valentinor17-Oct-23 19:26 
QuestionSending file though socket as byte[] Pin
JohnCodding25-Sep-23 20:52
JohnCodding25-Sep-23 20:52 
I have a socket though which I'm sending files. If the file is under the buffer size (8192 in my case) then it is fine, but if the file is over that size, then when it writes to file, it writes the first chunk of 8192 bites every time. I'm using ObjectStream because beside the file, I'm sending other objects as well in the actual app, but this is the part of sending the file.

Client it sends the file data (testing):
Java
System.out.println("Starting client");
Socket socket = new Socket("localhost", port);
System.out.println("Client sending data");
ObjectOutputStream dataOut = new ObjectOutputStream(socket.getOutputStream());
BufferedInputStream is = new BufferedInputStream(new FileInputStream(inFile));
byte[] buffer = new byte[8192];
int sizeRead = 0;
while ((sizeRead = is.read(buffer, 0, 8192)) >= 0) {
    printByte(buffer, "in"); //Function that writes to a file as strings the byte[], and after 8192 you can see it is the first byte[] each time, also it writes at the end of file
    dataOut.writeObject(true);
    dataOut.writeObject(buffer);
    dataOut.writeObject(sizeRead);
    dataOut.flush();
}
is.close();
System.out.println("Done sending file");
dataOut.writeObject(false);
socket.close();
System.out.println("Client closed");

Server that creates the file:
Java
System.out.println("Starting server");
ServerSocket server = new ServerSocket(port);
System.out.println("Server waiting");
Socket socket = server.accept();
System.out.println("Client connected");
ObjectInputStream dataIn = new ObjectInputStream(socket.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFile));
Object auxKeepReading = dataIn.readObject();
if (auxKeepReading instanceof Boolean) {
    while ((boolean) auxKeepReading) {
        Object auxBuffer = dataIn.readObject();
        if (!(auxBuffer instanceof byte[])) {
            System.out.println("Broke byte");
            break;
        }
        Object auxSize = dataIn.readObject();
        if (!(auxSize instanceof Integer)) {
            System.out.println("Broke int");
            break;
        }
        printByte((byte[]) auxBuffer, "out"); //Same as on client but different file, it writes at the end of file each time
        bos.write((byte[]) auxBuffer, 0, (int) auxSize);

        auxKeepReading = dataIn.readObject();
        if (!(auxKeepReading instanceof Boolean)) {
            System.out.println("Broke boolean");
            break;
        }
    }
} else {
    System.out.println("Broke boolean");
}
bos.close();
socket.close();
server.close();
System.out.println("Server stopped");

Why is it that it keeps sending the first chunk of byte[], and not what it reads new?
GeneralRe: Sending file though socket as byte[] Pin
JohnCodding25-Sep-23 21:33
JohnCodding25-Sep-23 21:33 
GeneralRe: Sending file though socket as byte[] Pin
Richard MacCutchan25-Sep-23 22:32
mveRichard MacCutchan25-Sep-23 22:32 
GeneralRe: Sending file though socket as byte[] Pin
JohnCodding25-Sep-23 23:44
JohnCodding25-Sep-23 23:44 
GeneralRe: Sending file though socket as byte[] Pin
Richard MacCutchan25-Sep-23 23:58
mveRichard MacCutchan25-Sep-23 23:58 
GeneralRe: Sending file though socket as byte[] Pin
JohnCodding26-Sep-23 0:53
JohnCodding26-Sep-23 0:53 
GeneralRe: Sending file though socket as byte[] Pin
jschell3-Oct-23 4:42
jschell3-Oct-23 4:42 
GeneralRe: Sending file though socket as byte[] Pin
JohnCodding12-Oct-23 23:35
JohnCodding12-Oct-23 23:35 
GeneralRe: Sending file though socket as byte[] Pin
jschell13-Oct-23 7:19
jschell13-Oct-23 7:19 
Question[Solved] Getting and setting a File's all attributes Pin
Valentinor24-Sep-23 22:47
Valentinor24-Sep-23 22:47 
AnswerRe: Getting and setting a File's all attributes Pin
Victor Nijegorodov24-Sep-23 23:04
Victor Nijegorodov24-Sep-23 23:04 
GeneralRe: Getting and setting a File's all attributes Pin
Valentinor24-Sep-23 23:17
Valentinor24-Sep-23 23:17 
Answer[Solution] Re: Getting and setting a File's all attributes Pin
Valentinor25-Sep-23 6:26
Valentinor25-Sep-23 6:26 
QuestionGetting java.lang.RuntimeException: ZeroCode Step execution failed. Details:java.lang.NullP Pin
Member 160852744-Sep-23 5:46
Member 160852744-Sep-23 5:46 
AnswerRe: Getting java.lang.RuntimeException: ZeroCode Step execution failed. Details:java.lang.NullP Pin
Richard Deeming4-Sep-23 21:21
mveRichard Deeming4-Sep-23 21:21 
AnswerRe: Getting java.lang.RuntimeException: ZeroCode Step execution failed. Details:java.lang.NullP Pin
Member 160852744-Sep-23 23:55
Member 160852744-Sep-23 23:55 
GeneralRe: Getting java.lang.RuntimeException: ZeroCode Step execution failed. Details:java.lang.NullP Pin
Member 160852744-Sep-23 23:58
Member 160852744-Sep-23 23:58 
GeneralRe: Getting java.lang.RuntimeException: ZeroCode Step execution failed. Details:java.lang.NullP Pin
Member 160852745-Sep-23 0:09
Member 160852745-Sep-23 0:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.