Skip to content

Instantly share code, notes, and snippets.

@ryan-beckett
ryan-beckett / RSA.java
Created August 20, 2012 20:44
Public-key encryption (RSA) - A primitive implementation not meant to be used in production systems, but rather to serve as a learning tool.
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
/**
* An efficient implementation of the RSA public-key cipher. All generated
* exponents used for encryption and decryption are greater than <code>5</code>
* (to secure against basic attacks, e.g. Chinese Remainder Theorem). The
@ryan-beckett
ryan-beckett / AsynchronousParse.java
Last active June 22, 2023 09:27
Example of how to parallelize file tree walk and parse.
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
@ryan-beckett
ryan-beckett / Terminal.java
Created January 12, 2012 06:02
A simple GUI-based non-fuctional terminal emulator
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
@ryan-beckett
ryan-beckett / NotificationHandler.java
Created February 10, 2012 19:51
A facade for receiving notifcations for SMB file modifications through the JCIFS API. Depends on jcifs-1.3.17.jar.
/**
*
* Defines a default handler for notification callbacks.
*
* @author Ryan Beckett
* @version 1.0
*
*/
public abstract class NotificationHandler {
@ryan-beckett
ryan-beckett / ControllerTest.java
Created February 10, 2012 14:14
MVC w/ event handlers and observers
import java.util.*;
public class ControllerTest {
private static Controller controller = Controller.getInstance();
private static View view = new View();
private static LoginHandler loginHandler = new LoginHandler();
public static void main(String[] args) {
@ryan-beckett
ryan-beckett / CommentStripper.java
Created February 10, 2012 11:34
A denomstration of how to strip block-style and inline comments using a finite-state machine. Handles edge cases correctly. You can change the behavior of the class by overriding CommentStripper.doAction(int index).
import java.util.*;
public class CommentStripper {
private StaticTransitionTable transitions;
private StringBuilder sb;
private int index;
private int state;
private final char EOF = '@';
@ryan-beckett
ryan-beckett / JInterpreter.java
Created February 4, 2012 07:06
JInterpreterDemo: A demo displaying how the JavaCompiler API can be used to create an application that executes java code given as runtime input from a user. This gist also includes a facade for the JavaCompiler API and a JInterpreter class for interpreti
import java.io.*;
import java.net.*;
import java.util.*;
/**
* A Java statement interpreter. Add a list of statements with
* <code>newStatement</code> and run them with
* <code>run</code>. After running the interpreter, all
* previously added statements are forgotten.
*/
@ryan-beckett
ryan-beckett / XMLValidator.java
Created October 4, 2012 22:53
Validates XML files against a specified schema or the default WC3 standard.
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
@ryan-beckett
ryan-beckett / LogicSolver.java
Created June 5, 2012 10:13
A quick and dirty implementation that parses and evaluates a logical expression in post-fix form.
import java.util.*;
import java.lang.*;
////////////////////////////////////////////////////////////////
// A quick and dirty implementation that parses and evaluates a
// logical expression in post-fix form. From left to right binary
// expressions are extracted (3 tokens), evaluated, and it's result
// is inserted into its place. This is done until the expression is
// gone. E.g.
//
@ryan-beckett
ryan-beckett / loop.mips
Created May 2, 2012 01:19
A quick looping test in MIPS
.text
main:
li $t4, 1, #t4 = counter
i1: ori $t0, $0, 1000
i2: ori $t1, $0, 2000
i3: addi $t2, $t0, 100
i4: #lw $t3, 0($t1)
i5: #lw $t4, 0($t0)
i6: add $t3, $t3, $t4