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

org.liblouis.WideString Maven / Gradle / Ivy

Go to download

JNA based Java bindings to liblouis, an open-source braille translator and back-translator.

There is a newer version: 5.1.0
Show newest version
package org.liblouis;

import com.sun.jna.Memory;
import com.sun.jna.NativeMapped;
import com.sun.jna.Pointer;
import com.sun.jna.PointerType;

public class WideString extends PointerType implements NativeMapped {
	
	private final int length;
	
	public WideString() {
		this(0);
	}
	
	public WideString(int length) {
		this.length = length;
	}
	
	public WideString(String value) {
		this(value.length());
		write(value);
	}
	
	public WideString(Pointer p, int offset, int length) {
		this(length);
		setPointer(p.share(offset * WideChar.Constants.CHARSIZE));
	}
	
	public String read(int length) {
		if (length > length())
			throw new IllegalArgumentException("Maximum length is " + length());
		try {
			return new String(getPointer().getByteArray(0, length * WideChar.Constants.CHARSIZE), WideChar.Constants.ENCODING); }
		catch (Exception e) {
			throw new RuntimeException(e); }
	}
	
	public WideString write(String value) {
		if (value.length() > length)
			throw new IllegalArgumentException("Maximum string length is " + length());
		try {
			getPointer().write(0, value.getBytes(WideChar.Constants.ENCODING), 0, value.length() * WideChar.Constants.CHARSIZE); }
		catch (Exception e) {
			throw new RuntimeException(e); }
		return this;
	}
	
	@Override
	public Pointer getPointer() {
		if (super.getPointer() == null) {
			try {
				setPointer(new Memory(length * WideChar.Constants.CHARSIZE)); }
			catch (Exception e) {
				throw new RuntimeException(e); }}
		return super.getPointer();
	}
	
	public int length() {
		return length;
	}
	
	public WideString substring(int beginIndex) {
		return substring(beginIndex, length);
	}
	
	public WideString substring(int beginIndex, int endIndex) {
		if (beginIndex < 0 || endIndex > length || beginIndex > endIndex)
			throw new IndexOutOfBoundsException();
		return new WideString(getPointer(), beginIndex, endIndex - beginIndex);
	}
	
	@Override
	public String toString() {
		return read(length());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy