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