0

ここから、Int配列をファイルに書き込むためのコードを取得しました。しかし、私はそれを変換しようとしているので、Long配列をファイルに書き込むことができます。しかし、それはエラーを出します(以下に与えられたコード)。なぜエラーが発生するのか、修正されたコードは何であるのか、誰かが私を助けてくれますか?ありがとう。

import java.io.*;
import java.util.ArrayList;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Random;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

public class Test {
    private static final int bucketSize = 1<<17;//in real world should not be const, but we bored horribly
    static final int zipLevel = 2;//feel free to experiement, higher compression (5+)is likely to be total waste


 static void writes(long[] a, File file, boolean sync) throws IOException{
        byte[] bucket = new byte[Math.min(bucketSize,  Math.max(1<<13, Integer.highestOneBit(a.length >>3)))];//128KB bucket
        byte[] zipOut = new byte[bucket.length];

        final FileOutputStream fout = new FileOutputStream(file);
        FileChannel channel = fout.getChannel();
        try{

            ByteBuffer buf = ByteBuffer.wrap(bucket);
            //unfortunately java.util.zip doesn't support Direct Buffer - that would be the perfect fit
            ByteBuffer out = ByteBuffer.wrap(zipOut);
            out.putLong(a.length);//write length aka header
            if (a.length==0){
                doWrite(channel, out, 0);
                return;
            }

            Deflater deflater = new Deflater(zipLevel, false);
            try{
                for (int i=0;i<a.length;){
                    i = puts(a, buf, i);
                    buf.flip();
                    deflater.setInput(bucket, buf.position(), buf.limit());

                    if (i==a.length)
                        deflater.finish();

                    //hacking and using bucket here is tempting since it's copied twice but well
                    for (int n; (n= deflater.deflate(zipOut, out.position(), out.remaining()))>0;){
                        doWrite(channel, out, n);
                    }
                    buf.clear();
                }

            }finally{
                deflater.end();
            }
        }finally{
            if (sync)
                fout.getFD().sync();
            channel.close();
        }
    }

    static long[] reads(File file) throws IOException, DataFormatException{
        FileChannel channel = new FileInputStream(file).getChannel();
        try{
            byte[] in = new byte[(int)Math.min(bucketSize, channel.size())];
            ByteBuffer buf = ByteBuffer.wrap(in);

            channel.read(buf);
            buf.flip();
            long[] a = new long[(int)buf.getLong()];
            if (a.length==0)
                return a;
            int i=0;
            byte[] inflated = new byte[Math.min(1<<17, a.length*4)];
            ByteBuffer intBuffer = ByteBuffer.wrap(inflated);
            Inflater inflater = new Inflater(false);
            try{
                do{
                    if (!buf.hasRemaining()){
                        buf.clear();
                        channel.read(buf);
                        buf.flip();
                    }
                    inflater.setInput(in, buf.position(), buf.remaining());
                    buf.position(buf.position()+buf.remaining());//simulate all read

                    for (;;){
                        int n = inflater.inflate(inflated,intBuffer.position(), intBuffer.remaining());
                        if (n==0)
                            break;
                        intBuffer.position(intBuffer.position()+n).flip();
                        for (;intBuffer.remaining()>3 && i<a.length;i++){//need at least 4 bytes to form an int
                            a[i] = intBuffer.getInt();
                        }
                        intBuffer.compact();
                    }

                }while (channel.position()<channel.size() && i<a.length);
            }finally{
                inflater.end();
            }
            //          System.out.printf("read ints: %d - channel.position:%d %n", i, channel.position());
            return a;
        }finally{
            channel.close();
        }
    }

    private static void doWrite(FileChannel channel, ByteBuffer out, int n) throws IOException {
        out.position(out.position()+n).flip();
        while (out.hasRemaining())
            channel.write(out);
        out.clear();
    }
    private static int puts(long[] a, ByteBuffer buf, int i) {
        for (;buf.hasRemaining() && i<a.length;){
            buf.putLong(a[i++]);
        }
        return i;
    }





