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

org.vesalainen.nio.file.attribute.UserDefinedFileAttributes Maven / Gradle / Ivy

/*
 * Copyright (C) 2016 tkv
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.vesalainen.nio.file.attribute;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author tkv
 */
public class UserDefinedFileAttributes implements UserDefinedAttributes
{
    private final UserDefinedFileAttributeView view;
    private ByteBuffer bb;

    public UserDefinedFileAttributes(File file, int maxSize, LinkOption... options)
    {
        this(file.toPath(), maxSize, options);
    }
    public UserDefinedFileAttributes(Path path, int maxSize, LinkOption... options)
    {
        this(Files.getFileAttributeView(path, UserDefinedFileAttributeView.class, options), maxSize);
    }
    public UserDefinedFileAttributes(UserDefinedFileAttributeView view, int maxSize)
    {
        this.view = view;
        this.bb = ByteBuffer.allocate(maxSize);
    }

    @Override
    public boolean has(String name) throws IOException
    {
        return view.list().contains(name);
    }
    @Override
    public Collection list() throws IOException
    {
        return view.list();
    }
    @Override
    public void deleteAll() throws IOException
    {
        for (String name : list())
        {
            delete(name);
        }
    }
    @Override
    public void delete(String name) throws IOException
    {
        view.delete(name);
    }
    @Override
    public int size(String name) throws IOException
    {
        return view.size(name);
    }
    @Override
    public int write(String name, ByteBuffer src) throws IOException
    {
        return view.write(name, src);
    }
    @Override
    public int read(String name, ByteBuffer dst) throws IOException
    {
        return view.read(name, dst);
    }
    @Override
    public void set(String name, byte[] array) throws IOException
    {
        set(name, array, 0, array.length);
    }
    @Override
    public void set(String name, byte[] array, int offset, int length) throws IOException
    {
        bb.clear();
        bb.put(array, offset, length);
        bb.flip();
        view.write(name, bb);
    }
    @Override
    public byte[] get(String name) throws IOException
    {
        byte[] arr = new byte[size(name)];
        ByteBuffer wrap = ByteBuffer.wrap(arr);
        view.read(name, wrap);
        return arr;
    }
    @Override
    public boolean arraysEquals(String name, byte[] array) throws IOException
    {
        return arraysEquals(name, array, 0, array.length);
    }
    @Override
    public boolean arraysEquals(String name, byte[] array, int offset, int length) throws IOException
    {
        if (length != size(name))
        {
            return false;
        }
        bb.clear();
        view.read(name, bb);
        bb.flip();
        for (int ii=0;ii




© 2015 - 2024 Weber Informatics LLC | Privacy Policy