Example usage for java.nio.file.attribute UserDefinedFileAttributeView read

List of usage examples for java.nio.file.attribute UserDefinedFileAttributeView read

Introduction

In this page you can find the example usage for java.nio.file.attribute UserDefinedFileAttributeView read.

Prototype

int read(String name, ByteBuffer dst) throws IOException;

Source Link

Document

Read the value of a user-defined attribute into a buffer.

Usage

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("C:/home/docs/users.txt");

    UserDefinedFileAttributeView view = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class);
    view.write("publishable", Charset.defaultCharset().encode("true"));
    System.out.println("Publishable set");

    String name = "publishable";
    ByteBuffer buffer = ByteBuffer.allocate(view.size(name));
    view.read(name, buffer);
    buffer.flip();//  w  w  w  .  jav a  2  s .  c  o m
    String value = Charset.defaultCharset().decode(buffer).toString();
    System.out.println(value);

}

From source file:org.italiangrid.storm.webdav.fs.attrs.DefaultExtendedFileAttributesHelper.java

protected String getAttributeValue(UserDefinedFileAttributeView view, String attributeName) throws IOException {

    if (view.list().contains(attributeName)) {
        ByteBuffer buffer = ByteBuffer.allocateDirect(view.size(attributeName));
        view.read(attributeName, buffer);
        buffer.flip();//w  w  w .j a  va 2s .  c om
        return StandardCharsets.UTF_8.decode(buffer).toString();
    } else {
        return "";
    }
}

From source file:org.jclouds.kinetic.strategy.internal.KineticStorageStrategyImpl.java

private Blob createBlobFromByteSource(final String container, final String key, final ByteSource byteSource) {
    BlobBuilder builder = blobBuilders.get();
    builder.name(key);// www  .  ja  v a 2s  .co  m
    File file = getFileForBlobKey(container, key);
    try {
        String cacheControl = null;
        String contentDisposition = null;
        String contentEncoding = null;
        String contentLanguage = null;
        String contentType = null;
        HashCode hashCode = null;
        Date expires = null;
        ImmutableMap.Builder<String, String> userMetadata = ImmutableMap.builder();

        UserDefinedFileAttributeView view = getUserDefinedFileAttributeView(file.toPath());
        if (view != null) {
            Set<String> attributes = ImmutableSet.copyOf(view.list());

            cacheControl = readStringAttributeIfPresent(view, attributes, XATTR_CACHE_CONTROL);
            contentDisposition = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_DISPOSITION);
            contentEncoding = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_ENCODING);
            contentLanguage = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_LANGUAGE);
            contentType = readStringAttributeIfPresent(view, attributes, XATTR_CONTENT_TYPE);
            if (contentType == null && autoDetectContentType) {
                contentType = probeContentType(file.toPath());
            }
            if (attributes.contains(XATTR_CONTENT_MD5)) {
                ByteBuffer buf = ByteBuffer.allocate(view.size(XATTR_CONTENT_MD5));
                view.read(XATTR_CONTENT_MD5, buf);
                hashCode = HashCode.fromBytes(buf.array());
            }
            if (attributes.contains(XATTR_EXPIRES)) {
                ByteBuffer buf = ByteBuffer.allocate(view.size(XATTR_EXPIRES));
                view.read(XATTR_EXPIRES, buf);
                buf.flip();
                expires = new Date(buf.asLongBuffer().get());
            }
            for (String attribute : attributes) {
                if (!attribute.startsWith(XATTR_USER_METADATA_PREFIX)) {
                    continue;
                }
                String value = readStringAttributeIfPresent(view, attributes, attribute);
                userMetadata.put(attribute.substring(XATTR_USER_METADATA_PREFIX.length()), value);
            }

            builder.payload(byteSource).cacheControl(cacheControl).contentDisposition(contentDisposition)
                    .contentEncoding(contentEncoding).contentLanguage(contentLanguage)
                    .contentLength(byteSource.size()).contentMD5(hashCode).contentType(contentType)
                    .expires(expires).userMetadata(userMetadata.build());
        } else {
            builder.payload(byteSource).contentLength(byteSource.size())
                    .contentMD5(byteSource.hash(Hashing.md5()).asBytes());
        }
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    Blob blob = builder.build();
    blob.getMetadata().setContainer(container);
    blob.getMetadata().setLastModified(new Date(file.lastModified()));
    blob.getMetadata().setSize(file.length());
    if (blob.getPayload().getContentMetadata().getContentMD5() != null)
        blob.getMetadata()
                .setETag(base16().lowerCase().encode(blob.getPayload().getContentMetadata().getContentMD5()));
    return blob;
}

From source file:org.jclouds.kinetic.strategy.internal.KineticStorageStrategyImpl.java

/** Read the String representation of kinetic attribute, or return null if not present. */
private static String readStringAttributeIfPresent(UserDefinedFileAttributeView view, Set<String> attributes,
        String name) throws IOException {
    if (!attributes.contains(name)) {
        return null;
    }/*from  w w  w.j  av a  2 s.  c o  m*/
    ByteBuffer buf = ByteBuffer.allocate(view.size(name));
    view.read(name, buf);
    return new String(buf.array(), StandardCharsets.UTF_8);
}

From source file:org.ms123.common.git.FileObject.java

public String getString(String key) throws IOException {
    Path path = getPath();/*www  .  j  a va  2 s. c  om*/
    UserDefinedFileAttributeView view = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class);
    ByteBuffer bb = ByteBuffer.allocateDirect(64);
    int num = view.read(key, bb);
    bb.flip();
    return Charset.defaultCharset().decode(bb).toString();
}