Dear Sir/madam,

When i'm using connectionRequest to upload file to OneDrive, there is 
always out of memory error when the size of file is just a little large, 
say, over 60MB. Herewith attach a sample code i wrote.

/**
 * Your application code goes here
 */
package userclasses;

import com.codename1.io.BufferedOutputStream;
import com.codename1.system.NativeLookup;
import com.codename1.util.Base64;
import generated.StateMachineBase;
import com.codename1.ui.*; 
import com.codename1.ui.events.*;
import com.codename1.ui.util.Resources;
import com.codename1.io.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

public class StateMachine extends StateMachineBase {
    public StateMachine(String resFile) {
        super(resFile);
        // do not modify, write code in initVars and initialize class 
members there,
        // the constructor might be invoked too late due to race conditions 
that might occur
    }
    
    protected void initVars(Resources res) {
    }
    @Override
    protected void onMain_ButtonAction(Component c, ActionEvent event) {

            Display.getInstance().invokeAndBlock(new Runnable() {
                BackupOneDrive task = new BackupOneDrive();

                @Override
                public void run() {
//            new Thread(task).start();
                    task.run();

                }
            });
    
    }

    class BackupOneDrive extends Thread {
        public BackupOneDrive() {

        }

        public void run() {
            try {
               ConnectionRequest req = new ConnectionRequest() {


                    @Override
                    protected void buildRequestBody(OutputStream os) throws 
IOException {
                        System.out.println("Request Body");
                        byte[] b = new byte[157286400];
                        new Random().nextBytes(b);
                        InputStream inputStream = new 
ByteArrayInputStream(b);

                        synchronized (inputStream) {
                            BufferedOutputStream bos = new 
BufferedOutputStream(os);
                            try {
                                byte[] buffer = new byte[4096];
                                int length = 0;
                                while ((length = inputStream.read(buffer)) 
!= -1) {
                                    System.out.println("length: " + 
buffer.toString());
                                    bos.write(buffer, 0, length);
                                    bos.flush();
                                }
                                inputStream.close();
                            } catch (Throwable e) {
                                e.printStackTrace();
                            } finally {
                                bos.close();
                            }
                        }

                    }
                };
                req.setDuplicateSupported(true);
                req.setSilentRetryCount(10);
                req.setTimeout(3000);
                NetworkManager.getInstance().setTimeout(3000);
                req.setReadResponseForErrors(false);
                req.setChunkedStreamingMode(4096);
                req.setPriority(ConnectionRequest.PRIORITY_CRITICAL);
                req.setPost(true);
                req.addResponseListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        NetworkEvent e = (NetworkEvent) evt;
                        if (e.getError() != null) {
                            e.consume();
                            System.out.println("error: " + 
e.getError().getMessage());
                            return;
                        }
                    }
                });

                
req.setUrl("https://apis.live.net/v5.0/{folder}/files/test.mp4?access_token={AccessToken}&downsize_photo_uploads=false";);
                req.setHttpMethod("PUT");
                req.setContentType("");
                NetworkManager.getInstance().addToQueueAndWait(req);
                System.out.println("finish");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

}

In that case, I just use a random byte about 150MB and the out of memory 
came out at about 60MB.
Moreover, the error happens in both simulator and real mobile. 
Please advice and suggest us a solution.

Thanks and Regards,
Roanld

-- 
You received this message because you are subscribed to the Google Groups 
"CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/codenameone-discussions/2f07ccb6-766c-471b-b252-83a20152cddd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to