Search in sources :

Example 6 with BytesBuilder

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

the class As method utf8encode.

public static Outlet<Bytes> utf8encode(Outlet<Chars> charsOutlet) {
    Source<Chars> source = charsOutlet.source();
    return Outlet.of(new Source<>() {

        public Bytes source() {
            Chars chars = source.source();
            if (chars != null) {
                BytesBuilder bb = new BytesBuilder();
                for (int i = 0; i < chars.size(); i++) {
                    char ch = chars.get(i);
                    if (ch < 0x80)
                        bb.append((byte) ch);
                    else if (ch < 0x800) {
                        bb.append((byte) (0xC0 + ((ch >> 6) & 0x1F)));
                        bb.append((byte) (0x80 + ((ch >> 0) & 0x3F)));
                    } else if (ch < 0x10000) {
                        bb.append((byte) (0xE0 + ((ch >> 12) & 0x0F)));
                        bb.append((byte) (0x80 + ((ch >> 6) & 0x3F)));
                        bb.append((byte) (0x80 + ((ch >> 0) & 0x3F)));
                    } else {
                        bb.append((byte) (0xF0 + ((ch >> 18) & 0x07)));
                        bb.append((byte) (0x80 + ((ch >> 12) & 0x3F)));
                        bb.append((byte) (0x80 + ((ch >> 6) & 0x3F)));
                        bb.append((byte) (0x80 + ((ch >> 0) & 0x3F)));
                    }
                }
                return bb.toBytes();
            } else
                return null;
        }
    });
}
Also used : Bytes(suite.primitive.Bytes) Chars(suite.primitive.Chars) BytesBuilder(suite.primitive.Bytes.BytesBuilder)

Example 7 with BytesBuilder

use of suite.primitive.Bytes.BytesBuilder 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 8 with BytesBuilder

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

the class Assembler method convertByteStream.

private Bytes convertByteStream(Node node) {
    BytesBuilder bb = new BytesBuilder();
    Tree tree;
    while ((tree = Tree.decompose(node, TermOp.AND___)) != null) {
        bb.append((byte) ((Int) tree.getLeft()).number);
        node = tree.getRight();
    }
    return bb.toBytes();
}
Also used : Tree(suite.node.Tree) Int(suite.node.Int) BytesBuilder(suite.primitive.Bytes.BytesBuilder)

Example 9 with BytesBuilder

use of suite.primitive.Bytes.BytesBuilder 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 10 with BytesBuilder

use of suite.primitive.Bytes.BytesBuilder 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

BytesBuilder (suite.primitive.Bytes.BytesBuilder)12 Bytes (suite.primitive.Bytes)6 Int (suite.node.Int)2 Chars (suite.primitive.Chars)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Extent (suite.file.ExtentAllocator.Extent)1 ExtentFile (suite.file.ExtentFile)1 Node (suite.node.Node)1 Reference (suite.node.Reference)1 Tree (suite.node.Tree)1 CharsBuilder (suite.primitive.Chars.CharsBuilder)1 DataInput_ (suite.util.DataInput_)1 DataOutput_ (suite.util.DataOutput_)1 Source (suite.util.FunUtil.Source)1 Serialize (suite.util.Serialize)1