diff -r 41e91ea94acb -r ec0e970e0830 src/main/java/cz/frantovo/rozsireneatributy/Atribut.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/cz/frantovo/rozsireneatributy/Atribut.java Sat Dec 16 20:13:13 2023 +0100 @@ -0,0 +1,97 @@ +/** + * Rozšířené atributy – program na správu rozšířených atributů souborů + * Copyright © 2012 František Kučera (frantovo.cz) + * + * 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. + * + * 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 cz.frantovo.rozsireneatributy; + +import java.nio.ByteBuffer; +import java.nio.charset.Charset; + +/** + * @author Ing. František Kučera (frantovo.cz) + */ +public class Atribut { + + private String klíč; + private String hodnota; + + public Atribut(String klíč, String hodnota) { + this.klíč = klíč; + this.hodnota = hodnota; + } + + public Atribut(String klíč, ByteBuffer hodnota) { + this.klíč = klíč; + setHodnota(hodnota); + } + + public Atribut() { + } + + public String getKlíč() { + return klíč; + } + + public void setKlíč(String klíč) { + this.klíč = klíč; + } + + /** + * Název atributu musí být nenulový a mít nějakou délku, aby šel uložit + * TODO: další kontroly? + * @return jestli je platný + */ + public boolean isPlatnýKlíč() { + return klíč != null && klíč.length() > 0; + } + + /** + * nulová hodnota → smazání atributu + * (ale může být prázdný řetězec) + * @return jestli je platná + */ + public boolean isPlatnáHodnota() { + return hodnota != null; + } + + public String getHodnota() { + return hodnota; + } + + public final ByteBuffer getHodnotaBajty() { + return zakóduj(getHodnota()); + } + + public void setHodnota(String hodnota) { + this.hodnota = hodnota; + } + + public final void setHodnota(ByteBuffer hodnota) { + setHodnota(dekóduj(hodnota)); + } + + private static String dekóduj(ByteBuffer bajty) { + bajty.flip(); + return Charset.defaultCharset().decode(bajty).toString(); + } + + private static ByteBuffer zakóduj(String text) { + if (text == null) { + return null; + } else { + return Charset.defaultCharset().encode(text); + } + } +}