CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-cdap-cdap--cdap-common

CDAP Common provides core common utilities and abstractions for the CDAP (Cask Data Application Platform) ecosystem including exception handling, service management, configuration, HTTP utilities, metadata management, security abstractions, discovery services, and various utility classes that are shared across CDAP components.

Pending
Overview
Eval results
Files

io.mddocs/

I/O Framework

I/O abstractions including binary encoding/decoding, location management, seekable streams, and various I/O utilities for file and stream processing.

Capabilities

Binary Encoding and Decoding

Utilities for binary data encoding and decoding operations.

/**
 * Binary encoder for writing structured data
 */
public class BinaryEncoder {
    public BinaryEncoder(OutputStream output);
    
    /**
     * Write boolean value
     */
    public void writeBool(boolean value) throws IOException;
    
    /**
     * Write byte value
     */
    public void writeByte(byte value) throws IOException;
    
    /**
     * Write integer value
     */
    public void writeInt(int value) throws IOException;
    
    /**
     * Write long value
     */
    public void writeLong(long value) throws IOException;
    
    /**
     * Write float value
     */
    public void writeFloat(float value) throws IOException;
    
    /**
     * Write double value
     */
    public void writeDouble(double value) throws IOException;
    
    /**
     * Write string value
     */
    public void writeString(String value) throws IOException;
    
    /**
     * Write byte array
     */
    public void writeBytes(byte[] bytes) throws IOException;
    
    /**
     * Flush the encoder
     */
    public void flush() throws IOException;
}

/**
 * Binary decoder for reading structured data
 */
public class BinaryDecoder {
    public BinaryDecoder(InputStream input);
    
    /**
     * Read boolean value
     */
    public boolean readBool() throws IOException;
    
    /**
     * Read byte value
     */
    public byte readByte() throws IOException;
    
    /**
     * Read integer value
     */
    public int readInt() throws IOException;
    
    /**
     * Read long value
     */
    public long readLong() throws IOException;
    
    /**
     * Read float value
     */
    public float readFloat() throws IOException;
    
    /**
     * Read double value
     */
    public double readDouble() throws IOException;
    
    /**
     * Read string value
     */
    public String readString() throws IOException;
    
    /**
     * Read byte array
     */
    public byte[] readBytes() throws IOException;
}

Codec Abstraction

Generic encoding/decoding interface for object serialization.

/**
 * Generic codec interface for encoding/decoding objects
 */
public interface Codec<T> {
    /**
     * Encode object to byte array
     */
    byte[] encode(T object) throws IOException;
    
    /**
     * Decode byte array to object
     */
    T decode(byte[] data) throws IOException;
    
    /**
     * Encode object to output stream
     */
    default void encode(T object, OutputStream output) throws IOException;
    
    /**
     * Decode object from input stream
     */
    default T decode(InputStream input) throws IOException;
}

Location Abstractions

Abstractions for file system and location management.

/**
 * Factory for creating location instances with caching
 */
public class CachingLocationFactory implements LocationFactory {
    public CachingLocationFactory(LocationFactory delegate, int cacheSize);
    
    @Override
    public Location create(String path);
    
    @Override
    public Location create(URI uri);
    
    /**
     * Clear location cache
     */
    public void clearCache();
    
    /**
     * Get cache statistics
     */
    public CacheStats getCacheStats();
}

/**
 * Interface for locations that support linking
 */
public interface LinkableLocation extends Location {
    /**
     * Create a symbolic link to this location
     */
    void createSymbolicLink(Location linkLocation) throws IOException;
    
    /**
     * Create a hard link to this location
     */
    void createHardLink(Location linkLocation) throws IOException;
    
    /**
     * Check if this location is a symbolic link
     */
    boolean isSymbolicLink();
    
    /**
     * Get the target of a symbolic link
     */
    Location getLinkTarget() throws IOException;
}

/**
 * Enumeration of location status types
 */