    private static long[] generateRandom(int len){
        Random r = new Random(17);
        long[] n = new long [len];
        for (int i=0;i<len;i++){
            n[i]= r.nextBoolean()?0: r.nextInt(1<<23);//limit bounds to have any sensible compression
        }
        return n;
    }
    public static void main(String[] args) throws Throwable{
        File file = new File("xxx.xxx");
        long[] n = generateRandom(3000000); //{0,2,4,1,2,3};
        long start = System.nanoTime();
        writes(n, file, false);
        long elapsed = System.nanoTime() - start;//elapsed will be fairer if the sync is true

        System.out.printf("File length: %d, for %d ints, ratio %.2f in %.2fms %n", file.length(), n.length, ((double)file.length())/4/n.length, java.math.BigDecimal.valueOf(elapsed, 6) );

        long[] m = reads(file);

        //compare, Arrays.equals doesn't return position, so it sucks/kinda
        for (int i=0; i<n.length; i++){
            if (m[i]!=n[i]){
                System.err.printf("Failed at %d%n",i);
                break;
            }
        }
        System.out.printf("All done!");
    };

}
4

2 に答える 2

2

そのため、実際にコードを実行するのに数分かかり、投稿されたコードから少し調整する必要がありましたが、ここにあります。

私がしたことの1つは、わかりやすくするために、「不要」に変更intBufferすることでしたlongBuffer。それは最初の違いの一部です

75  -  byte[] inflated = new byte[Math.min(1<<17, a.length*4)];
76  -  ByteBuffer intBuffer = ByteBuffer.wrap(inflated);
76  +  byte[] inflated = new byte[Math.min(1<<17, a.length*8)];
77  +  ByteBuffer longBuffer = ByteBuffer.wrap(inflated);

上記のスニペットでは、膨張したバッファーの長さをa.length * 8に変更して、int配列ではなくlong配列であることを反映しています。

89  -  int n = inflater.inflate(inflated,intBuffer.position(), intBuffer.remaining());
90  +  int n = inflater.inflate(inflated,longBuffer.position(), longBuffer.remaining());

これは、変数名の変更にすぎません。

92  -  intBuffer.position(intBuffer.position()+n).flip();
93  -  for (;intBuffer.remaining()>3 && i<a.length;i++){//need at least 4 bytes to form an int
94  -      a[i] = intBuffer.getInt();
93  +  longBuffer.position(longBuffer.position()+n).flip();
94  +  for (;longBuffer.remaining()>7 && i<a.length;i++){//need at least 4 bytes to form an int
95  +      a[i] = longBuffer.getLong();

これは非常に重要な変更です。最初に名前が変更されましたが、それは重要な部分ではありません。第二に、remaining()bestsssが指摘したように、これは3ではなく7です。最後に、a[i]はintではなくlongになりました。。それが最大の問題です。

96  -  intBuffer.compact();
97  +  longBuffer.compact();

ここで名前を変更するだけです。

142 -  System.out.printf("File length: %d, for %d ints, ratio %.2f in %.2fms %n", file.length(), n.length, ((double)file.length())/4/n.length, java.math.BigDecimal.valueOf(elapsed, 6) );
143 +  System.out.printf("File length: %d, for %d ints, ratio %.2f in %.2fms %n", file.length(), n.length, ((double)file.length())/8/n.length, java.math.BigDecimal.valueOf(elapsed, 6) );

これは、圧縮のアイデアを得るためにファイル出力にあります。現在、file.length/8からの結果の数を4以上ではなく計算しています。

そして、それらは私がそれを機能させるために行わなければならなかった唯一の必要な編集です。基本的に、すべての場所でintからlongに移動するだけです。

差分表記などをいじくりまわす場合に備えて、完全なコードはここにペーストビンにあります:http: //pastebin.com/emY14Ji4

注:削除しなかったデバッグステートメントのため、コピー(+)の行番号はコピー(-)より1つ大きくなっています...おっと...

于 2012-07-06T21:30:14.963 に答える
-1

バイトバッファを使用せず、代わりにObjectOutputStreamを使用して検証可能ファイルをファイルに格納し、ObjectInputStreamを使用してファイルから読み取ります。問題は、プロセス全体を通して「長い」定義を保持していないことだと思います。

ObjectOutputStreamおよびObjectInputStreamは、任意のデータ型の読み取り/書き込みを行うことができます。

于 2012-07-06T19:19:50.753 に答える