Code Explainers

Code explainers tagged #resource-management

php
<?php
 
namespace App\Storage;
 

A race-free file counter in PHP

file-locking concurrency atomicity
Intermediate 7 steps
java
public List<User> findUsersByEmailDomain(String domain, int minAge) {
    String sql = """
        SELECT id, username, email, age, created_at
        FROM users

Safe parameterized JDBC queries in Java

jdbc sql-injection prepared-statement
Intermediate 7 steps
java
public List<OrderSummary> streamRecentOrders(LocalDateTime since, Consumer<OrderSummary> handler) {
    String sql = """
        SELECT id, customer_id, total_cents, status, created_at
        FROM orders

Streaming large JDBC result sets safely

jdbc streaming resource-management
Intermediate 7 steps
php
<?php
 
function parseMultipartEmail(string $raw): array
{

Parsing multipart emails with mailparse

mime-parsing email encoding
Intermediate 9 steps
java
public final class ZipExtractor {
 
    private final Path targetDir;
 

Extracting a zip safely in Java

path-traversal streams file-io
Intermediate 8 steps
java
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.http.HttpClient;

Streaming a file download with Java's HttpClient

http-client streaming file-io
Intermediate 6 steps
java
public class OrderRepository {
 
    private static final int BATCH_SIZE = 500;
    private static final String INSERT_SQL =

Batched JDBC inserts with transaction safety

jdbc batching transactions
Intermediate 8 steps
java
public final class FileChecksum {
 
    private static final char[] HEX = "0123456789abcdef".toCharArray();
 

Streaming a SHA-256 file checksum in Java

hashing streams resource-management
Intermediate 7 steps
java
package com.example.fs;
 
import java.io.IOException;
import java.nio.file.FileSystems;

Watching a directory with Java NIO

file-io event-loop resource-management
Intermediate 8 steps
java
package com.example.logs;
 
import java.io.IOException;
import java.io.UncheckedIOException;

Counting HTTP statuses with Java streams

streams regex file-io
Intermediate 6 steps
python
class FileManager:
    """A context manager that safely opens and closes a file."""
 
    def __init__(self, path, mode="r"):

Writing context managers in Python

context-managers resource-management exception-handling
Intermediate 6 steps
java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
 

Java try-with-resources and AutoCloseable

try-with-resources autocloseable resource-management
Intermediate 6 steps
ruby
module FileResource
  # Opens a file, yields it to the caller, and guarantees the
  # handle is closed even if the block raises an exception.
  def self.open(path, mode = "r")

Guaranteeing cleanup with begin/ensure in Ruby

resource-management exception-safety blocks
Intermediate 8 steps