java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/PokusnýVýpis.java
author František Kučera <franta-hg@frantovo.cz>
Wed Dec 15 18:53:00 2010 +0100 (2010-12-15)
changeset 4 8183e063968c
parent 2 java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/PokusnyVypis.java@7cafdce717dd
permissions -rw-r--r--
čeština
     1 package cz.frantovo.rozsireneAtributy;
     2 
     3 import java.io.File;
     4 import java.io.IOException;
     5 import java.nio.ByteBuffer;
     6 import java.nio.charset.Charset;
     7 import java.nio.file.Path;
     8 import java.nio.file.attribute.UserDefinedFileAttributeView;
     9 import java.util.List;
    10 
    11 /**
    12  * http://freedesktop.org/wiki/CommonExtendedAttributes
    13  * http://download.oracle.com/javase/tutorial/essential/io/fileAttr.html#user
    14  * http://today.java.net/pub/a/today/2008/07/03/jsr-203-new-file-apis.html#so-what-is-a-path-really
    15  *
    16  * $ setfattr -n user.fiki.pozdrav -v 'Dobrý den!' pokus.txt
    17  *
    18  * @author fiki
    19  */
    20 public class PokusnýVýpis {
    21 
    22 	/**
    23 	 * Vypíše rozšířené atributy souboru.
    24 	 * @param args pole s jedním prvkem – názvem souboru
    25 	 * @throws IOException
    26 	 */
    27 	public static void main(String[] args) throws IOException {
    28 		File soubor = new File(args[0]);
    29 		Path cesta = soubor.toPath();
    30 		UserDefinedFileAttributeView pohled = cesta.getFileAttributeView(UserDefinedFileAttributeView.class);
    31 		List<String> jménaAtributů = pohled.list();
    32 		for (String jménoAtributu : jménaAtributů) {
    33 			ByteBuffer bajty = ByteBuffer.allocate(pohled.size(jménoAtributu));
    34 			pohled.read(jménoAtributu, bajty);
    35 			String hodnotaAtributu = dekóduj(bajty);
    36 			System.out.println(jménoAtributu + " = " + hodnotaAtributu);
    37 		}
    38 	}
    39 
    40 	private static String dekóduj(ByteBuffer bajty) {
    41 		bajty.flip();
    42 		return Charset.defaultCharset().decode(bajty).toString();
    43 	}
    44 }