# Kryonet



## Lurxl (18. Sep 2021)

Hey Leute,
[CODE lang="java" title="Send"]JFileChooser chooser = new JFileChooser();
                chooser.showOpenDialog(null);
                File file = new File(chooser.getSelectedFile().getAbsolutePath());

                    byte[] fileContent = null;

                    try {
                        fileContent = Files.readAllBytes(file.toPath());

                        int lenght = fileContent.length;
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        int currentByte = 0;
                        while(lenght > 4096) {
                            bos.write(fileContent, currentByte, 4096);
                            FileM m = new FileM(bos.toByteArray(), true);
    ClientHandler.sendFile(m);
                            currentByte += 4096;
                            lenght -= 4096;
                            bos.reset();
                            System.out.println(lenght);
                        }
                        if(lenght > 0) {
                            bos.write(fileContent, currentByte, lenght);
                            FileM m = new FileM(bos.toByteArray(), true);

                                ClientHandler.sendFile(m);

                        }
                        FileM m = new FileM(file.getName(), false);
                            ClientHandler.sendFile(m);

                        bos.reset();
                        bos.close();
                        fileContent = null;
                    } catch (IOException e1) {

                        e1.printStackTrace();
                    }[/CODE]
[CODE lang="java" title="FileM"]public class FileM {
byte[] bytes;
String FileName;
boolean isWriting;
    public FileM() {}
    public FileM(byte[] bytes,boolean isWriting) {
        this.isWriting = isWriting;
        this.bytes = bytes;
    }public FileM(String FileName,boolean isWriting) {
        this.isWriting = isWriting;
        this.FileName = FileName;
    }
    public boolean isWriting() {
        return isWriting;
    }
    public void setWriting(boolean isWriting) {
        this.isWriting = isWriting;
    }
    public String getEndung() {
        return FileName;
    }
    public void setEndung(String FileName) {
        this.FileName = FileName;
    }
    public byte[] getBytes() {
        return bytes;
    }



}[/CODE]
[CODE lang="java" title="received"]if(o instanceof FileM) {
            FileM f = (FileM)o;
            if(f.isWriting()) {
                try {
                    FileOutputStream out = new FileOutputStream("C:\\Users\\"+user+"\\AppData\\Local\\Temp\\SendingFile.txt",true);
                    out.write(f.getBytes());
                    out.flush();
                    out.close();
                } catch (FileNotFoundException e) {

                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }else if(!f.isWriting()) {
                String name = f.getEndung();
                File file = new File("C:\\Users\\"+user+"\\AppData\\Local\\Temp\\SendingFile.txt");
                int i = JOptionPane.showOptionDialog(null, "Möchtest du die datei "+ name +"\nGröße:"+file.length() / 1024+"kb", "File", JOptionPane.YES_NO_OPTION,  JOptionPane.INFORMATION_MESSAGE,null,null, null);
                if(i == JOptionPane.YES_OPTION) {
                    file.renameTo(new File("C:\\Users\\"+user+"\\Downloads\\"+name));
                }else if(i == JOptionPane.NO_OPTION) {
                    file.delete();
                }


            }
        }[/CODE]
[CODE lang="java" title="exeption"]Exception in thread "AWT-EventQueue-0" com.esotericsoftware.kryo.KryoException: Buffer overflow. Available: 0, required: 35[/CODE]
Das Programm funktioniert so weit die Datei wird geschickt aber, wenn die Datei eine bestimme Größe überschreite kommt eine Fehlermeldung. Das Programm verbraucht auch  gute 4 GB RAM.
Liebe Grüße.


----------



## kneitzel (19. Sep 2021)

Was hast du denn auch zusammen gebastelt? Du liest die ganze Datei in den Speicher ein — nur um dann irgendwie an 4096er Blöcke zu kommen …

Schau Dir mal FileInputStream an mit der Methode read. Dann liest Du nur noch in kleinen chunks.


----------



## Lurxl (19. Sep 2021)

[CODE lang="java" title="SendFile"]JFileChooser chooser = new JFileChooser();
                chooser.showOpenDialog(null);
                File file = new File(chooser.getSelectedFile().getAbsolutePath());


                try {
                InputStream is = new FileInputStream(file);

                int length = (int) file.length();           
                byte[] bytes = new byte[4096];
                int o=0;
                int a=4096;
                do {
                    is.read(bytes, 0, 4096);
                    o += a;
                    FileM m = new FileM(bytes, true);
                        ClientHandler.sendFile(m);

                } while (o < length);
                is.close();
                is=null;
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }[/CODE]
[CODE lang="java" title="Fehlermeldung"]Exception in thread "AWT-EventQueue-0" com.esotericsoftware.kryo.KryoException: Buffer overflow. Available: 0, required: 35
Serialization trace:
bytes (Message.FileM)
    at com.esotericsoftware.kryo.io.ByteBufferOutput.require(ByteBufferOutput.java:189)
    at com.esotericsoftware.kryo.io.ByteBufferOutput.writeBytes(ByteBufferOutput.java:283)
    at com.esotericsoftware.kryo.io.ByteBufferOutput.writeBytes(ByteBufferOutput.java:269)
    at com.esotericsoftware.kryo.serializers.DefaultArraySerializers$ByteArraySerializer.write(DefaultArraySerializers.java:36)
    at com.esotericsoftware.kryo.serializers.DefaultArraySerializers$ByteArraySerializer.write(DefaultArraySerializers.java:25)
    at com.esotericsoftware.kryo.Kryo.writeObjectOrNull(Kryo.java:577)
    at com.esotericsoftware.kryo.serializers.ObjectField.write(ObjectField.java:68)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:495)
    at com.esotericsoftware.kryo.Kryo.writeClassAndObject(Kryo.java:599)
    at com.esotericsoftware.kryonet.KryoSerialization.write(KryoSerialization.java:48)
    at com.esotericsoftware.kryonet.TcpConnection.send(TcpConnection.java:192)
    at com.esotericsoftware.kryonet.Connection.sendTCP(Connection.java:59)
    at Network.ClientHandler.sendFile(ClientHandler.java:66)
    at Gui.Gui.lambda$3(Gui.java:159)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6614)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6379)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4990)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4548)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2769)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)[/CODE]

[CODE lang="java" title="SendFIle"]public static void sendFile(Object o){
        if(o instanceof FileM){
            FileM m = (FileM)o;
            client.sendTCP(m); // fehlercode
        }
    }[/CODE]
Ich habe es jetzt erstmal so aber die Fehlermeldung kommt noch immer. Die Fehlermeldung kommt, wenn die Datei eine gewisse Größe überschreitet.


----------



## kneitzel (19. Sep 2021)

Also erst einmal zum Code, den Du nutzt zum lesen der Datei: Das ist nicht so, wie es eigentlich gedacht ist.
Wenn eine Methode etwas zurück gibt, dann ist es meist nicht gut, dies zu ignorieren. read gibt die Anzahl der gelesenen Bytes zurück. Damit wäre eine Schleife ungefähr so zu bauen:

```
int readBytes;
FileInputStream fileStream = ...;
byte[] buffer = new byte[BUFFER_SIZE]; // BUFFER_SIZE kannst Du beliebig setzen, also auch gerne 4096!
while ((readbytes = fileStream.read(buffer)) > 0) {
    // ...
}
```

Und wenn nicht der ganze Buffer gelesen wurde, dann kannst Du natürlich nicht einfach das ganze Array als Wert zurück geben. Du sendest z.B. das ganze Array mit 4096 Werten, auch wenn nur 10 Bytes gelesen wurden.

Bezüglich der Exception: Da wäre wichtig, was Du denn genau in sendFile vom ClientHandler machst. Du rufst da eine Methode auf, die irgendwas macht. Vermutlich wird da irgend etwas nicht ganz stimmen.


----------

