marți, 29 septembrie 2015

Java: Check an IPv4 address if it is privarte or loopback address

package test; 

import java.net.InetAddress; 
import java.net.UnknownHostException; 
import java.util.ArrayList; 

public class TestLocalIP { 

    public static void main(String[] args) throws UnknownHostException { 
          
         ArrayList<String> ips = new ArrayList<String>(); 
         ips.add("10.205.23.168"); 
         ips.add("192.168.0.1"); 
         ips.add("127.0.0.1"); 
         ips.add("1.1.1.1"); 

         for (String ip : ips) { 
             InetAddress ad = InetAddress.getByName(ip); 
             System.out.println("\n\n" + ip); 
             System.out.println("\n\tLocal Site: " + ad.isSiteLocalAddress());
             System.out.println("\n\tLoopback: " + ad.isLoopbackAddress()); 
        } 
         
    } 

}

joi, 24 septembrie 2015

Java: XML Parser Sample

package test; 

import java.io.*; 

import javax.xml.parsers.*; 

import org.w3c.dom.Document; 
import org.w3c.dom.NodeList; 
import org.xml.sax.SAXException; 

public class TestXMLParser { 

    public static void main(String[] args) { 
        String xml = "<superflow name=\"BreakingPoint ClientSim HTTP Slow Post\" class=\"canned\">\r\n" 
                + "      <label>\r\n" 
                + "        <string>ClientSim HTTP Slow POST</string>\r\n" 
                + "      </label>\r\n" 
                + "      <description>\r\n" 
                + "        <string>This Super Flow enacts an HTTP POST DDoS attack by exploiting a web server's support for users with slow or intermittent connections. The Content-Length header contains a message body size that will be fulfilled over several TCP packets.[RFC 2616]</string>\r\n" 
                + "      </description>\r\n" 
                + "    </superflow>"
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
        InputStream is = new ByteArrayInputStream(xml.getBytes()); 
        try { 

            // Using factory get an instance of document builder 
            DocumentBuilder db = dbf.newDocumentBuilder(); 

            // parse using builder to get DOM representation of the XML file 
            Document dom = db.parse(is); 
            NodeList stringNodes = dom.getElementsByTagName("string"); 
            String superflowName = dom.getElementsByTagName("superflow").item(0).getAttributes().getNamedItem("name").getNodeValue().replace("\n"""), 
                   superflowLabel = stringNodes.item(0).getTextContent(), 
                   superflowDescription =stringNodes.item(1).getTextContent(); 
            System.out.println(superflowName); 
            System.out.println(superflowLabel); 
            System.out.println(superflowDescription); 
        } catch (ParserConfigurationException pce) { 
            pce.printStackTrace(); 
        } catch (SAXException se) { 
            se.printStackTrace(); 
        } catch (IOException ioe) { 
            ioe.printStackTrace(); 
        } 
    } 

miercuri, 23 septembrie 2015

The Virtutes (after Benjamin Franklin)

1. Temperance. Eat not to dullness; drink not to elevation.
2. Silence. Speak not but what may benefit others or yourself; avoid trifling conversation.
3. Order. Let all your things have their places; let each part of your business have its time.
4. Resolution. Resolve to perform what you ought; perform without fail what you resolve.
5. Frugality. Make no expense but to do good to others or yourself; i. e. , waste nothing.
6. Industry. Lose no time; be always employ'd in something useful; cut off all unnecessary actions.
7. Sincerity. Use no hurtful deceit; think innocently and justly; and, if you speak, speak accordingly.
8. Justice. Wrong none by doing injuries, or omitting the benefits that are your duty.
9. Moderation. Avoid extremes; forbear resenting injuries so much as you think they deserve.
10. Cleanliness. Tolerate no uncleanliness in body, clothes, or habitation.
11. Tranquility. Be not disturbed at trifles, or at accidents common or unavoidable.
12. Chastity.

13. Humility. Imitate Jesus and Socrates.

vineri, 4 septembrie 2015

Java: Dispose Sample


public class ShapesSample extends Shape { 
    Cicle c; 
    Square s; 
    public ShapesSample(int i) {  
        super(i); 
        c = new Cicle(i); 
        s = new Square(i); 
        System.out.println("ShapesSample ctor"); 
    } 
     
    @Override 
    void dispose() { 
        s.dispose(); 
        c.dispose(); 
        System.out.println("ShapesSample dispose"); 
        super.dispose(); 
    } 
     
    public static void main(String[] args) { 
        ShapesSample sample = new ShapesSample(0); 
        try { 
             
        } 
        finally { 
            sample.dispose(); 
        } 
    } 


class Shape { 
    Shape(int i) { 
        System.out.println("Shape ctor"); 
    } 
     
    void dispose() { 
        System.out.println("Shape dispose"); 
    } 


class Cicle extends Shape { 
    Cicle(int i) { 
        super(i); 
        System.out.println("Cicle ctor"); 
    } 
     
    @Override 
    void dispose() { 
        System.out.println("Cicle dispose"); 
        super.dispose(); 
    } 


class Square extends Shape { 
    Square(int i) { 
        super(i); 
        System.out.println("Square ctor"); 
    } 
    @Override 
    void dispose() { 
        System.out.println("Square dispose"); 
        super.dispose(); 
    }