public enum LocationStatus {
    FILE,
    DIRECTORY,
    SYMBOLIC_LINK,
    DOES_NOT_EXIST;
    
    /**
     * Check if status indicates existence
     */
    public boolean exists();
    
    /**
     * Check if status indicates a file
     */
    public boolean isFile();
    
    /**
     * Check if status indicates a directory
     */
    public boolean isDirectory();
}

Stream Processing

Stream processing abstractions for seekable and advanced stream operations.

/**
 * Abstract seekable input stream
 */
public abstract class SeekableInputStream extends InputStream {
    /**
     * Seek to specific position in stream
     */
    public abstract void seek(long pos) throws IOException;
    
    /**
     * Get current position in stream
     */
    public abstract long getPos() throws IOException;
    
    /**
     * Get total length of stream if known
     */
    public abstract long length() throws IOException;
    
    /**
     * Check if stream supports seeking
     */
    public boolean seekable();
}

/**
 * File-based seekable input stream implementation
 */
public class FileSeekableInputStream extends SeekableInputStream {
    public FileSeekableInputStream(File file) throws FileNotFoundException;
    public FileSeekableInputStream(RandomAccessFile raf);
    
    @Override
    public int read() throws IOException;
    
    @Override
    public int read(byte[] b, int off, int len) throws IOException;
    
    @Override
    public void seek(long pos) throws IOException;
    
    @Override
    public long getPos() throws IOException;
    
    @Override
    public long length() throws IOException;
    
    @Override
    public void close() throws IOException;
}

I/O Utilities

Utility classes for common I/O operations and stream handling.

/**
 * Location utility functions
 */
public class Locations {
    /**
     * Copy from one location to another
     */
    public static void copy(Location source, Location destination) throws IOException;
    
    /**
     * Move from one location to another
     */
    public static void move(Location source, Location destination) throws IOException;
    
    /**
     * Delete location recursively
     */
    public static void deleteRecursively(Location location) throws IOException;
    
    /**
     * Create parent directories if they don't exist
     */
    public static void mkdirsIfNotExists(Location location) throws IOException;
    
    /**
     * Get relative path between two locations
     */
    public static String getRelativePath(Location base, Location target);
    
    /**
     * Check if location is under another location
     */
    public static boolean isChild(Location parent, Location child);
}

/**
 * ByteBuffer utility functions
 */
public class ByteBuffers {
    /**
     * Convert byte array to ByteBuffer
     */
    public static ByteBuffer wrap(byte[] bytes);
    
    /**
     * Convert ByteBuffer to byte array
     */
    public static byte[] getBytes(ByteBuffer buffer);
    
    /**
     * Copy ByteBuffer contents
     */
    public static ByteBuffer copy(ByteBuffer source);
    
    /**
     * Merge multiple ByteBuffers
     */
    public static ByteBuffer merge(ByteBuffer... buffers);
    
    /**
     * Compare two ByteBuffers
     */
    public static int compare(ByteBuffer a, ByteBuffer b);
}

/**
 * URL connection utilities
 */
public class URLConnections {
    /**
     * Set common properties for HTTP connections
     */
    public static void setDefaultProperties(URLConnection connection);
    
    /**
     * Set timeout properties
     */
    public static void setTimeout(URLConnection connection, int timeoutMs);
    
    /**
     * Set authentication headers
     */
    public static void setAuthentication(URLConnection connection, String username, String password);
    
    /**
     * Get response as string
     */
    public static String getResponse(URLConnection connection) throws IOException;
    
    /**
     * Download to file
     */
    public static void downloadToFile(URLConnection connection, File destination) throws IOException;
}

Usage Examples:

import io.cdap.cdap.common.io.*;
import java.nio.ByteBuffer;

// Binary encoding/decoding
public class DataSerializer {
    public byte[] serializeUserData(String username, int age, boolean active) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BinaryEncoder encoder = new BinaryEncoder(baos);
        
        encoder.writeString(username);
        encoder.writeInt(age);
        encoder.writeBool(active);
        encoder.flush();
        
        return baos.toByteArray();
    }
    
    public UserData deserializeUserData(byte[] data) throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        BinaryDecoder decoder = new BinaryDecoder(bais);
        
        String username = decoder.readString();
        int age = decoder.readInt();
        boolean active = decoder.readBool();
        
        return new UserData(username, age, active);
    }
}

// Custom codec implementation
public class JsonCodec<T> implements Codec<T> {
    private final Class<T> type;
    private final ObjectMapper objectMapper;
    
    public JsonCodec(Class<T> type) {
        this.type = type;
        this.objectMapper = new ObjectMapper();
    }
    
    @Override
    public byte[] encode(T object) throws IOException {
        return objectMapper.writeValueAsBytes(object);
    }
    
    @Override
    public T decode(byte[] data) throws IOException {
        return objectMapper.readValue(data, type);
    }
}

// Location management with caching
public class FileManager {
    private final CachingLocationFactory locationFactory;
    
    public FileManager(LocationFactory baseFactory) {
        this.locationFactory = new CachingLocationFactory(baseFactory, 1000);
    }
    
    public void processFiles(String basePath, List<String> filePaths) throws IOException {
        Location baseLocation = locationFactory.create(basePath);
        Locations.mkdirsIfNotExists(baseLocation);
        
        for (String filePath : filePaths) {
            Location fileLocation = locationFactory.create(
                baseLocation.toURI().resolve(filePath).toString()
            );
            
            if (fileLocation.exists()) {
                processFile(fileLocation);
            } else {
                System.out.println("File not found: " + filePath);
            }
        }
        
        // Print cache statistics
        System.out.println("Cache stats: " + locationFactory.getCacheStats());
    }
    
    public void createSymbolicLinks(Location sourceDir, Location linkDir) throws IOException {
        if (sourceDir instanceof LinkableLocation && linkDir instanceof LinkableLocation) {
            LinkableLocation linkableSource = (LinkableLocation) sourceDir;
            LinkableLocation linkableTarget = (LinkableLocation) linkDir;
            
            for (Location file : sourceDir.list()) {
                if (file.isFile()) {
                    Location linkFile = linkDir.append(file.getName());
                    ((LinkableLocation) file).createSymbolicLink(linkFile);
                }
            }
        }
    }
    
    private void processFile(Location location) throws IOException {
        // File processing logic
        System.out.println("Processing: " + location.getName() + 
                          " (size: " + location.length() + " bytes)");
    }
}

// Seekable stream processing
public class LogFileProcessor {
    public void processLogFile(File logFile, long startOffset, long endOffset) throws IOException {
        try (FileSeekableInputStream stream = new FileSeekableInputStream(logFile)) {
            // Seek to start position
            stream.seek(startOffset);
            
            byte[] buffer = new byte[8192];
            long currentPos = stream.getPos();
            
            while (currentPos < endOffset) {
                int bytesToRead = (int) Math.min(buffer.length, endOffset - currentPos);
                int bytesRead = stream.read(buffer, 0, bytesToRead);
                
                if (bytesRead == -1) {
                    break; // End of file
                }
                
                // Process the read data
                processLogData(buffer, 0, bytesRead);
                
                currentPos = stream.getPos();
            }
        }
    }
    
    public void indexLogFile(File logFile) throws IOException {
        try (FileSeekableInputStream stream = new FileSeekableInputStream(logFile)) {
            Map<String, Long> lineIndex = new HashMap<>();
            long position = 0;
            String line;
            
            BufferedReader reader = new BufferedReader(
                new InputStreamReader(stream, StandardCharsets.UTF_8)
            );
            
            while ((line = reader.readLine()) != null) {
                // Extract timestamp or identifier from line
                String key = extractLineKey(line);
                if (key != null) {
                    lineIndex.put(key, position);
                }
                position = stream.getPos();
            }
            
            System.out.println("Indexed " + lineIndex.size() + " lines");
        }
    }
    
