将其转换为java

时间:2015-05-17 19:31:04

标签: java actionscript-3

我正在尝试将此文件编码器从actionscript 3转换为java:

const FILE_SIGNATURE : String = "ASEF";

function encode ( pixels : Array ):ByteArray
{
    var swatch : ByteArray = new ByteArray();
    var ase : ByteArray = new ByteArray();
    var hex:String;
    var pix:uint;

    ase.writeUTFBytes (FILE_SIGNATURE);// header
    ase.writeInt (0x10000);// version
    ase.writeInt (pixels.length * 2);// blocks

    for (var i : int = 0; i < pixels.length; ++i)
    {
        pix = pixels[i];
        swatch.length = 0;

        // start of group

        ase.writeShort (0xC001);
        ase.writeInt (0);
        ase.writeShort (1);

        // swatch name

        hex = pix.toString(16);
        while (hex.length < 6) hex = "0" + hex;

        swatch.writeShort ((hex = "#" + hex).length + 1);
        for (var n : int = 0; n < hex.length; ++n) swatch.writeShort (hex.charCodeAt(n));
        swatch.writeShort (0);

        // colours

        swatch.writeUTFBytes ("RGB ");
        swatch.writeFloat ((pix >> 16 & 0xFF) / 255);
        swatch.writeFloat ((pix >> 8 & 0xFF) / 255);
        swatch.writeFloat ((pix & 0xFF) / 255);
        swatch.writeShort (2);

        // write swatch

        ase.writeInt (swatch.length);
        ase.writeBytes (swatch);
    }

    return ase;
}

这就是我提出来的,但它似乎不起作用:

public void Encoder(int[] pixels, File output) throws IOException {
        String FILE_SIGNATURE = "ASEF";
        DataOutputStream ase = new DataOutputStream(new FileOutputStream(output));
        DataOutputStream swatch = new DataOutputStream(ase);
        String hex;
        int pix;

        ase.writeUTF(FILE_SIGNATURE);
        ase.writeInt (0x10000);// version
        ase.writeInt (pixels.length * 2);// blocks

        for (int i = 0; i < pixels.length; ++i)
        {
            pix = pixels[i];

            // start of group
            ase.writeShort (0xC001);
            ase.writeInt (0);
            ase.writeShort (1);

            // swatch name
            hex = Integer.toHexString(pix);


            while (hex.length() < 6) hex = "0" + hex;

            swatch.writeShort ((hex = "#" + hex).length() + 1);
            for (int n = 0; n < hex.length(); ++n) swatch.writeShort (hex.charAt(n));
            swatch.writeShort (0);

            // colours
            swatch.writeUTF("RGB ");
            swatch.writeFloat ((pix >> 16 & 0xFF) / 255);
            swatch.writeFloat ((pix >> 8 & 0xFF) / 255);
            swatch.writeFloat ((pix & 0xFF) / 255);
            swatch.writeShort (2);

            // write swatch
            ase.writeInt (swatch.size());
            ase.writeBytes (swatch.toString());
        }

    }

我认为问题出在最后一行ase.writeBytes (swatch.toString());,因为输出包含“java.io.DataOutputStream @ ...”而不是输出流所拥有的数据。

我是否必须使用与DataOutputStream不同的方法,否则我只是在某个地方出错?请帮忙。

0 个答案:

没有答案