/* * AppleCommander - An Apple ][ image utility. * Copyright (C) 2002 by Robert Greene * robgreene at users.sourceforge.net * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package com.webcodepro.applecommander.storage; import java.util.Date; import java.util.GregorianCalendar; /** * This class contains helper methods for dealing with Apple2 data. *
* Date created: Oct 5, 2002 4:16:16 PM
* @author: Rob Greene
*/
public class AppleUtil {
/**
* Compute the value of a word.
* Pulls value from buffer given the offset.
* A word is two bytes, in standard Apple LO/HI format.
*/
public static int getWordValue(byte[] buffer, int offset) {
return getWordValue(buffer[offset], buffer[offset+1]);
}
/**
* Compute the value of a word.
*/
public static int getWordValue(byte low, byte high) {
return getUnsignedByte(low) + getUnsignedByte(high)*256;
}
/**
* Compute the value of a 3 byte value. This may be ProDOS specific.
* Pulls value from buffer given the offset.
* Stored in standard Apple LO/HI format.
*/
public static int get3ByteValue(byte[] buffer, int offset) {
return getUnsignedByte(buffer[offset])
+ getUnsignedByte(buffer[offset+1])*256
+ getUnsignedByte(buffer[offset+2])*65536;
}
/**
* Extract out an unsigned byte as an int.
* All Java bytes are signed; need to convert to an int
* and remove the sign.
*/
public static int getUnsignedByte(byte value) {
return (int) value & 0xff;
}
/**
* Count the number of bits set in a byte.
*/
public static int getBitCount(byte byt) {
int count = 0;
for (int ix=0; ix<8; ix++) {
if (isBitSet(byt, ix)) count++;
}
return count;
}
/**
* Determine if a specific bit is set.
*/
public static boolean isBitSet(byte byt, int bit) {
byte[] masks = { (byte)0x01, (byte)0x02,(byte)0x04, (byte)0x08,
(byte)0x10, (byte)0x20, (byte)0x40, (byte)0x80 };
return (byt & masks[bit]) != 0;
}
/**
* Extract a string from the buffer.
*/
public static String getString(byte[] buffer, int offset, int length) {
byte[] value = new byte[length];
for (int i=0; i