StringBuilder ucina stringa

0

Witam, pisze małą aplikacje na własny użytek, która używa StringBuildera do sklecenia jednego stringa do wysłania do aplikacji klienckiej. Jednak gdy chce sklecic wyniki metod zwracajacych Stringa StringBuilder skleja jedynie wynik ostatniej metody oraz zwykłe stringi następujące po niej. Oto kod klasy:

package pl.allst.rpi;

import java.io.*;

public class RPIStatus {
	private static String name="";
	private static Runtime r;
	private static Process p;
	private static StringBuilder sb;
	
	public static String getName(){
		if(name == null){
			try{
			r = Runtime.getRuntime();
			Process p = r.exec("uname -a");
			p.waitFor();
			BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
			sb = new StringBuilder();
			sb.append(b.readLine());
			b.close();		
			return sb.toString();
			}catch(IOException | InterruptedException e){
				return e.getStackTrace().toString();
			}
		}else{
			return name;
		}
	}
	public static String getProcLoad(){
		try{
		r = Runtime.getRuntime();
		//CPU INFO 
		p = r.exec(System.getProperty("user.home")+"/.config/RPIStatus/cpuLoad");
		p.waitFor();
		BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
		sb = new StringBuilder();
		sb.append(b.readLine());
		b.close();
		return sb.toString();
		}catch(IOException | InterruptedException e){
			return e.getStackTrace().toString();
		}
	}
	public static String getProcTemp(){
		try{
			r = Runtime.getRuntime();
			//CPU INFO -Temp
			p = r.exec("/opt/vc/bin/vcgencmd measure_temp");
			p.waitFor();
			BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
			sb = new StringBuilder();
			sb.append(b.readLine());
			b.close();
			return sb.toString().substring(5);
			}catch(IOException | InterruptedException e){
				return e.getStackTrace().toString();
			}
		
	}
	public static String getRamTotal(){
		try{
			r = Runtime.getRuntime();
			p = r.exec(System.getProperty("user.home")+"/.config/RPIStatus/cpuLoad");
			p.waitFor();
			BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
			sb = new StringBuilder();
			sb.append(b.readLine());
			b.close();
			String line = sb.toString();
			sb=new StringBuilder();
			
			for(int i=0;i<line.length();i++){
				if(Character.isDigit(line.charAt(i)))
					sb.append(line.charAt(i));
			}
			
			return sb.toString();
			}catch(IOException | InterruptedException e){
				return e.getStackTrace().toString();
			}
		
	}
	public static String getRamUsage(){
		try{
			r = Runtime.getRuntime();
			p = r.exec("cat /proc/meminfo | grep MemAvailable");
			p.waitFor();
			BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
			sb = new StringBuilder();
			sb.append(b.readLine());
			b.close();
			String line = sb.toString();
			sb=new StringBuilder();
			
			for(int i=0;i<line.length();i++){
				if(Character.isDigit(line.charAt(i)))
					sb.append(line.charAt(i));
			}
			
			return sb.toString();
			}catch(IOException | InterruptedException e){
				return e.getStackTrace().toString();
			}
		
	}
	public static String getLoggedPpl(){
		try{
			r = Runtime.getRuntime();
			p = r.exec("who");
			p.waitFor();
			BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
			sb = new StringBuilder("");
			String line="";
			while((line=b.readLine())!=null){
				sb.append(line);
				sb.append("\n");
			}			
			b.close();
			return sb.toString();
			}catch(IOException | InterruptedException e){
				return e.getStackTrace().toString();
			}
		
	}
	public static void initPath(){
		
		File file = new File(System.getProperty("user.home")+"/.config/RPIStatus/cpuLoad");
		if(!file.exists()){
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if(!file.exists()){
				System.out.println("Error: File was not created");
			}
			else{
				try{
				FileWriter writer = new FileWriter(file);
				writer.write("grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}'");
				writer.close();
				r = Runtime.getRuntime();
				p = r.exec("chmod +x "+file.getAbsolutePath());
				p.waitFor();
				}catch(IOException | InterruptedException e){
					e.printStackTrace();
				}
			}
		}
	}
	public static String processInput(String inputLine) {
		if(inputLine.equals("TEST"))
			return "OK";
		else{
			sb=new StringBuilder("");
			sb.append("Name: ");
			sb.append(getName());
			sb.append("\n");
			sb.append("CPUusage: ");
			sb.append(getProcLoad());
			sb.append("\n");
			sb.append("ProcTemp: ");
			sb.append(getProcTemp());
			sb.append("\n");
			sb.append("RamTotal: ");
			sb.append(getRamTotal());
			sb.append("\n");
			sb.append("RamUsage: ");
			sb.append(getRamUsage());
			sb.append("\n");
			sb.append("LoggedPPL: ");
			sb.append(getLoggedPpl());
			sb.append("\n");
			sb.append("Czemu to nie dziala");<--- Efekt desperacji 
			sb.append("Czemu to nie dziala");
			sb.append("Czemu to nie dziala");
			sb.append("Czemu to nie dziala");
			System.out.println(sb.toString());
			return sb.toString();
		}
	}
	
}

A oto output:

pi       :0           2016-03-30 18:39 (:0)
pi       tty1         2016-03-30 18:39
pi       pts/0        2016-04-02 20:38 (185.124.117.143)

Czemu to nie dzialaCzemu to nie dzialaCzemu to nie dzialaCzemu to nie dziala
 

Nie mam pojęcia czemu StringBuilder nie łączy wyników metod. Proszę o pomoc.

1

Nie działa ponieważ w całym programie używasz tego samego obiektu klasy StringBuilder, więc wywołując kolejne funkcje w klasie processInput nadpisujesz to co już masz w sb.
W klasie processInput możesz po prostu zrobić zmienną sb lokalną tak:

public static String processInput(String inputLine) {
        if(inputLine.equals("TEST"))
            return "OK";
        else{     
            StringBuilder sb=new StringBuilder(""); //  <-------------- w tej linii zmiana
            sb.append("Name: ");
            sb.append(getName());
            sb.append("\n");
                     .
                     .
                     .
       }
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1