Code example for BufferedReader

Methods: closeread

0
    final private static int buffSize = 1024 * 50;
 
    public static  GMetricDetail getGMetric(String metricName) throws Exception {
        // Open the socket, get the XML 
        Socket gangliaXMLSocket = new Socket("localhost", 8649);
        BufferedReader in = new BufferedReader(new InputStreamReader(
                gangliaXMLSocket.getInputStream()));
        char[] charBuff = new char[buffSize];
        int in_buff = in.read(charBuff, 0, buffSize);
 
        System.out.println("in_buff = " + in_buff);
        System.out.println("charBuff length: " + charBuff.length);
 
        if (in_buff != -1) {
            System.out.println("End of file");
        } 
 
        //System.out.println(charBuff); 
 
        // Parse XML 
        CharArrayReader car = new CharArrayReader(charBuff, 0, in_buff); // these two lines have to be here.
        BufferedReader br_car = new BufferedReader(car);
 
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setValidating(true);
 
        XMLReader xmlReader = null;
        SAXParser saxParser = spf.newSAXParser();
        xmlReader = saxParser.getXMLReader();
 
        // Set the ContentHandler of the XMLReader 
        MyXMLHandler handler = new MyXMLHandler( metricName );
        xmlReader.setContentHandler(handler);
 
        // Set an ErrorHandler before parsing 
        // xmlReader.setErrorHandler(new MyErrorHandler(System.err)); 
 
        xmlReader.parse(new InputSource(br_car));
        in.close();
        gangliaXMLSocket.close();
         
        return handler.getDetail() ;
    }