dmolony-DiskBrowser/src/com/bytezone/diskbrowser/utilities/Utility.java

96 lines
2.4 KiB
Java
Raw Normal View History

2016-07-29 12:28:11 +00:00
package com.bytezone.diskbrowser.utilities;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
2016-12-07 10:42:01 +00:00
import java.util.Arrays;
import java.util.List;
2016-07-29 12:28:11 +00:00
public class Utility
{
2016-12-07 10:42:01 +00:00
public static final List<String> suffixes =
2018-06-08 12:14:19 +00:00
Arrays.asList ("po", "dsk", "do", "hdv", "2mg", "v2d", "d13", "sdk", "woz");
2016-12-07 10:42:01 +00:00
// not used - it doesn't work with Oracle's JDK
2018-04-25 20:41:03 +00:00
// private static boolean hasRetinaDisplay ()
// {
// Object obj =
// Toolkit.getDefaultToolkit ().getDesktopProperty ("apple.awt.contentScaleFactor");
// if (obj instanceof Float)
// {
// Float f = (Float) obj;
// int scale = f.intValue ();
// return (scale == 2); // 1 indicates a regular mac display.
// }
// return false;
// }
public static boolean test (Graphics2D g)
{
return g.getFontRenderContext ().getTransform ()
.equals (AffineTransform.getScaleInstance (2.0, 2.0));
}
2016-08-03 11:32:47 +00:00
2017-04-26 08:04:48 +00:00
static int getLong (byte[] buffer, int ptr)
{
return getWord (buffer, ptr) + getWord (buffer, ptr + 2) * 0x10000;
}
static int getWord (byte[] buffer, int ptr)
{
int a = (buffer[ptr + 1] & 0xFF) << 8;
int b = buffer[ptr] & 0xFF;
return a + b;
}
2016-08-22 10:54:03 +00:00
public static boolean find (byte[] buffer, byte[] key)
2016-08-03 11:32:47 +00:00
{
for (int i = 0; i < buffer.length; i++)
{
if (buffer[i] == key[0])
{
if (matches (buffer, i, key))
2016-08-22 10:54:03 +00:00
{
2016-08-03 11:32:47 +00:00
System.out.printf ("Matches at %04X%n", i);
2016-08-22 10:54:03 +00:00
return true;
}
2016-08-03 11:32:47 +00:00
}
}
2016-08-22 10:54:03 +00:00
return false;
2016-08-03 11:32:47 +00:00
}
2016-08-08 04:53:29 +00:00
public static boolean matches (byte[] buffer, int offset, byte[] key)
2016-08-03 11:32:47 +00:00
{
int ptr = 0;
while (offset < buffer.length && ptr < key.length)
if (buffer[offset++] != key[ptr++])
return false;
2016-08-08 04:53:29 +00:00
2016-08-03 11:32:47 +00:00
return true;
}
2016-12-07 10:42:01 +00:00
public static int getSuffixNo (String filename)
{
return suffixes.indexOf (getSuffix (filename));
}
public static String getSuffix (String filename)
2016-12-07 10:42:01 +00:00
{
String lcFilename = filename.toLowerCase ();
if (lcFilename.endsWith (".gz"))
lcFilename = lcFilename.substring (0, lcFilename.length () - 3);
else if (lcFilename.endsWith (".zip"))
lcFilename = lcFilename.substring (0, lcFilename.length () - 4);
int dotPos = lcFilename.lastIndexOf ('.');
2016-12-07 10:42:01 +00:00
if (dotPos < 0)
return "";
2016-12-07 10:42:01 +00:00
return lcFilename.substring (dotPos + 1);
}
2016-12-07 10:42:01 +00:00
public static boolean validFileType (String filename)
{
return suffixes.contains (getSuffix (filename));
2016-12-07 10:42:01 +00:00
}
2016-07-29 12:28:11 +00:00
}