# HG changeset patch # User František Kučera # Date 1292429084 -3600 # Node ID 7cafdce717dd9ee6b8a988e81ae9feaffc53ff0e # Parent ef690c6b88c2eeac019815ccdd14d429970a6c20 Pokusný výpis rozšířených atributů souboru. Příklad použití: $ setfattr -n "user.pokus" -v "ahoj" licence/licence.txt $ setfattr -n "user.čeština" -v "fpohodě – +ěšččíáýéá=" licence/licence.txt $ /opt/jdk1.7.0/bin/java -jar java/rozsirene-atributy/dist/rozsirene-atributy.jar licence/licence.txt diff -r ef690c6b88c2 -r 7cafdce717dd java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/PokusnyVypis.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/rozsirene-atributy/src/cz/frantovo/rozsireneAtributy/PokusnyVypis.java Wed Dec 15 17:04:44 2010 +0100 @@ -0,0 +1,44 @@ +package cz.frantovo.rozsireneAtributy; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.nio.file.Path; +import java.nio.file.attribute.UserDefinedFileAttributeView; +import java.util.List; + +/** + * http://freedesktop.org/wiki/CommonExtendedAttributes + * http://download.oracle.com/javase/tutorial/essential/io/fileAttr.html#user + * http://today.java.net/pub/a/today/2008/07/03/jsr-203-new-file-apis.html#so-what-is-a-path-really + * + * $ setfattr -n user.fiki.pozdrav -v 'Dobrý den!' pokus.txt + * + * @author fiki + */ +public class PokusnyVypis { + + /** + * Vypíše rozšířené atributy souboru. + * @param args pole s jedním prvkem – názvem souboru + * @throws IOException + */ + public static void main(String[] args) throws IOException { + File soubor = new File(args[0]); + Path cesta = soubor.toPath(); + UserDefinedFileAttributeView pohled = cesta.getFileAttributeView(UserDefinedFileAttributeView.class); + List jmenaAtributu = pohled.list(); + for (String jmenoAtributu : jmenaAtributu) { + ByteBuffer bajty = ByteBuffer.allocate(pohled.size(jmenoAtributu)); + pohled.read(jmenoAtributu, bajty); + String hodnotaAtributu = dekoduj(bajty); + System.out.println(jmenoAtributu + " = " + hodnotaAtributu); + } + } + + private static String dekoduj(ByteBuffer bajty) { + bajty.flip(); + return Charset.defaultCharset().decode(bajty).toString(); + } +}