Java Utililty Methods Printer Usage

List of utility methods to do Printer Usage

Description

The list of methods to do Printer Usage are organized into topic(s).

Method

PrinterResolutiongetResolution(PrintService service, PrintRequestAttributeSet set, boolean tryDefaultIfNull)
get Resolution
return (PrinterResolution) getSetting(service, set, PrinterResolution.class, tryDefaultIfNull);
AttributegetSetting(PrintService service, PrintRequestAttributeSet set, Class type, boolean tryDefaultIfNull)
Extracts a setting from the specified PrintRequestAttributeSet or looks up a default value from the specified PrintService if the set doesn't contain it.
Attribute attribute = set.get(type);
if (tryDefaultIfNull && attribute == null && service != null) {
    attribute = (Attribute) service.getDefaultAttributeValue(type);
return attribute;
voidgetSupportedAttributes(PrintService p)
get Supported Attributes
for (Class c : p.getSupportedAttributeCategories()) {
    try {
        Object val = p.getSupportedAttributeValues(c, null, null);
        if (val.getClass().isArray()) {
            for (Object o : (Object[]) val) {
                System.out.println("  - " + o);
        } else {
...
StringgetSupportedAttrName(Class clazz)
Returns the name of the supported attribute based on the given standard attribute category.
return instanceByClass.get(clazz).getName();
booleanisValueSupported(PrintService ps, Attribute attr, DocFlavor flavor, AttributeSet attributes)
is Value Supported
boolean retVal = false;
if (ps == null || !isGoodPrinterName(ps.getName()))
    return retVal;
try {
    retVal = ps.isAttributeValueSupported(attr, flavor, attributes);
} catch (Exception e) {
    retVal = false;
return retVal;
MediaSizelookupMediaSize(final Media media)
lookup Media Size
if (media instanceof MediaSizeName) {
    return MediaSize.getMediaSizeForName((MediaSizeName) media);
} else if (media instanceof MediaName) {
    if (media.equals(MediaName.ISO_A4_TRANSPARENT) || media.equals(MediaName.ISO_A4_WHITE)) {
        return MediaSize.getMediaSizeForName(MediaSizeName.ISO_A4);
    } else if (media.equals(MediaName.NA_LETTER_TRANSPARENT) || media.equals(MediaName.NA_LETTER_WHITE)) {
        return MediaSize.getMediaSizeForName(MediaSizeName.NA_LETTER);
return null;
String[]lookupPrintNames(PrintService[] prtSrvs)
lookup Print Names
int i, n;
int total = 0;
String[] psnames;
int nums = 6;
String name;
if (prtSrvs != null) {
    nums += prtSrvs.length;
psnames = new String[nums];
if (prtSrvs != null) {
    for (n = 0; n < prtSrvs.length; n++) {
        if (prtSrvs[n] != null) {
            name = prtSrvs[n].getName();
            if (name != null) {
                if (isGoodPrinterName(name))
                    psnames[total++] = name;
String cupsPath = new StringBuffer().append(File.separator).append("etc").append(File.separator)
        .append("cups").append(File.separator).append("ppd").toString();
File dir = new File(cupsPath);
File files[] = null;
if (dir.exists() && dir.isDirectory())
    if (dir.canRead())
        files = dir.listFiles();
if (files != null) {
    for (i = 0; i < files.length; i++) {
        if (files[i].isFile()) {
            String fname = files[i].getName();
            if (fname.endsWith(".ppd")) {
                name = fname.substring(0, fname.indexOf(".ppd"));
                if (name.length() > 0) {
                    for (n = 0; n < nums; n++) {
                        if (psnames[n] != null) {
                            if (name.equals(psnames[n])) {
                                name = null;
                                break;
                    if (name != null && total < nums) {
                        psnames[total++] = name;
            if (total >= nums)
                break;
if (total > 0) {
    psList = new String[total];
    for (i = 0; i < total; i++)
        psList[i] = psnames[i];
return psList;
PrintServicelookupPrintService()
lookup Print Service
final PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
if (defaultService != null && defaultService.isDocFlavorSupported(DocFlavor.SERVICE_FORMATTED.PAGEABLE)) {
    return defaultService;
final PrintService printService;
final PrintService[] services = PrintServiceLookup.lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PAGEABLE,
        null);
if (services.length == 0) {
...
PrintService[]lookupPrintServices()
lookup Print Services
if (bWaitForPrinterServer)
    return printSrvs;
bWaitForPrinterServer = true;
if (printSrvs == null || printSrvs.length < 1) {
    long t = System.currentTimeMillis() / 1000;
    if ((t - lastQueryTime_all) < 120) {
        bWaitForPrinterServer = false;
        return printSrvs;
...
booleannotInArray(PrintService key, PrintService[] array)
not In Array
if (key == null)
    return false;
int i = array.length;
while (--i >= 0)
    if (array[i].equals(key))
        return false;
return true;