1

if nUploadSize is a Long integer below,

nUploadSize += currentline.getBytes().length;

Is there a way to get the actual byte size and not the length of the byte? If i try this:

nUploadSize += currentline.getBytes();

It doesn't accept saying:

The operator += is undefined for the argument type(s) long, byte[]

The reason i was asking this question is because I am trying a httppost upload with a dialog that shows progression in percentage. The problem is the Progress bar never shows any progression before the upload successful toast is shown

I have the following code:

boolean m_bSuccess = true; // initiating a boolean to be used later
        try
        {
                       //upload parameters done through multipartEntity

            MultipartEntity me = new MultipartEntity();

            File pFp = new File(strAttachedFile);

            me.addPart("videoFile", new FileBody(pFp, "video/3gpp"));



            httppost.setEntity(me);

            // get the total size of the file
            nTotalSize = pFp.length();

            //httpPst execution             
            HttpResponse responsePOST = client.execute(httppost);  
            HttpEntity resEntity = responsePOST.getEntity(); 

            //
            InputStream inputstream = resEntity.getContent();//generate stream from post resEntity
            BufferedReader buffered = new BufferedReader(new InputStreamReader(inputstream));//buffer initiated to read inputstream
            StringBuilder stringbuilder = new StringBuilder();
            String currentline = null; 
            while ((currentline = buffered.readLine()) != null) 
            { 
                // adding the  

                stringbuilder.append(currentline + "\n");// appending every line of upload to stringbuilder

                String result = stringbuilder.toString(); 
                nUploadSize += currentline.getBytes().length;// += upload size acquires all current line bites
                //convert upload size from integer to string to be read 
                String s = String.valueOf(nUploadSize);
                Log.i("Upload lineeeeeeeeeeeeeeeeee",s);
                Log.v("HTTP UPLOAD REQUEST",result); 
                inputstream.close();}  
            }

        catch (Exception e) {
            e.printStackTrace();
            m_bSuccess = false; //mbsucess is false on catch failure
        }
        publishProgress();

        if(m_bSuccess)
            return "success";
        return null;
    }


    protected void onProgressUpdate(Integer... progress) {

        if(m_vwProgress !=null){

            m_vwProgress.setProgress((int)(nUploadSize/nTotalSize));
        }

    }


    protected void onPostExecute(String result){

        super.onPostExecute(result);

        if(result==null){

            if(m_vwProgress !=null)
                m_vwProgress.dismiss();
            Toast.makeText(m_activity, "Upload Successful",  Toast.LENGTH_LONG).show(); 

        }
    }
}

When upload is attempted, during my Log.i("Upload lineeeeeeeeeeeeeeeeee",s); It shows the highest byte size i am getting from nUploadSize += currentline.getBytes().length; is 200. Explaining my main problem why the Progress dialog never shows percentage progression because the number is too small. That's why i was trying to see if I was actually getting the string byte size. If anybody know how to fix this, I will be grateful.

user875139
  • 1,599
  • 4
  • 23
  • 44
  • you mean the bytes as raw data? – esskar Nov 02 '11 at 20:06
  • 2
    I'm not sure I see the difference. `getBytes().length` gives you the number of bytes, how is that different from the "actual byte size"? – Kevin Nov 02 '11 at 20:06
  • 2
    If getBytes() is returning a byte array, and length gives you the length of that byte array, isn't what you have giving you the length of the line in bytes? What is not working? What are you expecting and receiving? – John B Nov 02 '11 at 20:08
  • Duplicate of http://stackoverflow.com/questions/4385623/bytes-of-a-string-in-java. Read the answers on that question to see the flaw in the answers below. – james.garriss Oct 11 '13 at 18:01

2 Answers2

3

currentLine.getBytes().length is the "byte size", i.e. the number of bytes...

SingleShot
  • 18,821
  • 13
  • 71
  • 101
1

The number of bytes is currentline.getBytes().length, and the raw data (the bytes) is currentline.getBytes()

esskar
  • 10,638
  • 3
  • 36
  • 57