Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

Windows XP System Variables

// Windows XP Default System Variables

%SystemDrive%  		C:

%SystemRoot% 		C:\WINNT, C:\WINDOWS

%SystemDirectory% 	C:\WINNT\System32, C:\WINDOWS\System32

%WinDir% 		C:\WINNT, C:\WINDOWS, C:\WINNT\Program Files

%ComSpec% 		C:\WINNT\system32\cmd.exe

%Temp% 			C:\DOCUME~1\Usr\LOCALS~1\Temp from C:\Documents and Settings\Usr\Local Settings\Temp

%HOMEDRIVE% 		C: The drive letter associated with the user's home directory

%HOMEPATH% 		The path to the user's home directory (excluding drive): \Documents and Settings\Guest

%OS% 			Windows_NT -> The operating system the user is running

%USERDOMAIN% 		The name of the domain that contains the user's account

%USERNAME% 		The user's name

Java: Getting the Name of a Class Object

// Ref: http://www.exampledepot.com/egs/java.lang/GetClassName.html

    // Get the fully-qualified name of a class
    Class cls = java.lang.String.class;
    String name = cls.getName();        // java.lang.String
    
    // Get the fully-qualified name of a inner class
    cls = java.util.Map.Entry.class;
    name = cls.getName();               // java.util.Map$Entry
    
    // Get the unqualified name of a class
    cls = java.util.Map.Entry.class;
    name = cls.getName();
    if (name.lastIndexOf('.') > 0) {
        name = name.substring(name.lastIndexOf('.')+1);  // Map$Entry
    }
    // The $ can be converted to a .
    name = name.replace('$', '.');      // Map.Entry
    
    
    // Get the name of a primitive type
    name = int.class.getName();         // int
    
    // Get the name of an array
    name = boolean[].class.getName();   // [Z
    name = byte[].class.getName();      // [B
    name = char[].class.getName();      // [C
    name = short[].class.getName();     // [S
    name = int[].class.getName();       // [I
    name = long[].class.getName();      // [J
    name = float[].class.getName();     // [F
    name = double[].class.getName();    // [D
    name = String[].class.getName();    // [Ljava.lang.String;
    name = int[][].class.getName();     // [[I
    
    // Get the name of void
    cls = Void.TYPE;
    name = cls.getName();               // void

Java: File System: Getting the path of TMP directory from Environmental Variable

public static String INDEX_FILE_LOCATION = System.getenv("TMP") + File.separator + "dl-index.tmp";

Java - CUT&PASTE

// Cut&Paste Method
package system.clipboard;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;	 

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class ClipBoard extends JFrame implements ClipboardOwner, ActionListener
{
	private static final long serialVersionUID = 1L;
	
	JTextArea srcText, dstText;
	JButton copyButton, pasteButton;
	Clipboard clipboard = getToolkit().getSystemClipboard();

	public ClipBoard()
	{
		super("Clipboard Test");
		
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		
		setLayout(gridbag);
		
		srcText = new JTextArea(8, 32);
		c.gridwidth = 2;
		c.anchor = GridBagConstraints.CENTER;
		gridbag.setConstraints(srcText, c);
		add(srcText);

		copyButton = new JButton("Copy Above");
		copyButton.setActionCommand("copy");
		copyButton.addActionListener(this);
		c.gridy = 1;
		c.gridwidth = 1;
		gridbag.setConstraints(copyButton, c);
		add(copyButton);
		
		pasteButton = new JButton("Paste Below");
		pasteButton.setActionCommand("paste");
		pasteButton.addActionListener(this);
		pasteButton.setEnabled(false);
		c.gridx = 1;
		gridbag.setConstraints(pasteButton, c);
		add(pasteButton);
		
		dstText = new JTextArea(8, 32);
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 2;
		gridbag.setConstraints(dstText, c);
		add(dstText);
		
		pack();
	}
	
	public void actionPerformed(ActionEvent evt)
	{
		String cmd = evt.getActionCommand();
		
		if(cmd.equals("copy")) 
		{
			// Implement Copy operation
			String srcData = srcText.getText();
			
			if(srcData != null)
			{
				StringSelection contents = new StringSelection(srcData);
				clipboard.setContents(contents, this);
				pasteButton.setEnabled(true);
			}
		}
		else if(cmd.equals("paste"))
		{
			// Implement Paste operation
			Transferable content = clipboard.getContents(this);
			if(content != null) 
			{
				try
				{
					String dstData = (String) content.getTransferData(DataFlavor.stringFlavor);
					dstText.append(dstData);
				}
				catch(Exception e)
				{
					System.out.println("Couldn't get contents in format: " + DataFlavor.stringFlavor.getHumanPresentableName());
				}
			}
		}
	}

	public void lostOwnership(Clipboard clipboard, Transferable contents)
	{
		System.out.println("Clipboard contents replaced");
	}
	
	public static void main(String[] args) 
	{
		ClipBoard test = new ClipBoard();
		test.setVisible(true);
	}
}

J2ME - System Properties

// System Properties J2ME

package System;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class SystemProperties extends MIDlet implements CommandListener
{
	private Command esci;
	
	private Display display;
	
	private Form form;
	
	protected void startApp() throws MIDletStateChangeException
	{
		display = Display.getDisplay(this);
		
		form = new Form("System Propiertis");
		form.setCommandListener(this);
		
		esci = new Command("Esci", Command.EXIT, 0);
		form.addCommand(esci);
		
		Runtime rt = Runtime.getRuntime();
		rt.gc(); // Garbage Collection
		
		form.append("Free Memory: " + rt.freeMemory() + "\n");
		form.append("Total Memory: " + rt.totalMemory() + "\n");
		form.append(showProp("microedition.configuration"));
		form.append(showProp("microedition.platform"));
		form.append(showProp("microedition.locale"));
		form.append(showProp("microedition.encoding"));
		form.append(showProp("microedition.encodingClass"));
		form.append(showProp("microedition.http_proxy"));
		
		display.setCurrent(form);
	}

	protected void pauseApp()
	{

	}

	protected void destroyApp(boolean unconditional) throws MIDletStateChangeException
	{
		notifyDestroyed();
	}

	public String showProp(String str)
	{
		String value = System.getProperty(str);
		StringBuffer stringbuffer = new StringBuffer(50);
		
		stringbuffer.setLength(0);
		stringbuffer.append(str);
		stringbuffer.append(" = ");
		
		if(value == null)
			stringbuffer.append("<undefined>");
		else
		{
			stringbuffer.append("\"");
			stringbuffer.append(value);
			stringbuffer.append("\"");
		}
		
		stringbuffer.append("\n");
		
		return stringbuffer.toString();
	}
	
	public void commandAction(Command c, Displayable d)
	{
		if(c == esci)
		{
			try
			{
				destroyApp(true);
			}
			catch(MIDletStateChangeException e)
			{
				showException(e);
			}
		}
	}
	
	public void showException(Exception e)
	{
		Alert alert = new Alert("Errore !!!");
		alert.setString(e.getMessage());
		alert.setType(AlertType.ERROR);
		alert.setTimeout(Alert.FOREVER);
		
		display.setCurrent(alert);
	}
}

Hardware/system Info

List the devices on a FreeBSD machine

pciconf -lv

Find which pids are using which TCP ports

On unix systems: lsof
On windows: command line: Tcpvcon
GUI: TCPView
(can be downloaded from http://www.sysinternals.com/Utilities/TcpView.html)
« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS