Class AppleUtil
- java.lang.Object
-
- com.webcodepro.applecommander.util.AppleUtil
-
public class AppleUtil extends java.lang.Object
This class contains helper methods for dealing with Apple2 data.Date created: Oct 5, 2002 4:16:16 PM
-
-
Constructor Summary
Constructors Constructor Description AppleUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
changeImageOrderByBlock(ImageOrder sourceOrder, ImageOrder targetOrder)
Change ImageOrder from source order to target order by copying block by block.static void
changeImageOrderByTrackAndSector(ImageOrder sourceOrder, ImageOrder targetOrder)
Change ImageOrder from source order to target order by copying sector by sector.static byte
clearBit(byte byt, int bit)
Clear a specific bit (turn it off).static int
convertFormattedWord(java.lang.String word)
Convert a typical Apple formatted word.static int
get3ByteValue(byte[] buffer, int offset)
Compute the value of a 3 byte value.static byte[]
getApplesoftFloat(double number)
Convert a double number to an Applesoft float.static int
getBitCount(byte byt)
Count the number of bits set in a byte.static java.lang.String
getFormatted3ByteAddress(int addr)
Format a 3 byte value as hexadecimal.static java.lang.String
getFormattedByte(int byt)
Format a byte value as hexadecimal.static java.lang.String
getFormattedWord(int word)
Format a word value as hexadecimal.static java.lang.String
getHexDump(byte[] bytes)
Generate a simple hex dump from the given byte array.static java.lang.String
getHexDump(int address, byte[] bytes)
static long
getLongValue(byte[] buffer, int offset)
Compute the value of a 4 byte value.static java.lang.String
getNiceFilename(java.lang.String filename)
Make a "nice" filename.static java.util.Date
getPascalDate(byte[] buffer, int offset)
Extract a Pascal date from the buffer.
Bits 0-3: month (1-12)
Bits 4-8: day (1-31)
Bits 9-15: year (0-99)static java.lang.String
getPascalString(byte[] buffer, int offset)
Extract a Pascal string from the buffer.static java.util.Date
getProdosDate(byte[] buffer, int offset)
Extract a ProDOS date from the buffer.static java.lang.String
getProdosString(byte[] buffer, int offset)
Extract a ProDOS string from the buffer.static double
getSaneNumber(byte[] buffer, int offset)
Pull a SANE formatted number from the buffer and return it as a Java double datatype.static int
getSignedWordValue(byte[] buffer, int offset)
Compute the signed value of a word.static java.lang.String
getString(byte[] buffer, int offset, int length)
Extract a string from the buffer.static int
getUnsignedByte(byte value)
Extract out an unsigned byte as an int.static int
getUnsignedByte(byte[] buffer, int offset)
Extract out an unsigned byte as an int.static int
getWordValue(byte[] buffer, int offset)
Compute the value of a word.static int
getWordValue(byte low, byte high)
Compute the value of a word.static boolean
isBitSet(byte byt, int bit)
Determine if a specific bit is set.protected static boolean
sameBlocksPerDisk(ImageOrder sourceOrder, ImageOrder targetOrder)
Answers true if the two disks have the same number of blocks per disk.protected static boolean
sameSectorsPerDisk(ImageOrder sourceOrder, ImageOrder targetOrder)
Answers true if the two disks have the same sectors per disk.static void
set3ByteValue(byte[] buffer, int offset, int value)
Set the value of a 3 byte value.static byte
setBit(byte byt, int bit)
Set a specific bit (turn it on).static void
setPascalDate(byte[] buffer, int offset, java.util.Date date)
Set a Pascal data to the buffer.
Bits 0-3: month (1-12)
Bits 4-8: day (1-31)
Bits 9-15: year (0-99)static void
setPascalString(byte[] buffer, int offset, java.lang.String string, int maxLength)
Set a Pascal string into the buffer.static void
setProdosDate(byte[] buffer, int offset, java.util.Date date)
Set a ProDOS date into the buffer.static void
setProdosString(byte[] buffer, int offset, java.lang.String string, int maxLength)
Sets a ProDOS string into the buffer.static void
setString(byte[] buffer, int offset, java.lang.String string)
Create an Apple string that is the same length as the given string.static void
setString(byte[] buffer, int offset, java.lang.String string, int length)
Create an Apple string that is space delimited.static void
setString(byte[] buffer, int offset, java.lang.String string, int length, boolean highBitOn)
Create an Apple string that is space delimited.static void
setWordValue(byte[] buffer, int offset, int value)
Set a word value.static byte[]
unpackBytes(byte[] compressedData)
Mimic the Apple IIGS UnPackBytes method call.
-
-
-
Method Detail
-
getWordValue
public static int getWordValue(byte[] buffer, int offset)
Compute the value of a word. Pulls value from buffer given the offset. A word is two bytes, in standard Apple LO/HI format.
-
getWordValue
public static int getWordValue(byte low, byte high)
Compute the value of a word.
-
getSignedWordValue
public static int getSignedWordValue(byte[] buffer, int offset)
Compute the signed value of a word.
-
setWordValue
public static void setWordValue(byte[] buffer, int offset, int value)
Set a word value.
-
get3ByteValue
public static int get3ByteValue(byte[] buffer, int offset)
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.
-
getLongValue
public static long getLongValue(byte[] buffer, int offset)
Compute the value of a 4 byte value. This is specific to DC42 processing. Pulls value from buffer given the offset, MSB first.
-
set3ByteValue
public static void set3ByteValue(byte[] buffer, int offset, int value)
Set the value of a 3 byte value.
-
getUnsignedByte
public static int getUnsignedByte(byte value)
Extract out an unsigned byte as an int. All Java bytes are signed; need to convert to an int and remove the sign.
-
getUnsignedByte
public static int getUnsignedByte(byte[] buffer, int offset)
Extract out an unsigned byte as an int. All Java bytes are signed; need to convert to an int and remove the sign.
-
getBitCount
public static int getBitCount(byte byt)
Count the number of bits set in a byte.
-
isBitSet
public static boolean isBitSet(byte byt, int bit)
Determine if a specific bit is set.
-
setBit
public static byte setBit(byte byt, int bit)
Set a specific bit (turn it on).
-
clearBit
public static byte clearBit(byte byt, int bit)
Clear a specific bit (turn it off).
-
getString
public static java.lang.String getString(byte[] buffer, int offset, int length)
Extract a string from the buffer.
-
setString
public static void setString(byte[] buffer, int offset, java.lang.String string, int length)
Create an Apple string that is space delimited.
-
setString
public static void setString(byte[] buffer, int offset, java.lang.String string, int length, boolean highBitOn)
Create an Apple string that is space delimited.
-
setString
public static void setString(byte[] buffer, int offset, java.lang.String string)
Create an Apple string that is the same length as the given string.
-
getPascalString
public static java.lang.String getPascalString(byte[] buffer, int offset)
Extract a Pascal string from the buffer.
-
setPascalString
public static void setPascalString(byte[] buffer, int offset, java.lang.String string, int maxLength)
Set a Pascal string into the buffer.
-
getPascalDate
public static java.util.Date getPascalDate(byte[] buffer, int offset)
Extract a Pascal date from the buffer.
Bits 0-3: month (1-12)
Bits 4-8: day (1-31)
Bits 9-15: year (0-99)
-
setPascalDate
public static void setPascalDate(byte[] buffer, int offset, java.util.Date date)
Set a Pascal data to the buffer.
Bits 0-3: month (1-12)
Bits 4-8: day (1-31)
Bits 9-15: year (0-99)
-
getProdosString
public static java.lang.String getProdosString(byte[] buffer, int offset)
Extract a ProDOS string from the buffer.
-
setProdosString
public static void setProdosString(byte[] buffer, int offset, java.lang.String string, int maxLength)
Sets a ProDOS string into the buffer.
-
getFormattedByte
public static java.lang.String getFormattedByte(int byt)
Format a byte value as hexadecimal.
-
getFormattedWord
public static java.lang.String getFormattedWord(int word)
Format a word value as hexadecimal.
-
getFormatted3ByteAddress
public static java.lang.String getFormatted3ByteAddress(int addr)
Format a 3 byte value as hexadecimal.
-
convertFormattedWord
public static int convertFormattedWord(java.lang.String word)
Convert a typical Apple formatted word. This is essentially a hex string that may start with a '$' and has 1 - 4 digits.
-
getProdosDate
public static java.util.Date getProdosDate(byte[] buffer, int offset)
Extract a ProDOS date from the buffer.
-
setProdosDate
public static void setProdosDate(byte[] buffer, int offset, java.util.Date date)
Set a ProDOS date into the buffer.
-
getNiceFilename
public static java.lang.String getNiceFilename(java.lang.String filename)
Make a "nice" filename. Some of the Apple ][ file names have characters that are unpalatable - such as "/" or "\" or ":" which are directory separators along with other characters that are not allowed by various operating systems. This method just sanitizes the filename.
-
unpackBytes
public static byte[] unpackBytes(byte[] compressedData)
Mimic the Apple IIGS UnPackBytes method call. The compression is very similar the RLE, but has the following coding conventions:
00xx xxxx = 1 to 64 bytes follow (all different)
01xx xxxx = 3, 5, 6, or 7 repeats of next byte
10xx xxxx = 1 to 64 repeats of next 4 bytes
11xx xxxx = 1 to 64 repeats of next byte taken as 4 bytes
The 6 data bits are stored as length-1; hence 000000 is a length of 1 and 111111 is a length of 64.
-
getSaneNumber
public static double getSaneNumber(byte[] buffer, int offset)
Pull a SANE formatted number from the buffer and return it as a Java double datatype. Fortunately, SANE is the IEEE 754 format which _is_ Java's double datatype. The Double class has an intrinsic longBitsToDouble method to do this. The SANE/IEEE 754 format is setup as such:E SSSSSSSSSSS FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF...F
Where E is the sign bit, S is the exponent bits and F is the fraction bits. The format is discussed within the Double class documentation as around the web. Be aware that the fraction bits are base 2. Meaning that a fraction of .101 is, in reality, a binary fraction. In decimal, this is 1/2 + 0/4 + 1/8 = 5/8 or .625. See http://www.psc.edu/general/software/packages/ieee/ieee.html for an example.
0 1........11 12....................................63
Note: SANE numbers, as stored by AppleWorks are in typical low/high format.
-
getApplesoftFloat
public static byte[] getApplesoftFloat(double number)
Convert a double number to an Applesoft float. This is a 5 byte number. See "Applesoft: Internals" for more details and review the Merlin-generated Applesoft source code.Since the number is 5 bytes long, a float will not work - hence the double. Some precision is lost, but (hopefully) nothing significant!
More specifically, the mapping is as follows:
(Applesoft)
EEEEEEEE SFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
(IEEE 754 - Java)
SEEEEEEE EEEEFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
The mapping will blank the following Double bits:
S000EEEE EEEEFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFF0000 00000000 00000000
-
getHexDump
public static java.lang.String getHexDump(byte[] bytes)
Generate a simple hex dump from the given byte array.This is in the general form of:
MMMMMM: HH HH HH HH HH HH HH HH HH HH HH HH HH HH HH HH AAAAAAAA AAAAAAAA
Where MMMMMM = memory address, HH = hex byte, and A = ASCII character.
-
getHexDump
public static java.lang.String getHexDump(int address, byte[] bytes)
-
changeImageOrderByTrackAndSector
public static void changeImageOrderByTrackAndSector(ImageOrder sourceOrder, ImageOrder targetOrder)
Change ImageOrder from source order to target order by copying sector by sector.
-
sameSectorsPerDisk
protected static boolean sameSectorsPerDisk(ImageOrder sourceOrder, ImageOrder targetOrder)
Answers true if the two disks have the same sectors per disk.
-
changeImageOrderByBlock
public static void changeImageOrderByBlock(ImageOrder sourceOrder, ImageOrder targetOrder)
Change ImageOrder from source order to target order by copying block by block.
-
sameBlocksPerDisk
protected static boolean sameBlocksPerDisk(ImageOrder sourceOrder, ImageOrder targetOrder)
Answers true if the two disks have the same number of blocks per disk.
-
-