| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AbstractString |
|
| 1.2857142857142858;1.286 |
| 1 | /** | |
| 2 | * @author : Paul Taylor | |
| 3 | * @author : Eric Farng | |
| 4 | * | |
| 5 | * Version @version:$Id: AbstractString.java 836 2009-11-12 15:44:07Z paultaylor $ | |
| 6 | * | |
| 7 | * MusicTag Copyright (C)2003,2004 | |
| 8 | * | |
| 9 | * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser | |
| 10 | * General Public License as published by the Free Software Foundation; either version 2.1 of the License, | |
| 11 | * or (at your option) any later version. | |
| 12 | * | |
| 13 | * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | |
| 14 | * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 15 | * See the GNU Lesser General Public License for more details. | |
| 16 | * | |
| 17 | * You should have received a copy of the GNU Lesser General Public License along with this library; if not, | |
| 18 | * you can get a copy from http://www.opensource.org/licenses/lgpl-license.php or write to the Free Software | |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 20 | * | |
| 21 | * Description: | |
| 22 | * | |
| 23 | */ | |
| 24 | package org.jaudiotagger.tag.datatype; | |
| 25 | ||
| 26 | import org.jaudiotagger.tag.id3.AbstractTagFrameBody; | |
| 27 | import org.jaudiotagger.tag.id3.valuepair.TextEncoding; | |
| 28 | ||
| 29 | import java.nio.charset.Charset; | |
| 30 | import java.nio.charset.CharsetEncoder; | |
| 31 | ||
| 32 | /** | |
| 33 | * A partial implementation for String based ID3 fields | |
| 34 | */ | |
| 35 | public abstract class AbstractString extends AbstractDataType | |
| 36 | { | |
| 37 | /** | |
| 38 | * Creates a new datatype | |
| 39 | * | |
| 40 | * @param identifier | |
| 41 | * @param frameBody | |
| 42 | */ | |
| 43 | protected AbstractString(String identifier, AbstractTagFrameBody frameBody) | |
| 44 | { | |
| 45 | 13060 | super(identifier, frameBody); |
| 46 | 13060 | } |
| 47 | ||
| 48 | /** | |
| 49 | * Creates a new datatype, with value | |
| 50 | * | |
| 51 | * @param identifier | |
| 52 | * @param frameBody | |
| 53 | * @param value | |
| 54 | */ | |
| 55 | public AbstractString(String identifier, AbstractTagFrameBody frameBody, String value) | |
| 56 | { | |
| 57 | 8 | super(identifier, frameBody, value); |
| 58 | 8 | } |
| 59 | ||
| 60 | /** | |
| 61 | * Copy constructor | |
| 62 | * | |
| 63 | * @param object | |
| 64 | */ | |
| 65 | protected AbstractString(AbstractString object) | |
| 66 | { | |
| 67 | 7617 | super(object); |
| 68 | 7617 | } |
| 69 | ||
| 70 | /** | |
| 71 | * Return the size in bytes of this datatype as it was/is held in file this | |
| 72 | * will be effected by the encoding type. | |
| 73 | * | |
| 74 | * @return the size | |
| 75 | */ | |
| 76 | public int getSize() | |
| 77 | { | |
| 78 | 15641 | return size; |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * Sets the size in bytes of this datatype. | |
| 83 | * This is set after writing the data to allow us to recalculate the size for | |
| 84 | * frame header. | |
| 85 | * @param size | |
| 86 | */ | |
| 87 | protected void setSize(int size) | |
| 88 | { | |
| 89 | 15391 | this.size = size; |
| 90 | 15391 | } |
| 91 | ||
| 92 | /** | |
| 93 | * Return String representation of datatype | |
| 94 | * | |
| 95 | * @return a string representation of the value | |
| 96 | */ | |
| 97 | public String toString() | |
| 98 | { | |
| 99 | 24 | return (String) value; |
| 100 | } | |
| 101 | ||
| 102 | /** | |
| 103 | * Check the value can be encoded with the specified encoding | |
| 104 | * @return | |
| 105 | */ | |
| 106 | public boolean canBeEncoded() | |
| 107 | { | |
| 108 | //Try and write to buffer using the CharSet defined by the textEncoding field (note if using UTF16 we dont | |
| 109 | //need to worry about LE,BE at this point it makes no difference) | |
| 110 | 5034 | byte textEncoding = this.getBody().getTextEncoding(); |
| 111 | 5034 | String charSetName = TextEncoding.getInstanceOf().getValueForId(textEncoding); |
| 112 | 5034 | CharsetEncoder encoder = Charset.forName(charSetName).newEncoder(); |
| 113 | ||
| 114 | 5034 | if (encoder.canEncode((String) value)) |
| 115 | { | |
| 116 | 4966 | return true; |
| 117 | } | |
| 118 | else | |
| 119 | { | |
| 120 | 68 | logger.finest("Failed Trying to decode" + value + "with" + encoder.toString()); |
| 121 | 68 | return false; |
| 122 | } | |
| 123 | } | |
| 124 | } |