All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.thepun.unsafe.chars.WideZeroEndingCharSequence Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C)2011 - Marat Gariev 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.github.thepun.unsafe.chars;

import io.github.thepun.unsafe.OffHeapMemory;
import io.github.thepun.unsafe.TypeSize;

public final class WideZeroEndingCharSequence implements CharSequence {

    private long address;
    private int length;

    public WideZeroEndingCharSequence() {
        length = 0;
        address = 0;
    }

    public WideZeroEndingCharSequence(long address, int maxLnegth) {
        setAddress(address, maxLnegth);
    }

    public void setAddress(long address, int maxLnegth) {
        this.address = address;
        scanForZero(maxLnegth);
    }

    @Override
    public int length() {
        return length;
    }

    @Override
    public char charAt(int index) {
        return OffHeapMemory.getChar(address + index * TypeSize.CHAR);
    }

    @Override
    public WideOffHeapCharSequence subSequence(int start, int end) {
        return new WideOffHeapCharSequence(address + start * TypeSize.CHAR, end - start);
    }

    @Override
    public String toString() {
        char[] chars = new char[length];
        OffHeapMemory.copyToArray(address, chars);
        return new String(chars);
    }

    private void scanForZero(int maxLnegth) {
        int k = 0;

        long currentAddress = address + maxLnegth * TypeSize.CHAR;
        for (long i = address; i < currentAddress; i += TypeSize.CHAR) {
            char c = OffHeapMemory.getChar(i);
            if (c == 0) {
                length = k;
                return;
            }

            k++;
        }

        length = maxLnegth;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy