<commit_before>package com.ripplar_games.mesh_io;

import java.io.BufferedInputStream;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.ripplar_games.mesh_io.formats.mbmsh.MbMshFormat;
import com.ripplar_games.mesh_io.formats.obj.ObjFormat;
import com.ripplar_games.mesh_io.formats.ply.PlyFormatAscii_1_0;
import com.ripplar_games.mesh_io.index.IndicesDataType;
import com.ripplar_games.mesh_io.io.PrimitiveInputStream;
import com.ripplar_games.mesh_io.io.PrimitiveOutputStream;
import com.ripplar_games.mesh_io.mesh.IMesh;
import com.ripplar_games.mesh_io.mesh.ImmutableMeshBuilder;
import com.ripplar_games.mesh_io.mesh.MeshType;
import com.ripplar_games.mesh_io.vertex.VertexFormat;

public class MeshIO {
    private static final Logger LOGGER = Logger.getLogger(MeshIO.class.getName());

    private final Map<String, IMeshFormat> extensionFormats = new HashMap<String, IMeshFormat>();

    public MeshIO() {
        registerMeshFormat(new PlyFormatAscii_1_0());
        registerMeshFormat(new ObjFormat());
        registerMeshFormat(new MbMshFormat());
    }

    public void registerMeshFormat(IMeshFormat meshFormat) {
        extensionFormats.put(meshFormat.getFileExtension(), meshFormat);
    }

    public IMesh readQuietly(String filePath, MeshType meshType, IndicesDataType<?> indicesDataType, Set<VertexFormat> formats) {
        ImmutableMeshBuilder builder = new ImmutableMeshBuilder(meshType, indicesDataType, formats);
        return readQuietly(filePath, builder);
    }

    public <T extends IMesh> T readQuietly(String filePath, IMeshBuilder<T> builder) {
        try {
            read(filePath, builder);
            return builder.build();
        } catch (MeshIOException e) {
            LOGGER.log(Level.SEVERE, "Failed to read mesh");
            return null;
        }
    }

    public <T extends IMesh> T read(String filePath, IMeshBuilder<T> builder) throws MeshIOException {
        requireNotNull(filePath, "file path");
        requireNotNull(builder, "mesh builder");
        IMeshFormat format = getFormatFromFilePath(filePath);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(filePath);
            BufferedInputStream bis = new BufferedInputStream(fis);
            PrimitiveInputStream pis = new PrimitiveInputStream(bis);
            builder.clear();
            format.read(builder, pis);
            return builder.build();<commit_msg>Refactorings<commit_after>package com.ripplar_games.mesh_io;

import java.io.BufferedInputStream;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.ripplar_games.mesh_io.formats.mbmsh.MbMshFormat;
import com.ripplar_games.mesh_io.formats.obj.ObjFormat;
import com.ripplar_games.mesh_io.formats.ply.PlyFormatAscii_1_0;
import com.ripplar_games.mesh_io.index.IndicesDataType;
import com.ripplar_games.mesh_io.io.PrimitiveInputStream;
import com.ripplar_games.mesh_io.io.PrimitiveOutputStream;
import com.ripplar_games.mesh_io.mesh.IMesh;
import com.ripplar_games.mesh_io.mesh.ImmutableMeshBuilder;
import com.ripplar_games.mesh_io.mesh.MeshType;
import com.ripplar_games.mesh_io.vertex.VertexFormat;

public class MeshIO {
    private static final Logger LOGGER = Logger.getLogger(MeshIO.class.getName());

    private final Map<String, IMeshFormat> extensionFormats = new HashMap<String, IMeshFormat>();

    public MeshIO() {
        registerMeshFormat(new PlyFormatAscii_1_0());
        registerMeshFormat(new ObjFormat());
        registerMeshFormat(new MbMshFormat());
    }

    public void registerMeshFormat(IMeshFormat meshFormat) {
        extensionFormats.put(meshFormat.getFileExtension(), meshFormat);
    }

    public IMesh readAs(String filePath, MeshType meshType, IndicesDataType<?> indicesDataType, Set<VertexFormat> formats) throws MeshIOException {
        ImmutableMeshBuilder builder = new ImmutableMeshBuilder(meshType, indicesDataType, formats);
        read(filePath, builder);
        return builder.build();
    }

    public <T extends IMesh> T read(String filePath, IMeshBuilder<T> builder) throws MeshIOException {
        requireNotNull(filePath, "file path");
        requireNotNull(builder, "mesh builder");
        IMeshFormat format = getFormatFromFilePath(filePath);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(filePath);
            BufferedInputStream bis = new BufferedInputStream(fis);
            PrimitiveInputStream pis = new PrimitiveInputStream(bis);
            builder.clear();
            format.read(builder, pis);
            return builder.build();
{'fraction_non_alphanumeric': 0.09345425867507887, 'fraction_numerical': 0.0015772870662460567, 'mean_word_length': 4.1294236602628915, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, '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': '1776387', 'n_tokens_mistral': 1521, 'n_tokens_neox': 1478, 'n_words': 294}