<commit_before>package com.ironz.binaryprefs.files;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;


/**
 * Concrete file adapter which implements NIO file operations
 */
// TODO: 12/12/16 create benchmarks for MappedByteBuffer and plain direct ByteBuffer/ByteChannel
// TODO: 13/12/16 implement tests
public class NioFileAdapter implements FileAdapter {

    private final File srcDir;

    public NioFileAdapter(File srcDir) {
        this.srcDir = srcDir;
    }

    @Override
    public String[] names() {
        return srcDir.list();
    }

    @Override
    public byte[] fetch(String name) throws Exception {
        synchronized (NioFileAdapter.class) {
            FileChannel channel = null;
            RandomAccessFile randomAccessFile = null;
            try {
                File file = new File(srcDir, name);
                randomAccessFile = new RandomAccessFile(file, "r");
                channel = randomAccessFile.getChannel();
                int size = (int) randomAccessFile.length();
                MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, size);
                byte[] bytes = new byte[size];
                buffer.get(bytes);
                return bytes;
            } finally {
                try {
                    if (randomAccessFile != null) randomAccessFile.close();
                    if (channel != null) channel.close();
                } catch (IOException ignored) {
                }
            }
        }
    }

    @Override
    public void save(String name, byte[] bytes) throws Exception {
        synchronized (NioFileAdapter.class) {
            FileChannel channel = null;
            RandomAccessFile randomAccessFile = null;
            try {
                File file = new File(srcDir, name);
                randomAccessFile = new RandomAccessFile(file, "rw");
                channel = randomAccessFile.getChannel();
                MappedByteBuffer byteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, bytes.length);
                byteBuffer.put(bytes);
                channel.write(byteBuffer);
            } finally {
                try {
                    if (randomAccessFile != null) randomAccessFile.close();
                    if (channel != null) channel.close();
                } catch (Exception ignored) {
                }
            }
        }
    }

    @Override
    public boolean clear() {
        synchronized (NioFileAdapter.class) {
            boolean allDeleted = true;
            for (File file : srcDir.listFiles()) {
                boolean deleted = file.delete();
                if (!deleted) {
                    allDeleted = false;
                }
            }
            return allDeleted;
        }
    }

    @Override
    public boolean remove(String name) {
        synchronized (NioFileAdapter.class) {
            File file = new File(srcDir, name);
            return file.delete();
        }
    }
}
<commit_msg>NioFileAdapter.java - removed synchronization block for all api method<commit_after>package com.ironz.binaryprefs.files;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;


/**
 * Concrete file adapter which implements NIO file operations
 */
// TODO: 12/12/16 create benchmarks for MappedByteBuffer and plain direct ByteBuffer/ByteChannel
// TODO: 13/12/16 implement tests
public class NioFileAdapter implements FileAdapter {

    private final File srcDir;

    public NioFileAdapter(File srcDir) {
        this.srcDir = srcDir;
    }

    @Override
    public String[] names() {
        return srcDir.list();
    }

    @Override
    public byte[] fetch(String name) throws Exception {
        FileChannel channel = null;
        RandomAccessFile randomAccessFile = null;
        try {
            File file = new File(srcDir, name);
            randomAccessFile = new RandomAccessFile(file, "r");
            channel = randomAccessFile.getChannel();
            int size = (int) randomAccessFile.length();
            MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, size);
            byte[] bytes = new byte[size];
            buffer.get(bytes);
            return bytes;
        } finally {
            try {
                if (randomAccessFile != null) randomAccessFile.close();
                if (channel != null) channel.close();
            } catch (IOException ignored) {
            }
        }
    }

    @Override
    public void save(String name, byte[] bytes) throws Exception {
        FileChannel channel = null;
        RandomAccessFile randomAccessFile = null;
        try {
            File file = new File(srcDir, name);
            randomAccessFile = new RandomAccessFile(file, "rw");
            channel = randomAccessFile.getChannel();
            MappedByteBuffer byteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, bytes.length);
            byteBuffer.put(bytes);
            channel.write(byteBuffer);
        } finally {
            try {
                if (randomAccessFile != null) randomAccessFile.close();
                if (channel != null) channel.close();
            } catch (Exception ignored) {
            }
        }
    }

    @Override
    public boolean clear() {
        boolean allDeleted = true;
        for (File file : srcDir.listFiles()) {
            boolean deleted = file.delete();
            if (!deleted) {
                allDeleted = false;
            }
        }
        return allDeleted;
    }

    @Override
    public boolean remove(String name) {
        File file = new File(srcDir, name);
        return file.delete();
    }
}

{'fraction_non_alphanumeric': 0.09217540842648324, 'fraction_numerical': 0.004815133276010318, 'mean_word_length': 1.8607968519429414, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5396953', 'n_tokens_mistral': 1514, 'n_tokens_neox': 1457, 'n_words': 389}