    private void processLogData(byte[] data, int offset, int length) {
        // Process log data
        String logData = new String(data, offset, length, StandardCharsets.UTF_8);
        System.out.println("Processing log data: " + logData.length() + " chars");
    }
    
    private String extractLineKey(String line) {
        // Extract timestamp, request ID, etc.
        return line.length() > 20 ? line.substring(0, 20) : null;
    }
}

// Advanced I/O utilities
public class DataTransferService {
    public void transferLargeFile(Location source, Location destination) throws IOException {
        System.out.println("Transferring: " + source.getName() + " -> " + destination.getName());
        
        // Copy with progress tracking
        long totalSize = source.length();
        long transferred = 0;
        
        try (InputStream input = source.getInputStream();
             OutputStream output = destination.getOutputStream()) {
            
            byte[] buffer = new byte[64 * 1024]; // 64KB buffer
            int bytesRead;
            
            while ((bytesRead = input.read(buffer)) != -1) {
                output.write(buffer, 0, bytesRead);
                transferred += bytesRead;
                
                // Report progress
                if (transferred % (1024 * 1024) == 0) { // Every MB
                    double progress = (double) transferred / totalSize * 100;
                    System.out.printf("Progress: %.1f%% (%d/%d bytes)%n", 
                                     progress, transferred, totalSize);
                }
            }
        }
        
        System.out.println("Transfer completed: " + transferred + " bytes");
    }
    
    public void downloadFromUrl(String url, File destination, int timeoutMs) throws IOException {
        URLConnection connection = new URL(url).openConnection();
        URLConnections.setTimeout(connection, timeoutMs);
        URLConnections.setDefaultProperties(connection);
        
        // Download to file
        URLConnections.downloadToFile(connection, destination);
        
        System.out.println("Downloaded: " + url + " -> " + destination.getAbsolutePath());
    }
    
    public void mergeFiles(List<Location> sourceFiles, Location destination) throws IOException {
        try (OutputStream output = destination.getOutputStream()) {
            for (Location sourceFile : sourceFiles) {
                System.out.println("Merging: " + sourceFile.getName());
                
                try (InputStream input = sourceFile.getInputStream()) {
                    byte[] buffer = new byte[32 * 1024];
                    int bytesRead;
                    
                    while ((bytesRead = input.read(buffer)) != -1) {
                        output.write(buffer, 0, bytesRead);
                    }
                }
            }
        }
        
        System.out.println("Merged " + sourceFiles.size() + " files into: " + 
                          destination.getName());
    }
}

// ByteBuffer operations
public class BufferProcessor {
    public void processDataBuffers(List<ByteBuffer> buffers) {
        // Merge all buffers
        ByteBuffer merged = ByteBuffers.merge(buffers.toArray(new ByteBuffer[0]));
        
        System.out.println("Merged buffer size: " + merged.remaining() + " bytes");
        
        // Process merged data
        byte[] data = ByteBuffers.getBytes(merged);
        processData(data);
    }
    
    public void compareBuffers(ByteBuffer buffer1, ByteBuffer buffer2) {
        int comparison = ByteBuffers.compare(buffer1, buffer2);
        
        if (comparison == 0) {
            System.out.println("Buffers are identical");
        } else if (comparison < 0) {
            System.out.println("Buffer1 is lexicographically smaller");
        } else {
            System.out.println("Buffer1 is lexicographically larger");
        }
    }
    
    private void processData(byte[] data) {
        // Data processing logic
        System.out.println("Processing " + data.length + " bytes");
    }
}

Install with Tessl CLI

npx tessl i tessl/maven-io-cdap-cdap--cdap-common

docs

configuration.md

exceptions.md

http.md

index.md

io.md

logging.md

network.md

security.md

services.md

utilities.md

tile.json