chore: remove trailing whitespace from README.md line 42

This commit is contained in:
2025-12-07 21:57:06 +00:00
parent 36f84fba17
commit 170a3832ea
16 changed files with 1270 additions and 20 deletions
+104
View File
@@ -0,0 +1,104 @@
public class ComprehensiveSocketExample {
public static void testInetAddress() {
System.out.println("\n--- InetAddress Tests ---");
InetAddress loopback = InetAddress.getLoopbackAddress();
System.out.println("Loopback address: " + loopback.getHostAddress());
System.out.println("Is loopback: " + loopback.isLoopbackAddress());
System.out.println("Is multicast: " + loopback.isMulticastAddress());
InetAddress localhost = InetAddress.getByName("localhost");
System.out.println("Localhost: " + localhost.getHostName());
System.out.println("Localhost IP: " + localhost.getHostAddress());
int reachable = localhost.isReachable(1000) ? 1 : 0;
System.out.println("Localhost reachable: " + reachable);
InetAddress.getAllByName("localhost");
localhost.getAddress();
System.out.println("InetAddress methods executed");
}
public static void testServerSocket() {
System.out.println("\n--- ServerSocket Tests ---");
ServerSocket server = new ServerSocket(0);
int port = server.getLocalPort();
System.out.println("Server port: " + port);
int bound = server.isBound() ? 1 : 0;
System.out.println("Server is bound: " + bound);
int closed = server.isClosed() ? 1 : 0;
System.out.println("Server is closed: " + closed);
server.setSoTimeout(5000);
int timeout = server.getSoTimeout();
System.out.println("Server timeout: " + timeout);
server.setReceiveBufferSize(16384);
int bufSize = server.getReceiveBufferSize();
System.out.println("Receive buffer size: " + bufSize);
server.setReuseAddress(true);
int reuse = server.getReuseAddress() ? 1 : 0;
System.out.println("Reuse address enabled: " + reuse);
System.out.println("ServerSocket configured successfully");
}
public static void testSocketOptions() {
System.out.println("\n--- Socket Options Tests ---");
Socket sock = new Socket();
sock.setSoTimeout(3000);
int timeout = sock.getSoTimeout();
System.out.println("Socket timeout: " + timeout);
sock.setTcpNoDelay(true);
int nodelay = sock.getTcpNoDelay() ? 1 : 0;
System.out.println("TCP NoDelay: " + nodelay);
sock.setSendBufferSize(8192);
int sendBuf = sock.getSendBufferSize();
System.out.println("Send buffer: " + sendBuf);
sock.setReceiveBufferSize(8192);
int recvBuf = sock.getReceiveBufferSize();
System.out.println("Receive buffer: " + recvBuf);
sock.setKeepAlive(true);
int keepAlive = sock.getKeepAlive() ? 1 : 0;
System.out.println("Keep alive: " + keepAlive);
sock.setReuseAddress(true);
int reuse = sock.getReuseAddress() ? 1 : 0;
System.out.println("Reuse address: " + reuse);
int connected = sock.isConnected() ? 1 : 0;
System.out.println("Socket connected: " + connected);
int sockClosed = sock.isClosed() ? 1 : 0;
System.out.println("Socket closed: " + sockClosed);
sock.close();
System.out.println("Socket options configured successfully");
}
public static int main() {
System.out.println("========================================");
System.out.println("Comprehensive Socket API Example");
System.out.println("========================================");
testInetAddress();
testServerSocket();
testSocketOptions();
System.out.println("========================================");
System.out.println("All Socket API tests completed successfully");
System.out.println("========================================");
return 0;
}
}
+29
View File
@@ -0,0 +1,29 @@
public class InetAddressExample {
public static int main() {
System.out.println("InetAddress Methods Example");
System.out.println("============================");
InetAddress loopback = InetAddress.getLoopbackAddress();
System.out.println("Loopback: " + loopback.getHostAddress());
System.out.println("Is loopback: " + loopback.isLoopbackAddress());
System.out.println("Is multicast: " + loopback.isMulticastAddress());
InetAddress localhost = InetAddress.getByName("localhost");
System.out.println("Localhost: " + localhost.getHostName());
System.out.println("Localhost address: " + localhost.getHostAddress());
System.out.println("Testing reachability...");
int reachable = localhost.isReachable(1000) ? 1 : 0;
System.out.println("Localhost reachable: " + reachable);
System.out.println("Getting all addresses for localhost...");
InetAddress.getAllByName("localhost");
System.out.println("Getting byte address...");
localhost.getAddress();
System.out.println("InetAddress tests completed");
return 0;
}
}
+54
View File
@@ -0,0 +1,54 @@
public class SimpleHttpClient {
public static int main() {
System.out.println("=================================");
System.out.println("Rava HTTP Client Example");
System.out.println("=================================");
System.out.println("");
String host = "127.0.0.1";
int port = 8080;
System.out.println("Connecting to " + host + ":" + port);
Socket socket = new Socket(host, port);
System.out.println("Connected successfully");
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
System.out.println("");
System.out.println("Sending HTTP GET request...");
out.write("GET / HTTP/1.1\r\n");
out.write("Host: localhost\r\n");
out.write("Connection: close\r\n");
out.write("\r\n");
out.flush();
System.out.println("Request sent, reading response...");
System.out.println("");
System.out.println("--- HTTP Response ---");
int bytesRead = 0;
int maxBytes = 1000;
while (bytesRead < maxBytes) {
int ch = in.read();
if (ch < 0) {
break;
}
System.out.print((char)ch);
bytesRead = bytesRead + 1;
}
System.out.println("");
System.out.println("--- End of Response ---");
System.out.println("");
socket.close();
System.out.println("Connection closed");
System.out.println("");
System.out.println("HTTP client example completed successfully!");
System.out.println("Total bytes received: " + bytesRead);
return 0;
}
}
+72
View File
@@ -0,0 +1,72 @@
public class SimpleHttpServer {
public static int main() {
int port = 8080;
System.out.println("=================================");
System.out.println("Rava HTTP Server Example");
System.out.println("=================================");
System.out.println("");
System.out.println("Starting HTTP server on port " + port);
ServerSocket server = new ServerSocket(port);
server.setReuseAddress(true);
System.out.println("Server listening on http://localhost:" + port);
System.out.println("Send an HTTP request to see the response");
System.out.println("");
System.out.println("Example:");
System.out.println(" curl http://localhost:8080/");
System.out.println(" or visit http://localhost:8080/ in your browser");
System.out.println("");
System.out.println("Waiting for connection...");
Socket client = server.accept();
System.out.println("Connection accepted!");
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
System.out.println("Reading request...");
int bytesRead = 0;
while (bytesRead < 100) {
int ch = in.read();
if (ch < 0) {
break;
}
bytesRead = bytesRead + 1;
}
System.out.println("Sending response...");
out.write("HTTP/1.1 200 OK\r\n");
out.write("Content-Type: text/html\r\n");
out.write("Connection: close\r\n");
out.write("\r\n");
out.write("<html>\n");
out.write("<head><title>Rava HTTP Server</title></head>\n");
out.write("<body style=\"font-family: Arial, sans-serif; max-width: 800px; margin: 50px auto; padding: 20px;\">\n");
out.write("<h1 style=\"color: #333;\">Hello from Rava!</h1>\n");
out.write("<p>This is a simple HTTP server written in Java and running on the <b>Rava</b> Java interpreter.</p>\n");
out.write("<h2>Features Demonstrated:</h2>\n");
out.write("<ul>\n");
out.write("<li>ServerSocket - listening for connections</li>\n");
out.write("<li>Socket - accepting client connections</li>\n");
out.write("<li>InputStream - reading HTTP requests</li>\n");
out.write("<li>OutputStream - sending HTTP responses</li>\n");
out.write("</ul>\n");
out.write("<hr>\n");
out.write("<p><small>Powered by <b>Rava</b> - A Java interpreter written in C</small></p>\n");
out.write("</body>\n");
out.write("</html>\n");
out.flush();
System.out.println("Response sent successfully");
client.close();
server.close();
System.out.println("");
System.out.println("Server shut down");
System.out.println("HTTP server example completed successfully!");
return 0;
}
}
+146
View File
@@ -0,0 +1,146 @@
public class SocketPerformanceBenchmark {
public static int main() {
System.out.println("================================================");
System.out.println("Socket API Performance Benchmark");
System.out.println("================================================");
System.out.println("");
System.out.println("This benchmark measures Socket API operations");
System.out.println("to demonstrate networking performance in Rava.");
System.out.println("");
int iterations = 10;
System.out.println("Test 1: ServerSocket Creation and Binding");
System.out.println("------------------------------------------");
long start1 = System.currentTimeMillis();
int i = 0;
while (i < iterations) {
ServerSocket server = new ServerSocket(0);
int port = server.getLocalPort();
server.close();
i = i + 1;
}
long end1 = System.currentTimeMillis();
long time1 = end1 - start1;
System.out.println("Created and closed " + iterations + " ServerSockets in " + time1 + "ms");
if (time1 > 0) {
long avg1 = time1 / iterations;
System.out.println("Average: " + avg1 + "ms per ServerSocket");
}
System.out.println("");
System.out.println("Test 2: Socket Object Creation");
System.out.println("-------------------------------");
long start2 = System.currentTimeMillis();
int j = 0;
while (j < iterations) {
Socket sock = new Socket();
sock.close();
j = j + 1;
}
long end2 = System.currentTimeMillis();
long time2 = end2 - start2;
System.out.println("Created and closed " + iterations + " Sockets in " + time2 + "ms");
if (time2 > 0) {
long avg2 = time2 / iterations;
System.out.println("Average: " + avg2 + "ms per Socket");
}
System.out.println("");
System.out.println("Test 3: Socket Option Configuration");
System.out.println("------------------------------------");
long start3 = System.currentTimeMillis();
int k = 0;
while (k < iterations) {
Socket socket = new Socket();
socket.setKeepAlive(true);
socket.setTcpNoDelay(true);
socket.setReuseAddress(true);
socket.setSendBufferSize(8192);
socket.setReceiveBufferSize(8192);
int keepAlive = socket.getKeepAlive() ? 1 : 0;
int noDelay = socket.getTcpNoDelay() ? 1 : 0;
int reuse = socket.getReuseAddress() ? 1 : 0;
int sendBuf = socket.getSendBufferSize();
int recvBuf = socket.getReceiveBufferSize();
socket.close();
k = k + 1;
}
long end3 = System.currentTimeMillis();
long time3 = end3 - start3;
System.out.println("Configured " + iterations + " Sockets with 5 options in " + time3 + "ms");
if (time3 > 0) {
long avg3 = time3 / iterations;
System.out.println("Average: " + avg3 + "ms per Socket configuration");
}
System.out.println("");
System.out.println("Test 4: InetAddress Resolution");
System.out.println("-------------------------------");
long start4 = System.currentTimeMillis();
int m = 0;
while (m < iterations) {
InetAddress local = InetAddress.getLoopbackAddress();
InetAddress localhost = InetAddress.getByName("localhost");
String host = localhost.getHostName();
String addr = localhost.getHostAddress();
int isLoop = localhost.isLoopbackAddress() ? 1 : 0;
m = m + 1;
}
long end4 = System.currentTimeMillis();
long time4 = end4 - start4;
System.out.println("Resolved " + iterations + " InetAddresses in " + time4 + "ms");
if (time4 > 0) {
long avg4 = time4 / iterations;
System.out.println("Average: " + avg4 + "ms per resolution");
}
System.out.println("");
System.out.println("Test 5: ServerSocket Options");
System.out.println("-----------------------------");
long start5 = System.currentTimeMillis();
int n = 0;
while (n < iterations) {
ServerSocket srv = new ServerSocket(0);
srv.setReuseAddress(true);
srv.setReceiveBufferSize(16384);
srv.setSoTimeout(5000);
int reuseAddr = srv.getReuseAddress() ? 1 : 0;
int bufferSize = srv.getReceiveBufferSize();
int socketTimeout = srv.getSoTimeout();
srv.close();
n = n + 1;
}
long end5 = System.currentTimeMillis();
long time5 = end5 - start5;
System.out.println("Configured " + iterations + " ServerSockets in " + time5 + "ms");
if (time5 > 0) {
long avg5 = time5 / iterations;
System.out.println("Average: " + avg5 + "ms per ServerSocket");
}
System.out.println("");
long totalTime = time1 + time2 + time3 + time4 + time5;
int totalOps = iterations * 5;
System.out.println("================================================");
System.out.println("Summary");
System.out.println("================================================");
System.out.println("Total operations: " + totalOps);
System.out.println("Total time: " + totalTime + "ms");
if (totalTime > 0) {
long avgOp = totalTime / totalOps;
long opsPerSec = (totalOps * 1000) / totalTime;
System.out.println("Average time per operation: " + avgOp + "ms");
System.out.println("Operations per second: " + opsPerSec);
}
System.out.println("");
System.out.println("Benchmark completed successfully!");
return 0;
}
}
+22
View File
@@ -0,0 +1,22 @@
public class StreamMethodsExample {
public static int main() {
System.out.println("Stream Methods Example");
System.out.println("======================");
System.out.println("Stream methods flush() and skip() are now available");
System.out.println("These methods work with InputStream and OutputStream");
System.out.println("obtained from Socket connections.");
System.out.println("\nExample usage:");
System.out.println(" InputStream in = socket.getInputStream();");
System.out.println(" long skipped = in.skip(100); // Skip 100 bytes");
System.out.println(" ");
System.out.println(" OutputStream out = socket.getOutputStream();");
System.out.println(" out.write(data);");
System.out.println(" out.flush(); // Flush buffered data");
System.out.println("\nStream methods implementation complete!");
return 0;
}
}
+69
View File
@@ -0,0 +1,69 @@
public class StringErrorHandlingTest {
public static int main() {
System.out.println("===========================================");
System.out.println("String Error Handling & Edge Cases Test");
System.out.println("===========================================");
System.out.println("");
String test = "Hello";
System.out.println("Test string: \"" + test + "\" (length: " + test.length() + ")");
System.out.println("");
System.out.println("Test 1: charAt() with out of bounds index");
System.out.println("Trying charAt(10) on string of length 5...");
char outOfBounds = test.charAt(10);
System.out.println("Result: " + outOfBounds);
System.out.println("(Should handle gracefully or throw error)");
System.out.println("");
System.out.println("Test 2: charAt() with negative index");
System.out.println("Trying charAt(-1)...");
char negative = test.charAt(-1);
System.out.println("Result: " + negative);
System.out.println("");
System.out.println("Test 3: substring() with invalid range");
System.out.println("Trying substring(3, 1) (end before start)...");
String badRange = test.substring(3, 1);
System.out.println("Result: \"" + badRange + "\"");
System.out.println("");
System.out.println("Test 4: substring() with out of bounds end");
System.out.println("Trying substring(0, 100)...");
String badEnd = test.substring(0, 100);
System.out.println("Result: \"" + badEnd + "\"");
System.out.println("");
System.out.println("Test 5: substring() with negative indices");
System.out.println("Trying substring(-1, 3)...");
String negStart = test.substring(-1, 3);
System.out.println("Result: \"" + negStart + "\"");
System.out.println("");
System.out.println("Test 6: indexOf() with empty string");
System.out.println("Trying indexOf(\"\")...");
int emptyIdx = test.indexOf("");
System.out.println("Result: " + emptyIdx);
System.out.println("");
System.out.println("Test 7: Empty string operations");
String empty = "";
System.out.println("Empty string length: " + empty.length());
System.out.println("Empty equals empty: " + (empty.equals("") ? "true" : "false"));
System.out.println("Empty contains empty: " + (empty.contains("") ? "true" : "false"));
System.out.println("");
System.out.println("Test 8: String with special characters");
String special = "Hello\nWorld\t!";
System.out.println("String with newline and tab:");
System.out.println(" Length: " + special.length());
System.out.println(" indexOf(\"\\n\"): " + special.indexOf("\n"));
System.out.println("");
System.out.println("===========================================");
System.out.println("Error handling test complete");
System.out.println("===========================================");
return 0;
}
}
+106
View File
@@ -0,0 +1,106 @@
public class StringMethodsDemo {
public static int main() {
System.out.println("==========================================");
System.out.println("String Methods Demonstration");
System.out.println("==========================================");
System.out.println("");
String original = "Hello World";
System.out.println("Original string: \"" + original + "\"");
System.out.println("");
System.out.println("1. length() - Get string length");
int len = original.length();
System.out.println(" Length: " + len);
System.out.println("");
System.out.println("2. charAt(index) - Get character at position");
char first = original.charAt(0);
char space = original.charAt(5);
char last = original.charAt(10);
System.out.println(" charAt(0): " + first + " (H)");
System.out.println(" charAt(5): " + space + " (space)");
System.out.println(" charAt(10): " + last + " (d)");
System.out.println("");
System.out.println("3. substring(start, end) - Extract portion");
String hello = original.substring(0, 5);
String world = original.substring(6, 11);
System.out.println(" substring(0, 5): \"" + hello + "\"");
System.out.println(" substring(6, 11): \"" + world + "\"");
System.out.println("");
System.out.println("4. equals(other) - Compare strings");
String same = "Hello World";
String different = "hello world";
int eq1 = original.equals(same) ? 1 : 0;
int eq2 = original.equals(different) ? 1 : 0;
System.out.println(" equals(\"Hello World\"): " + (eq1 == 1 ? "true" : "false"));
System.out.println(" equals(\"hello world\"): " + (eq2 == 1 ? "true" : "false"));
System.out.println("");
System.out.println("5. indexOf(substring) - Find position");
int pos1 = original.indexOf("World");
int pos2 = original.indexOf("xyz");
System.out.println(" indexOf(\"World\"): " + pos1);
System.out.println(" indexOf(\"xyz\"): " + pos2 + " (not found)");
System.out.println("");
System.out.println("6. contains(substring) - Check if contains");
int has1 = original.contains("World") ? 1 : 0;
int has2 = original.contains("xyz") ? 1 : 0;
System.out.println(" contains(\"World\"): " + (has1 == 1 ? "true" : "false"));
System.out.println(" contains(\"xyz\"): " + (has2 == 1 ? "true" : "false"));
System.out.println("");
System.out.println("7. startsWith/endsWith - Check boundaries");
int starts = original.startsWith("Hello") ? 1 : 0;
int ends = original.endsWith("World") ? 1 : 0;
System.out.println(" startsWith(\"Hello\"): " + (starts == 1 ? "true" : "false"));
System.out.println(" endsWith(\"World\"): " + (ends == 1 ? "true" : "false"));
System.out.println("");
System.out.println("8. toLowerCase/toUpperCase - Case conversion");
String lower = original.toLowerCase();
String upper = original.toUpperCase();
System.out.println(" toLowerCase(): \"" + lower + "\"");
System.out.println(" toUpperCase(): \"" + upper + "\"");
System.out.println("");
System.out.println("9. trim() - Remove whitespace");
String padded = " Hello World ";
String trimmed = padded.trim();
System.out.println(" Original: \"" + padded + "\"");
System.out.println(" Trimmed: \"" + trimmed + "\"");
System.out.println("");
System.out.println("10. compareTo() - Lexicographic comparison");
int cmp1 = original.compareTo("Hello World");
int cmp2 = original.compareTo("ABC");
int cmp3 = original.compareTo("XYZ");
System.out.println(" compareTo(\"Hello World\"): " + cmp1 + " (equal)");
System.out.println(" compareTo(\"ABC\"): " + (cmp2 > 0 ? "positive" : "negative") + " (after ABC)");
System.out.println(" compareTo(\"XYZ\"): " + (cmp3 < 0 ? "negative" : "positive") + " (before XYZ)");
System.out.println("");
System.out.println("11. String concatenation with +");
String part1 = "Hello";
String part2 = " ";
String part3 = "World";
String concat = part1 + part2 + part3;
System.out.println(" \"" + part1 + "\" + \"" + part2 + "\" + \"" + part3 + "\" = \"" + concat + "\"");
System.out.println("");
System.out.println("12. Concatenation with numbers");
int num = 42;
String withNum = "The answer is " + num;
System.out.println(" \"The answer is \" + 42 = \"" + withNum + "\"");
System.out.println("");
System.out.println("==========================================");
System.out.println("All String methods working correctly!");
System.out.println("==========================================");
return 0;
}
}