libevent: identifikace serverových soketů a jednotlivých spojení (pomocí čísel FD)
1 package cz.frantovo.příklady;
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 import java.awt.GridLayout;
18 import java.awt.HeadlessException;
19 import java.awt.event.ActionEvent;
20 import java.awt.event.ActionListener;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import javax.swing.JButton;
25 import javax.swing.JFrame;
26 import javax.swing.JLabel;
27 import javax.swing.JTextField;
30 * @author Ing. František Kučera (frantovo.cz)
32 public class Caesar extends JFrame {
34 private JTextField prostýText = new JTextField();
35 private JTextField šifrovanýText = new JTextField();
36 private JButton šifruj = new JButton("Šifruj");
37 private JButton dešifruj = new JButton("Dešifruj");
38 private static final char[] ABECEDA_ZNAKY = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
39 private static final List<Character> ABECEDA;
40 private static final int SKOK = 3;
43 List<Character> abeceda = new ArrayList<>();
44 for (char z : ABECEDA_ZNAKY) {
47 ABECEDA = Collections.unmodifiableList(abeceda);
50 public static void main(String[] args) {
51 Caesar c = new Caesar();
55 public Caesar() throws HeadlessException {
56 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
57 setTitle("Caesarova šifra");
58 setLocationRelativeTo(null);
59 setLayout(new GridLayout(3, 2));
61 add(new JLabel("Prostý text"));
64 add(new JLabel("Šifrovaný text"));
70 šifruj.addActionListener(new ActionListener() {
72 public void actionPerformed(ActionEvent e) {
73 šifrovanýText.setText(posuň(prostýText.getText(), SKOK));
77 dešifruj.addActionListener(new ActionListener() {
79 public void actionPerformed(ActionEvent e) {
80 prostýText.setText(posuň(šifrovanýText.getText(), -SKOK));
87 private static String posuň(String text, int oKolik) {
88 StringBuilder posunutý = new StringBuilder(text.length());
90 for (char z : text.toCharArray()) {
91 posunutý.append(posuň(z, oKolik));
94 return posunutý.toString();
97 private static char posuň(char znak, int oKolik) {
98 int původníPozice = ABECEDA.indexOf(znak);
100 if (původníPozice < 0) {
103 int nováPozice = (původníPozice + oKolik) % ABECEDA_ZNAKY.length;
104 nováPozice = nováPozice < 0 ? ABECEDA_ZNAKY.length + nováPozice : nováPozice;
105 return ABECEDA.get(nováPozice);