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

com.g2forge.alexandria.java.io.dataaccess.IDataSource Maven / Gradle / Ivy

There is a newer version: 0.0.18
Show newest version
package com.g2forge.alexandria.java.io.dataaccess;

import java.io.InputStream;
import java.io.Reader;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;

import com.g2forge.alexandria.annotations.message.TODO;
import com.g2forge.alexandria.java.io.HBinaryIO;
import com.g2forge.alexandria.java.io.HIO;
import com.g2forge.alexandria.java.type.ref.ITypeRef;

public interface IDataSource extends IDataAccess {
	public default byte[] getBytes() {
		return HBinaryIO.read(getStream(null));
	}

	@TODO(value = "Use static type switch", link = "G2-432")
	public default  T getReader(ITypeRef type) {
		if ((type != null) && !Reader.class.equals(type.getErasedType())) throw new IllegalArgumentException();
		@SuppressWarnings("unchecked")
		final T retVal = (T) Channels.newReader(getChannel(ITypeRef.of(ReadableByteChannel.class)), Charset.defaultCharset().newDecoder(), -1);
		return retVal;
	}

	@TODO(value = "Use static type switch", link = "G2-432")
	public default  T getStream(ITypeRef type) {
		if ((type != null) && !InputStream.class.equals(type.getErasedType())) throw new IllegalArgumentException();
		@SuppressWarnings("unchecked")
		final T retVal = (T) Channels.newInputStream(getChannel(ITypeRef.of(ReadableByteChannel.class)));
		return retVal;
	}

	public default String getString() {
		return HIO.readAll(getStream(null), false);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy