java/alt2xml-in-fs/src/cz/frantovo/alt2xml/in/fs/ExtendedAttribute.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:56:03 +0200
changeset 111 e4900596abdb
parent 106 02739f60b1ec
permissions -rw-r--r--
fix license version: GNU GPLv3
     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, 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.alt2xml.in.fs;
    18 
    19 import java.nio.ByteBuffer;
    20 import java.nio.charset.Charset;
    21 
    22 public class ExtendedAttribute {
    23 
    24 	private String key;
    25 	private String value;
    26 
    27 	public ExtendedAttribute(String key, String value) {
    28 		this.key = key;
    29 		this.value = value;
    30 	}
    31 
    32 	public ExtendedAttribute(String key, ByteBuffer value) {
    33 		this.key = key;
    34 		setValue(value);
    35 	}
    36 
    37 	public ExtendedAttribute() {
    38 	}
    39 
    40 	public String getKey() {
    41 		return key;
    42 	}
    43 
    44 	public void setKey(String key) {
    45 		this.key = key;
    46 	}
    47 
    48 	public String getValue() {
    49 		return value;
    50 	}
    51 
    52 	public final ByteBuffer getValueBytes() {
    53 		return encode(getValue());
    54 	}
    55 
    56 	public void setValue(String value) {
    57 		this.value = value;
    58 	}
    59 
    60 	public final void setValue(ByteBuffer value) {
    61 		setValue(decode(value));
    62 	}
    63 
    64 	private static String decode(ByteBuffer bytes) {
    65 		bytes.flip();
    66 		return Charset.defaultCharset().decode(bytes).toString();
    67 	}
    68 
    69 	private static ByteBuffer encode(String text) {
    70 		if (text == null) {
    71 			return null;
    72 		} else {
    73 			return Charset.defaultCharset().encode(text);
    74 		}
    75 	}
    76 }