java/rozsirene-atributy/src/cz/frantovo/rozsireneatributy/Atribut.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 12 Dec 2023 21:19:11 +0100
branchv_0
changeset 30 d511e4bf7d8f
parent 28 c2ffda907125
permissions -rw-r--r--
volitelné zamykání souborů (POSIX) a zaslání notifikace ostatním aplikacím (inotify/CLOSE_WRITE)
     1 /**
     2  * Rozšířené atributy – program na správu rozšířených atributů souborů
     3  * Copyright © 2012 František Kučera (frantovo.cz)
     4  * 
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License.
     8  * 
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  * 
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  */
    17 package cz.frantovo.rozsireneatributy;
    18 
    19 import java.nio.ByteBuffer;
    20 import java.nio.charset.Charset;
    21 
    22 /**
    23  * @author Ing. František Kučera (frantovo.cz)
    24  */
    25 public class Atribut {
    26 
    27 	private String klíč;
    28 	private String hodnota;
    29 
    30 	public Atribut(String klíč, String hodnota) {
    31 		this.klíč = klíč;
    32 		this.hodnota = hodnota;
    33 	}
    34 
    35 	public Atribut(String klíč, ByteBuffer hodnota) {
    36 		this.klíč = klíč;
    37 		setHodnota(hodnota);
    38 	}
    39 
    40 	public Atribut() {
    41 	}
    42 
    43 	public String getKlíč() {
    44 		return klíč;
    45 	}
    46 
    47 	public void setKlíč(String klíč) {
    48 		this.klíč = klíč;
    49 	}
    50 
    51 	/**
    52 	 * Název atributu musí být nenulový a mít nějakou délku, aby šel uložit
    53 	 * TODO: další kontroly?
    54 	 * @return jestli je platný
    55 	 */
    56 	public boolean isPlatnýKlíč() {
    57 		return klíč != null && klíč.length() > 0;
    58 	}
    59 
    60 	/**
    61 	 * nulová hodnota → smazání atributu
    62 	 * (ale může být prázdný řetězec)
    63 	 * @return jestli je platná
    64 	 */
    65 	public boolean isPlatnáHodnota() {
    66 		return hodnota != null;
    67 	}
    68 
    69 	public String getHodnota() {
    70 		return hodnota;
    71 	}
    72 
    73 	public final ByteBuffer getHodnotaBajty() {
    74 		return zakóduj(getHodnota());
    75 	}
    76 
    77 	public void setHodnota(String hodnota) {
    78 		this.hodnota = hodnota;
    79 	}
    80 
    81 	public final void setHodnota(ByteBuffer hodnota) {
    82 		setHodnota(dekóduj(hodnota));
    83 	}
    84 
    85 	private static String dekóduj(ByteBuffer bajty) {
    86 		bajty.flip();
    87 		return Charset.defaultCharset().decode(bajty).toString();
    88 	}
    89 
    90 	private static ByteBuffer zakóduj(String text) {
    91 		if (text == null) {
    92 			return null;
    93 		} else {
    94 			return Charset.defaultCharset().encode(text);
    95 		}
    96 	}
    97 }