/**
 * Oshi (https://github.com/dblock/oshi)
 * 
 * Copyright (c) 2010 - 2016 The Oshi Project Team
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 * dblock[at]dblock[dot]org
 * alessandro[at]perucchi[dot]org
 * widdis[at]gmail[dot]com
 * https://github.com/dblock/oshi/graphs/contributors
 */
package oshi.hardware.platform.linux;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.json.Json;
import javax.json.JsonBuilderFactory;
import javax.json.JsonObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import oshi.hardware.Display;
import oshi.json.NullAwareJsonObjectBuilder;
import oshi.util.EdidUtil;
import oshi.util.ExecutingCommand;
import oshi.util.ParseUtil;

/**
 * A Display
 * 
 * @author widdis[at]gmail[dot]com
 */
public class LinuxDisplay implements Display {
    private static final Logger LOG = LoggerFactory.getLogger(LinuxDisplay.class);

    private byte[] edid;

    private JsonBuilderFactory jsonFactory = Json.createBuilderFactory(null);

    public LinuxDisplay(byte[] edid) {
        this.edid = edid;
        LOG.debug("Initialized LinuxDisplay");
    }

    @Override
    public byte[] getEdid() {
        return Arrays.copyOf(edid, edid.length);
    }

    @Override
    public JsonObject toJSON() {
        return NullAwareJsonObjectBuilder.wrap(jsonFactory.createObjectBuilder())
                .add("edid", EdidUtil.toString(getEdid())).build();
    }

    /**
     * Gets Display Information
     * 
     * @return An array of Display objects representing monitors, etc.
     */
    public static Display[] getDisplays() {
        List<Display> displays = new ArrayList<Display>();
        ArrayList<String> xrandr = ExecutingCommand.runNative("xrandr --verbose");
        boolean foundEdid = false;
        StringBuilder sb = new StringBuilder();
        for (String s : xrandr) {
            if (s.contains("EDID")) {
                foundEdid = true;
                sb = new StringBuilder();
                continue;
            }
            if (foundEdid) {
                sb.append(s.trim());
                if (sb.length() >= 256) {
                    String edidStr = sb.toString();
                    LOG.debug("Parsed EDID: {}", edidStr);
                    Display display = new LinuxDisplay(ParseUtil.hexStringToByteArray(edidStr));
                    displays.add(display);
                    foundEdid = false;
                }
            }
        }

        return displays.toArray(new Display[displays.size()]);
    }
}
@@ -2043,24 +2043,59 @@
 verbose%22);%0D%0A
+        if (xrandr != null) %7B%0D%0A    
         bool
@@ -2122,24 +2122,28 @@
 e;%0D%0A        
+   
+ 
 StringBuilde
@@ -2179,16 +2179,20 @@
         
+    
 for (Str
@@ -2218,24 +2218,28 @@
             
+  
+  
 if (s.contai
@@ -2261,32 +2261,36 @@
                 
+    
 foundEdid = true
@@ -2308,16 +2308,20 @@
         
+ 
+   
 sb = new
@@ -2331,32 +2331,36 @@
 ringBuilder();%0D%0A
+    
                 
@@ -2374,35 +2374,43 @@
 e;%0D%0A            
-%7D%0D%0A
+    %7D%0D%0A    
             if (
@@ -2439,16 +2439,20 @@
         
+    
 sb.appen
@@ -2477,24 +2477,28 @@
             
+   
+ 
 if (sb.lengt
@@ -2508,24 +2508,28 @@
  %3E= 256) %7B%0D%0A
+    
             
@@ -2581,32 +2581,36 @@
                 
+   
+ 
 LOG.debug(%22Parse
@@ -2629,24 +2629,28 @@
  edidStr);%0D%0A
+    
             
@@ -2755,16 +2755,20 @@
         
+  
+  
 displays
@@ -2795,32 +2795,36 @@
                 
+    
 foundEdid = fals
@@ -2819,32 +2819,55 @@
 dEdid = false;%0D%0A
+                    %7D%0D%0A
                 

Fix NPE with no xrandr command.