<commit_before>package org.openlca.core.matrix.io.npy;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;

class Header {
	String dtype;
	boolean fortranOrder;
	int[] shape;

	/**
	 * Contains the number of bytes from the beginning of the file to the
	 * position where the data section starts. The offset includes the magic
	 * string, the version fields, the header length, and the header.
	 */
	int dataOffset;

	@Override
	public String toString() {
		StringBuilder b = new StringBuilder("{'descr': '");
		b.append(dtype).append("', 'fortran_order': ");
		if (fortranOrder) {
			b.append("True");
		} else {
			b.append("False");
		}
		b.append(", 'shape': (");
		if (shape != null) {
			for (int i : shape) {
				b.append(i).append(',');
			}
		}
		b.append("), }");
		return b.toString();
	}

	static Header read(File file) {
		try (FileInputStream fis = new FileInputStream(file)) {
			return read(fis);
		} catch (IOException e) {
			throw new RuntimeException(
					"failed to read header from " + file, e);
		}
	}

	static Header read(InputStream in) {
		try {
			// The first 6 bytes are a magic string: exactly \x93NUMPY.
			byte[] bytes = new byte[6];
			int n = in.read(bytes);
			if (n != 6) {
				throw new IllegalArgumentException("Not a npy file.");
			}
			String numpy = new String(bytes, 1, 5);
			if (!numpy.equals("NUMPY")) {
				throw new IllegalArgumentException("Not a npy file.");
			}

			// check the version
			int v = in.read();
			if (v < 1) {
				throw new IllegalArgumentException(
						"Invalid npy file, a major version >= 1 is required.");
			}

			// skip the minor version
			in.read();

			// read the header length
			bytes = new byte[2];
			n = in.read(bytes);
			if (n != 2) {
				throw new IllegalArgumentException("Not a npy file.");
			}<commit_msg>it is useful to have the Header in the public API<commit_after>package org.openlca.core.matrix.io.npy;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;

/**
 * The header information of an NPY file.
 *
 * see https://numpy.org/devdocs/reference/generated/numpy.lib.format.html
 */
public class Header {

	public String dtype;
	public boolean fortranOrder;
	public int[] shape;

	/**
	 * Contains the number of bytes from the beginning of the file to the
	 * position where the data section starts. The offset includes the magic
	 * string, the version fields, the header length, and the header.
	 */
	int dataOffset;

	@Override
	public String toString() {
		StringBuilder b = new StringBuilder("{'descr': '");
		b.append(dtype).append("', 'fortran_order': ");
		if (fortranOrder) {
			b.append("True");
		} else {
			b.append("False");
		}
		b.append(", 'shape': (");
		if (shape != null) {
			for (int i : shape) {
				b.append(i).append(',');
			}
		}
		b.append("), }");
		return b.toString();
	}

	public static Header read(File file) {
		try (FileInputStream fis = new FileInputStream(file)) {
			return read(fis);
		} catch (IOException e) {
			throw new RuntimeException(
					"failed to read header from " + file, e);
		}
	}

	public static Header read(InputStream in) {
		try {
			// The first 6 bytes are a magic string: exactly \x93NUMPY.
			byte[] bytes = new byte[6];
			int n = in.read(bytes);
			if (n != 6) {
				throw new IllegalArgumentException("Not a npy file.");
			}
			String numpy = new String(bytes, 1, 5);
			if (!numpy.equals("NUMPY")) {
				throw new IllegalArgumentException("Not a npy file.");
			}

			// check the version
			int v = in.read();
			if (v < 1) {
				throw new IllegalArgumentException(
						"Invalid npy file, a major version >= 1 is required.");
			}

			// skip the minor version
			in.read();

			// read the header length
			bytes = new byte[2];
			n = in.read(bytes);
			if (n != 2) {
				throw new IllegalArgumentException("Not a npy file.");
			}
{'fraction_non_alphanumeric': 0.13804795349963672, 'fraction_numerical': 0.005328166626301768, 'mean_word_length': 3.150753768844221, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5563598', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1313, 'n_words': 431}