| 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,v 1.13 2008/07/21 10:45:40 paultaylor Exp $ | |
| 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 | 2907 | super(identifier, frameBody); |
| 46 | 2907 | } |
| 47 | ||
| 48 | /** | |
| 49 | * Creates a new datatype, with value | |
| 50 | * | |
| 51 | * @param identifier | |
| 52 | * @param frameBody | |
| 53 | */ | |
| 54 | public AbstractString(String identifier, AbstractTagFrameBody frameBody, String value) | |
| 55 | { | |
| 56 | 0 | super(identifier, frameBody, value); |
| 57 | 0 | } |
| 58 | ||
| 59 | /** | |
| 60 | * Copy constructor | |
| 61 | * | |
| 62 | * @param object | |
| 63 | */ | |
| 64 | protected AbstractString(AbstractString object) | |
| 65 | { | |
| 66 | 1650 | super(object); |
| 67 | 1650 | } |
| 68 | ||
| 69 | /** | |
| 70 | * Return the size in bytes of this datatype as it was/is held in file this | |
| 71 | * will be effected by the encoding type. | |
| 72 | * | |
| 73 | * @return the size | |
| 74 | */ | |
| 75 | public int getSize() | |
| 76 | { | |
| 77 | 3559 | return size; |
| 78 | } | |
| 79 | ||
| 80 | /** | |
| 81 | * Sets the size in bytes of this datatype. | |
| 82 | * This is set after writing the data to allow us to recalculate the size for | |
| 83 | * frame header. | |
| 84 | */ | |
| 85 | protected void setSize(int size) | |
| 86 | { | |
| 87 | 3503 | this.size = size; |
| 88 | 3503 | } |
| 89 | ||
| 90 | /** | |
| 91 | * Return String representation of datatype | |
| 92 | * | |
| 93 | * @return a string representation of the value | |
| 94 | */ | |
| 95 | public String toString() | |
| 96 | { | |
| 97 | 6 | return (String) value; |
| 98 | } | |
| 99 | ||
| 100 | /** | |
| 101 | * Check the value can be encoded with the specified encoding | |
| 102 | */ | |
| 103 | public boolean canBeEncoded() | |
| 104 | { | |
| 105 | //Try and write to buffer using the CharSet defined by the textEncoding field (note if using UTF16 we dont | |
| 106 | //need to worry about LE,BE at this point it makes no difference) | |
| 107 | 1165 | byte textEncoding = this.getBody().getTextEncoding(); |
| 108 | 1165 | String charSetName = TextEncoding.getInstanceOf().getValueForId(textEncoding); |
| 109 | 1165 | CharsetEncoder encoder = Charset.forName(charSetName).newEncoder(); |
| 110 | ||
| 111 | 1165 | if (encoder.canEncode((String) value)) |
| 112 | { | |
| 113 | 1148 | return true; |
| 114 | } | |
| 115 | else | |
| 116 | { | |
| 117 | 17 | logger.finest("Failed Trying to decode" + value + "with" + encoder.toString()); |
| 118 | 17 | return false; |
| 119 | } | |
| 120 | } | |
| 121 | } |