Search in sources :

Example 41 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class TextUtil method patch.

public Bytes patch(Bytes bytes, List<Pair<Bytes, Bytes>> pairs) throws ConflictException {
    BytesBuilder bb = new BytesBuilder();
    int p = 0;
    for (Pair<Bytes, Bytes> pair : pairs) {
        int p1 = p + pair.t0.size();
        if (Objects.equals(bytes.range(p, p1), pair.t0))
            bb.append(pair.t1);
        else
            throw new ConflictException();
        p = p1;
    }
    return bb.toBytes();
}
Also used : Bytes(suite.primitive.Bytes) BytesBuilder(suite.primitive.Bytes.BytesBuilder)

Example 42 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class RollingHashUtilTest method test2.

@Test
public void test2() {
    Bytes bytes = To.bytes("0123456789abcdef");
    int size = bytes.size();
    int rollingHash = rollingHashUtil.hash(bytes.range(0, 10));
    for (int pos = 10; pos < size; pos++) {
        rollingHash = rollingHashUtil.unroll(rollingHash, bytes.get(pos - 10), 10);
        rollingHash = rollingHashUtil.roll(rollingHash, bytes.get(pos));
    }
    assertEquals(rollingHashUtil.hash(bytes.range(size - 10)), rollingHash);
}
Also used : Bytes(suite.primitive.Bytes) Test(org.junit.Test)

Example 43 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class BootMain method run.

@Override
protected boolean run(String[] args) throws IOException {
    Bytes bootLoader = new Assembler(16).assemble(FileUtil.read("src/main/asm/bootloader.asm"));
    Bytes kernel = new ImperativeCompiler().compile(0x40000, Paths.get("src/main/il/kernel.il"));
    if (bootLoader.size() == 512 && kernel.size() < 65536) {
        // combine the images and align to 512 bytes
        Bytes disk0 = Bytes.concat(bootLoader, kernel);
        Bytes disk1 = disk0.pad(disk0.size() + 511 & 0xFFFFFE00);
        String image = "target/boot.bin";
        Files.write(Paths.get(image), disk1.toArray());
        System.out.println("cat " + image + " | dd bs=512 count=1 | /opt/udis86-1.7.2/udcli/udcli -16 | less");
        System.out.println("cat " + image + " | dd bs=512 skip=1 | /opt/udis86-1.7.2/udcli/udcli -32 | less");
        System.out.println("qemu-system-x86_64 target/boot.bin");
        return true;
    } else
        return Fail.t("size not match");
}
Also used : Bytes(suite.primitive.Bytes) ImperativeCompiler(suite.ip.ImperativeCompiler)

Example 44 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class AllocatorImpl method updateAllocMap.

private void updateAllocMap(int start, int end, byte b) {
    while (start < end) {
        int p = start / pageSize;
        int s = p * pageSize;
        int e = s + pageSize;
        int end_ = min(e, end);
        int p0 = 0;
        int p1 = start - s;
        int p2 = end_ - s;
        int p3 = pageSize;
        Bytes bytes = allocMapFile.load(p);
        BytesBuilder bb = new BytesBuilder();
        bb.append(bytes.range(p0, p1));
        for (int i = p1; i < p2; i++) bb.append(b);
        bb.append(bytes.range(p2, p3));
        allocMapFile.save(p, bb.toBytes());
        start = end_;
    }
}
Also used : Bytes(suite.primitive.Bytes) BytesBuilder(suite.primitive.Bytes.BytesBuilder)

Example 45 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class FileFactory method extentFile.

public static ExtentFile extentFile(PageFile pf) {
    Serialize serialize = Serialize.me;
    Serializer<Extent> extentSerializer = serialize.extent();
    Serializer<Bytes> bytesSerializer = serialize.variableLengthBytes;
    SerializedPageFile<Block> pageFile = SerializedFileFactory.serialized(pf, new Serializer<>() {

        public Block read(DataInput_ dataInput) throws IOException {
            Extent extent = extentSerializer.read(dataInput);
            Bytes bytes = bytesSerializer.read(dataInput);
            return new Block(extent, bytes);
        }

        public void write(DataOutput_ dataOutput, Block block) throws IOException {
            extentSerializer.write(dataOutput, block.extent);
            bytesSerializer.write(dataOutput, block.bytes);
        }
    });
    return new ExtentFile() {

        public void close() throws IOException {
            pageFile.close();
        }

        public void sync() {
            pageFile.sync();
        }

        public Bytes load(Extent extent) {
            BytesBuilder bb = new BytesBuilder();
            for (int pointer = extent.start; pointer < extent.end; pointer++) {
                Block block = pageFile.load(pointer);
                Util.assert_(Objects.equals(block.extent, extent));
                bb.append(block.bytes);
            }
            return bb.toBytes();
        }

        public void save(Extent extent, Bytes bytes) {
            for (int pointer = extent.start, p = 0; pointer < extent.end; pointer++) {
                int p1 = p + blockSize;
                pageFile.save(pointer, new Block(extent, bytes.range(p, p1)));
                p = p1;
            }
        }

        public List<Extent> scan(int start, int end) {
            List<Extent> extents = new ArrayList<>();
            int pointer = start;
            while (pointer < end) {
                Extent extent = pageFile.load(pointer).extent;
                if (start <= extent.start && extent.end <= end)
                    extents.add(extent);
                pointer = extent.end;
            }
            return extents;
        }
    };
}
Also used : Serialize(suite.util.Serialize) Extent(suite.file.ExtentAllocator.Extent) DataOutput_(suite.util.DataOutput_) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ExtentFile(suite.file.ExtentFile) Bytes(suite.primitive.Bytes) DataInput_(suite.util.DataInput_) BytesBuilder(suite.primitive.Bytes.BytesBuilder)

Aggregations

Bytes (suite.primitive.Bytes)56 Test (org.junit.Test)18 BytesBuilder (suite.primitive.Bytes.BytesBuilder)8 IOException (java.io.IOException)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Pair (suite.adt.pair.Pair)4 OutputStream (java.io.OutputStream)3 Path (java.nio.file.Path)3 To (suite.util.To)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Constants (suite.Constants)2 Amd64Interpret (suite.assembler.Amd64Interpret)2 Condition (suite.concurrent.Condition)2 Extent (suite.file.ExtentAllocator.Extent)2 JournalledPageFile (suite.file.JournalledPageFile)2 ImperativeCompiler (suite.ip.ImperativeCompiler)2 DataInput_ (suite.util.DataInput_)2