Вопрос

I'm trying to download a song file. The following code (well, the original code, this is just an example of what I'm doing) is working perfectly on an Asha 310 device. However, on the newer Asha 501 devices, the resulting downloaded file is much larger than the actual file size. A 2.455.870 byte file ends up downloading 2.505.215 bytes if I use a 512 buffer, and it doesn't load either. Using a 4096 buffer, the file ends up being 3.342.335 bytes in size!!

What could be the reason for this happening? It's working perfectly on the other phone, and I'm using very reasonable buffers.

    downloadedFile = (FileConnection) Connector.open(saveLocation+"testing.m4a", Connector.READ_WRITE);

    if (!downloadedFile.exists()) {
        downloadedFile.create();
    }

    ops = downloadedFile.openOutputStream();
    hc = (HttpConnection) Connector.open(url);
    hc.setRequestMethod(HttpsConnection.POST);
    hc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    String postData = "sid=" + session.sid + "&fileid=" + file.getId();

    byte[] request_body = postData.getBytes();

    DataOutputStream dos = null;
    dos = hc.openDataOutputStream();
    for (int i = 0; i < request_body.length; i++) {
        dos.writeByte(request_body[i]);
    }

    byte[] buf = new byte[512];

    dis = hc.openInputStream();
    int downloadSize = 0;

    while (dis.read(buf) != -1) {

        ops.write(buf, 0, buf.length);

        downloadedSize += buf.length;            

    }
Это было полезно?

Решение

Turns out the buffer isn't being fully filled out, so the rest of each buffer that isn't filled out is junk. Which explains why when a bigger buffer is set, the file is bigger, as it has more junk.

http://developer.nokia.com/Community/Discussion/showthread.php/244179-Download-size-larger-than-available-stream-file-size-(Asha-501)

int len;
while((len=dis.read(buf))!=-1)
{
    ops.write(buf,0,len);
    downloadedSize += len;
}

Edit: It was working on the older phones because they filled out the entire buffer all the time with actual data. The newer devices don't.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top