// Path: core/src/main/java/net/xsoftlab/ml4j/exception/Ml4jException.java
// public class Ml4jException extends RuntimeException {
// 
// 	private static final long serialVersionUID = 5254527327442579657L;
// 
// 	public static Logger logger = LoggerFactory.getLogger(Ml4jException.class);
// 
// 	public Ml4jException(String message) {
// 
// 		super(message);
// 	}
// 
// 	public static void logAndThrowException(String message) {
// 		Ml4jException e = new Ml4jException(message);
// 		logger.error(message, e);
// 		throw e;
// 	}
// }


import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import net.xsoftlab.ml4j.exception.Ml4jException;
import org.jblas.FloatMatrix;
	public static FloatMatrix loadMNISTLabel(String filePath) throws IOException {

		FileInputStream in = null;
		FloatMatrix matrix = null;
		try {
			in = new FileInputStream(filePath);
			// 获取输入输出通道
			FileChannel channel = in.getChannel();
			ByteBuffer byteBuffer = ByteBuffer.allocate(8);
			channel.read(byteBuffer);

			byteBuffer.rewind();
			int magicNumber = byteBuffer.getInt();
			if (magicNumber != 2049) {
				Ml4jException.logAndThrowException("magic number = " + magicNumber + " 不正确，应为2049");
			}

			int count = byteBuffer.getInt();
			byteBuffer = ByteBuffer.allocate(count);
			float[] buffer = new float[count];
			matrix = new FloatMatrix(count, 1);

			int j = 0;
			byteBuffer.clear();
			channel.read(byteBuffer);
			byteBuffer.rewind();
			while (byteBuffer.hasRemaining()) {
				buffer[j++] = byteBuffer.get() & 0xff;
			}
			matrix.putColumn(0, new FloatMatrix(buffer));
// Path: core/src/main/java/net/xsoftlab/ml4j/exception/Ml4jException.java
// public class Ml4jException extends RuntimeException {
// 
// 	private static final long serialVersionUID = 5254527327442579657L;
// 
// 	public static Logger logger = LoggerFactory.getLogger(Ml4jException.class);
// 
// 	public Ml4jException(String message) {
// 
// 		super(message);
// 	}
// 
// 	public static void logAndThrowException(String message) {
// 		Ml4jException e = new Ml4jException(message);
// 		logger.error(message, e);
// 		throw e;
// 	}
// }


// Path: examples/src/main/java/net/xsoftlab/ml4j/util/MNISTReader.java
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import net.xsoftlab.ml4j.exception.Ml4jException;
import org.jblas.FloatMatrix;

	public static FloatMatrix loadMNISTLabel(String filePath) throws IOException {

		FileInputStream in = null;
		FloatMatrix matrix = null;
		try {
			in = new FileInputStream(filePath);
			// 获取输入输出通道
			FileChannel channel = in.getChannel();
			ByteBuffer byteBuffer = ByteBuffer.allocate(8);
			channel.read(byteBuffer);

			byteBuffer.rewind();
			int magicNumber = byteBuffer.getInt();
			if (magicNumber != 2049) {
				Ml4jException.logAndThrowException("magic number = " + magicNumber + " 不正确，应为2049");
			}

			int count = byteBuffer.getInt();
			byteBuffer = ByteBuffer.allocate(count);
			float[] buffer = new float[count];
			matrix = new FloatMatrix(count, 1);

			int j = 0;
			byteBuffer.clear();
			channel.read(byteBuffer);
			byteBuffer.rewind();
			while (byteBuffer.hasRemaining()) {
				buffer[j++] = byteBuffer.get() & 0xff;
			}
			matrix.putColumn(0, new FloatMatrix(buffer));
		} finally {