package com.google.kosugetti.netcalc; import java.applet.Applet; import java.awt.HeadlessException; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class NetCalc extends Applet { static final long serialVersionUID = 1; JTextField txtIPArea; JTextField txtResult; JButton btnExec; public NetCalc() throws HeadlessException { txtIPArea = new JTextField(""); txtResult = new JTextField(""); btnExec = new JButton("計算"); } public void init() { String param = getParameter("iparea"); if (param != null) { txtIPArea.setText(param); } this.setLayout(new GridLayout(3, 1)); this.add(txtIPArea); this.add(txtResult); this.add(btnExec); btnExec.addActionListener(new BtnExecClicked()); txtIPArea.addKeyListener(new TxtIPAreaTyped()); } public class BtnExecClicked implements ActionListener { public void actionPerformed(ActionEvent e) { execCalcCDIR(); } } public class TxtIPAreaTyped extends KeyAdapter { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { execCalcCDIR(); } } } private void execCalcCDIR() { long startAddress = 0L; long endAddress = 0L; long andValue; int mask = 0; String s = txtIPArea.getText(); StringTokenizer t = new StringTokenizer(s, " .-\t", false); if (t.countTokens() == 8) { try { for (int i = 0; i < 4; i++) { startAddress <<= 8; startAddress += Integer.parseInt(t.nextToken()); } for (int i = 0; i < 4; i++) { endAddress <<= 8; endAddress += Integer.parseInt(t.nextToken()); } andValue = startAddress ^ endAddress; for (mask = 0; mask < 32; mask++) { if ((andValue & 0x01) == 0x00) { break; } andValue >>= 1; } int top = (int)(startAddress & 0xff000000) >> 24; if (top < 0) { top += 256; } String result = Integer.toString(top) + "." + Integer.toString((int)(startAddress & 0x00ff0000) >> 16) + "." + Integer.toString((int)(startAddress & 0x0000ff00) >> 8) + "." + Integer.toString((int)(startAddress & 0x000000ff) ) + "/" + Integer.toString(32 - mask); txtResult.setText(result); txtResult.requestFocus(); txtResult.selectAll(); } catch (NumberFormatException e) { txtResult.setText("IPAddressが不正"); } } else { txtResult.setText("IPAddressが不正"); } } } |
あ、このソースのライセンスは、GPLv2ということで
0 件のコメント:
コメントを投稿