1
votes

When writing byte array of file getting excetion like java.net.SocketException: Software caused connection abort: socket write error java.lang.ArrayIndexOutOfBoundsException: 512

returnValue=1;
File file = new File(filename);
    fileLength = file.length();

connect(servername);

private int connect(String host) throws IOException {
    int response = 0;
    if (socket == null) {
        InetAddress ipAddress = InetAddress.getByName(host);
        socket = new Socket(ipAddress, port);
        output = new DataOutputStream(socket.getOutputStream());
        input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    }
    return response;
}

byte[] b = convert();

private byte[] convert() throws IOException {
    File file = new File(filename);
    try (InputStream is = new FileInputStream(file)) {
        long alength = 0;
        alength = file.length();
        bytes = new byte[(int) alength];
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }

    } catch (IOException io) {
        return new byte[0];
    }
    return bytes;
}

// Writing Byte Array

output.write(b,0,4);
  output.write(b, 4, ( (int) fileLength)); //exactly here i am facing error  
1
Very stupid question: How large is the file? Could it be too large, resulting in a cut when casting alength to int? - TreffnonX
Where in your code is the exception being thrown? - TreffnonX
after output.write(b,0,4); in catch block. - WASEEM
Might this line be executed?: return new byte[0]; That would result in an empty array, and you would not be able to write anythign (even just those few characters). - TreffnonX
without casting to int how can i handle. byte array will accept only int types - WASEEM

1 Answers

0
votes

Try this:

returnValue=1;
File file = new File(filename);
    fileLength = file.length();

connect(servername);

private int connect(String host) throws IOException {
    int response = 0;
    if (socket == null) {
        InetAddress ipAddress = InetAddress.getByName(host);
        socket = new Socket(ipAddress, port);
        output = new DataOutputStream(socket.getOutputStream());
        input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    }
    return response;
}

byte[] b = convert();

private byte[] convert() throws IOException {
    File file = new File(filename);
    try (InputStream is = new FileInputStream(file)) {
        long alength = 0;
        alength = file.length();
        bytes = new byte[(int) alength];
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }

    } catch (IOException io) {
        output.write(b,0,4);
        return new byte[0];
    }
    return bytes;
}

// Writing Byte Array
  output.write(b, 4, ( (int) fileLength)); //exactly here i am facing error