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

85 lines
2.1 KiB
Java
Raw Normal View History

2016-07-29 12:28:11 +00:00
package com.bytezone.diskbrowser.utilities;
import java.awt.Graphics2D;
2016-07-29 12:28:11 +00:00
import java.awt.Toolkit;
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 =
2017-03-20 02:50:41 +00:00
Arrays.asList ("po", "dsk", "do", "hdv", "2mg", "v2d", "d13", "sdk");
2016-12-07 10:42:01 +00:00
// not used - it doesn't work with Oracle's JDK
private static boolean hasRetinaDisplay ()
2016-07-29 12:28:11 +00:00
{
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
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
}