Results 1 to 10 of 10
  1. #1
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default Set max upload speed

    I have this method:
    Java Code:
    private String m_sLocalFile = "image.jpg";
    private FtpClient m_client;
    ....
    ....
    ....
    protected void putFile(String HostFile) {
        byte[] buffer = new byte[BUFFER_SIZE];
        try {
          File f = new File(m_sLocalFile);
          int size = (int) f.length();
          System.out.println("File " + m_sLocalFile + ": " + size + " bytes");
          System.out.println(size);
          FileInputStream in = new FileInputStream(m_sLocalFile);
          OutputStream out = m_client.put(HostFile);
    
          int counter = 0;
          while (true) {
            int bytes = in.read(buffer);
            if (bytes < 0)
              break;
            out.write(buffer, 0, bytes);
            counter += bytes;
            System.out.println(counter);
          }
    
          out.close();
          in.close();
        } catch (Exception ex) {
          System.out.println("Error: " + ex.toString());
        }
    I need to set a max upload speed (for exemple 10kb/s) in this public. Naturally it must NOT change upload speed of other programms..
    How can i do this?
    Thx
    Last edited by Giuseppe_Mazzei; 06-02-2011 at 10:03 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    I need to set a max upload speed (for exemple 10kb/s) in this public
    Do you mean you want to pace the writing of bits to 10kb/s?
    Make a loop that writes a/some byte(s) and then waits some time and writes another byte(s). Compute the time to wait by dividing out the rate. 1 byte every xxx millisecs.

  3. #3
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    mmm so how?
    For exemple that could be right? If it's right it sends 1kb for second
    Java Code:
          int counter = 0;
          while (true) {
            Thread.sleep(1000) //1 second
            int bytes;
            if(counter+10000 > buffer.length)
            bytes = in.read(buffer, counter, buffer.length);
            else
            bytes = in.read(buffer, counter, counter+10000);
            if (bytes < 0)
              break;
            out.write(buffer, 0, bytes);
            counter += bytes;
            System.out.println(counter);
          }

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Is there a reason to slow done the read? Do all the read at normal speed.
    Then slow the writing.

    while(haveBytes > 0) {
    write a byte
    sleep long enough for the rate to be 10kb/s
    haveBytes--;
    }

  5. #5
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    I tried this:
    Java Code:
    while (true) {
            Thread.sleep(50); //0.05 second
            int bytes = in.read();
            if (bytes < 0)
              break;
            out.write(buffer, counter, bytes);
            counter += bytes;
            System.out.println(counter);
          }
    doesn't work, i have Error: java.lang.ArrayIndexOutOfBoundsException
    Pls help me _

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Please post the FULL text of any error messages.

  7. #7
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    Just this:
    Java Code:
    File PacificGaming.jpg: 143384 bytes
    143384
    255
    471
    726
    950
    950
    966
    1040
    1110
    1183
    1253
    1253
    1254
    1256
    1256
    1256
    1257
    1257
    1258
    1258
    1258
    1513
    1732
    1732
    1799
    1799
    1807
    1813
    1819
    1826
    1832
    1837
    1845
    1852
    1859
    1866
    1875
    1884
    1892
    1902
    1914
    1934
    1947
    1959
    1970
    1981
    1993
    2018
    2036
    2055
    2070
    2090
    2119
    2145
    2176
    2206
    2235
    2261
    2289
    2317
    2349
    2385
    2431
    2470
    2502
    2536
    2580
    2615
    2643
    2671
    2711
    2766
    2807
    2851
    2899
    2948
    3000
    3052
    3104
    3135
    3174
    3231
    3292
    3348
    3398
    3458
    3504
    3555
    3607
    3657
    3912
    4131
    4131
    4198
    4199
    4208
    4217
    4226
    4238
    4249
    4261
    4285
    4298
    4311
    4335
    4385
    4418
    4446
    4479
    4529
    4579
    4629
    4679
    4729
    4779
    4829
    4879
    4929
    4979
    5029
    5079
    5129
    Error: java.lang.ArrayIndexOutOfBoundsException

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Remove the println of the counter and run it again to get only the error message. It looks truncated/editted.
    Here's what I get with an OOB error:
    Java Code:
          int[] array = new int[3];
          for(int i=0; i < 10; i++)
            int x = array[i]; // out of bounds here
    //Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    //	at TestCode1.main(TestCode1.java:38)
    Can you explain what the loop is doing?
    Explain each line in your code.

    Why do you have the read() inside of the timing loop?
    Read all the bytes normally then write them slowing in the loop.
    Last edited by Norm; 06-02-2011 at 11:10 PM.

  9. #9
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Remove the println of the counter and run it again to get only the error message. It looks truncated/editted.
    Here's what I get with an OOB error:
    Java Code:
          int[] array = new int[3];
          for(int i=0; i < 10; i++)
            int x = array[i]; // out of bounds here
    //Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    //	at TestCode1.main(TestCode1.java:38)
    Can you explain what the loop is doing?
    Explain each line in your code.

    Why do you have the read() inside of the timing loop?
    Read all the bytes normally then write them slowing in the loop.

    mmmm sry but i don't understand... slowing how?

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Read all the bytes to be written .
    Begin loop
    write a byte
    wait some <<<<<<<<< This slows the writing of the bytes
    end loop

Similar Threads

  1. Improve the Speed of loop
    By venk123 in forum New To Java
    Replies: 15
    Last Post: 02-16-2011, 06:36 AM
  2. :large file upload to server(chunk upload)
    By tommy_725 in forum Networking
    Replies: 0
    Last Post: 10-16-2009, 12:21 AM
  3. Test internet speed
    By kalpo in forum Networking
    Replies: 0
    Last Post: 03-31-2009, 12:20 AM
  4. How to speed sql Statements?
    By bezudar in forum Advanced Java
    Replies: 3
    Last Post: 11-20-2008, 09:53 AM
  5. compare speed
    By bbq in forum JDBC
    Replies: 1
    Last Post: 06-28-2007, 05:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •