java/alt2xml-in-fs/src/cz/frantovo/alt2xml/in/fs/ExtendedAttribute.java
author František Kučera <franta-hg@frantovo.cz>
Wed, 29 Oct 2014 02:03:14 +0100
changeset 106 02739f60b1ec
child 111 e4900596abdb
permissions -rw-r--r--
in-fs: permissions and extended attributes
     1 /**
     2  * Alt2XML
     3  * Copyright © 2014 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, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package cz.frantovo.alt2xml.in.fs;
    19 
    20 import java.nio.ByteBuffer;
    21 import java.nio.charset.Charset;
    22 
    23 public class ExtendedAttribute {
    24 
    25 	private String key;
    26 	private String value;
    27 
    28 	public ExtendedAttribute(String key, String value) {
    29 		this.key = key;
    30 		this.value = value;
    31 	}
    32 
    33 	public ExtendedAttribute(String key, ByteBuffer value) {
    34 		this.key = key;
    35 		setValue(value);
    36 	}
    37 
    38 	public ExtendedAttribute() {
    39 	}
    40 
    41 	public String getKey() {
    42 		return key;
    43 	}
    44 
    45 	public void setKey(String key) {
    46 		this.key = key;
    47 	}
    48 
    49 	public String getValue() {
    50 		return value;
    51 	}
    52 
    53 	public final ByteBuffer getValueBytes() {
    54 		return encode(getValue());
    55 	}
    56 
    57 	public void setValue(String value) {
    58 		this.value = value;
    59 	}
    60 
    61 	public final void setValue(ByteBuffer value) {
    62 		setValue(decode(value));
    63 	}
    64 
    65 	private static String decode(ByteBuffer bytes) {
    66 		bytes.flip();
    67 		return Charset.defaultCharset().decode(bytes).toString();
    68 	}
    69 
    70 	private static ByteBuffer encode(String text) {
    71 		if (text == null) {
    72 			return null;
    73 		} else {
    74 			return Charset.defaultCharset().encode(text);
    75 		}
    76 	}
    77 }