1  /*
     2   * Copyright the original author or authors.
     3   * 
     4   * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   * 
     8   *      http://www.mozilla.org/MPL/MPL-1.1.html
     9   * 
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  import org.as2lib.data.type.Bit;
    18  
    19  /**
    20   * {@code Byte} is represents a byte value.
    21   * 
    22   * <p>{@code Byte} can be used for a different kind of formatting of a byte value.
    23   * It allows to access the value as bit, kilo-bit, mega-bit, giga-bit, tera-bit,
    24   * byte, kilo-byte, mega-byte, giga-byte and tera-byte.
    25   * 
    26   * @author Martin Heidegger
    27   * @version 1.1
    28   */
    29  class org.as2lib.data.type.Byte extends Bit {
    30  	
    31  	/** Shortname of byte. */
    32  	private static var SHORT_BYTE:String = "B";
    33  	
    34  	/** Shortname of kilo-byte. */
    35  	private static var SHORT_KILO_BYTE:String = "KB";
    36  	
    37  	/** Shortname of mega-byte. */
    38  	private static var SHORT_MEGA_BYTE:String = "MB";
    39  	
    40  	/** Shortname of giga-byte. */
    41  	private static var SHORT_GIGA_BYTE:String = "GB";
    42  	
    43  	/** Shortname of tera-byte. */
    44  	private static var SHORT_TERA_BYTE:String = "TB";
    45  	
    46  	/**
    47  	 * Constructs a new {@code Byte}.
    48  	 * 
    49  	 * @param bytes value in byte
    50  	 */
    51  	public function Byte(bytes:Number) {
    52  		super(bytes*BYTE);
    53  	}
    54  	
    55  	/**
    56  	 * Extended toString method for a well formatted byte value.
    57  	 * 
    58  	 * <p>This method uses the next matching size and adds the matching Shortname for it.
    59  	 * 
    60  	 * <p>Examples:
    61  	 * <code>
    62  	 *   new ByteFormat(1).toString(); // 1B
    63  	 *   new ByteFormat(1234).toString(); // 1.21KB
    64  	 *   new ByteFormat(15002344).toString(); // 14.31MB
    65  	 * </code>
    66  	 * 
    67  	 * @return bytes as string with correct ending.
    68  	 * @see BitFormat#DEFAULT_FLOATING_POINTS
    69  	 */
    70  	public function toString():String {
    71  		if(bit < KILO_BYTE) {
    72  			return getBytes()+SHORT_BYTE;
    73  		} else if(bit < MEGA_BYTE) {
    74  			return getKiloBytes()+SHORT_KILO_BYTE;
    75  		} else if(bit < GIGA_BYTE) {
    76  			return getMegaBytes()+SHORT_MEGA_BYTE;
    77  		} else if(bit < TERA_BYTE) {
    78  			return getGigaBytes()+SHORT_GIGA_BYTE;
    79  		} else {
    80  			return getTeraBytes()+SHORT_TERA_BYTE;
    81  		}
    82  	}
    83  }