file writing methods


    Apr 25 2006 | 11:02 pm
    Hi,
    today I discovered that both the writeint16() and the writebytes()
    methods of the file object will not take arrays with more than 256
    elements.
    Am I right in finding this out ?
    so i wrote these two functions, the only other variable they need is
    'file', which should be a open (and checked) file object.
    Works fine, but for largish files the thing gets very slow indeed..
    function writeManyInts(array)
    {
    var chunkSize = 256;
    var count = array.length;
    var chunks = Math.floor(count / chunkSize);
    var rem = count % chunkSize;
    var tempArray;
    switch (chunks)
    {
    case 0:
    file.writeint16(array); // write first chunk = remainder
    break;
    case 1:
    file.writeint16(array.splice(0, chunkSize)); // write first chunk
    file.writeint16(array); // write remainder
    break;
    default:
    // post("itemsleft:", array.length, "filepos:", file.position,"n");
    for (var k=0; k {
    tempArray = array.splice(0, chunkSize);
    file.writeint16(tempArray);
    // post("temparraylen:", tempArray.length,"n");
    // post("itemsleft:", array.length, "filepos:", file.position,"n");
    }
    file.writeint16(array); // write remainder
    }
    }
    function writeManyBytes(array)
    {
    var chunkSize = 256;
    var count = array.length;
    var chunks = Math.floor(count / chunkSize);
    var rem = count % chunkSize;
    switch (chunks)
    {
    case 0:
    file.writebytes(array); // write first chunk = remainder
    break;
    case 1:
    file.writebytes(array.splice(0, chunkSize)); // write first chunk
    file.writebytes(array); // write remainder
    break;
    default:
    for (var k=0; k {
    file.writebytes(array.splice(0, chunkSize));
    }
    file.writebytes(array); // write remainder
    }
    }
    -------------------------------------------
    Joost Rekveld
    -------------------------------------------
    "The mystery of the world is the visible, not the invisible"
    (Oscar Wilde)
    -------------------------------------------