Skip to content

Instantly share code, notes, and snippets.

@thekalinga
thekalinga / Merriam Webster to Lexico linker.user.js
Created December 1, 2019 10:00
Merriam Webster -> Lexico linker
// ==UserScript==
// @name Merriam Webster -> Lexico linker
// @namespace http://tampermonkey.net/
// @version 0.1
// @author You
// @match https://www.merriam-webster.com/dictionary/*
// @grant none
// ==/UserScript==
(function() {
// ==UserScript==
// @name wikipedia
// @namespace http://tampermonkey.net/
// @version 0.1
// @author You
// @match https://*.wikipedia.org/wiki/*
// @match https://*.wikiquote.org/wiki/*
// @match https://*.wiktionary.org/wiki/*
// @grant none
// ==/UserScript==
@thekalinga
thekalinga / base64EncodeFile.java
Created August 28, 2019 13:04
Base64 encode file content (eager fetch) in java8
public static String base64Encode(Path filePath) {
String base64EncodeFileConetnt = "";
try (InputStream fileStream = newInputStream(filePath)) {
// Reading a Image file from file system
byte[] fileData = new byte[(int) Files.size(filePath)];
//noinspection ResultOfMethodCallIgnored
fileStream.read(fileData);
base64EncodeFileConetnt = Base64.getEncoder().encodeToString(fileData);
} catch (FileNotFoundException e) {
System.out.println("File not found" + e);
@thekalinga
thekalinga / numOfDigitsOfNumber.java
Created August 28, 2019 13:03
Get number of digits of a number in java8
public static int getNumOfDigits(int number) {
int numOfDigits = 0;
int remainder = number;
do {
numOfDigits++;
remainder = remainder / 10;
} while (remainder > 0);
return numOfDigits;
}
@thekalinga
thekalinga / deleteRecursive.java
Created August 28, 2019 13:03
Delete directory recursively in java8
public static void deleteDirRecursively(Path pathToDel) throws IOException {
Files.walk(pathToDel)
.sorted(reverseOrder()) // dir would be listed before the files inside. So, we delete the files in the right order
.forEach(path -> run(() -> delete(path)));
}
@thekalinga
thekalinga / zip.java
Created August 28, 2019 13:02
Zip directory recursively in java8
public static void zip(Path sourceDirPath, Path zipFilePath) throws IOException {
//@formatter:off
createFile(zipFilePath, DEFAULT_FILE_ATTRS);
try (ZipOutputStream zos = new ZipOutputStream(newOutputStream(zipFilePath)); Stream<Path> paths = Files.walk(sourceDirPath)) {
paths.filter(path -> !Files.isDirectory(path))
.forEach(path -> {
ZipEntry zipEntry = new ZipEntry(sourceDirPath.relativize(path).toString());
try {
zos.putNextEntry(zipEntry);
Files.copy(path, zos);
@thekalinga
thekalinga / Splitter.java
Last active August 28, 2019 12:54
Split pdf using openpdf/iText
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
@thekalinga
thekalinga / create-image-pdf-from-any-pdf.md
Created August 22, 2019 11:27
Creating image version pdf from text pdf using Apache Box
  void saveAsImagePdfFromTextPdf() throws IOException {
    final Path originalPdfPath = Paths.get("<source pdf path>");
    final Path imagePdfPath = Paths.get("<destination pdf path>");
    try(PDDocument document = PDDocument.load(originalPdfPath.toFile()); PDDocument imageDoc = new PDDocument()) {
      PDFRenderer pdfRenderer = new PDFRenderer(document);
      for (int pageNum = 0; pageNum < document.getNumberOfPages(); ++pageNum) {
        final BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(pageNum, 300, ImageType.RGB);
        PDPage page = new PDPage(new PDRectangle(bufferedImage.getWidth(), bufferedImage.getHeight()));
 imageDoc.addPage(page);
@thekalinga
thekalinga / Kafka commands.md
Created April 6, 2019 13:34 — forked from vkroz/Kafka commands.md
Kafka frequent commands

Kafka frequent commands

Assuming that the following environment variables are set:

  • KAFKA_HOME where Kafka is installed on local machine (e.g. /opt/kafka)
  • ZK_HOSTS identifies running zookeeper ensemble, e.g. ZK_HOSTS=192.168.0.99:2181
  • KAFKA_BROKERS identifies running Kafka brokers, e.g. KAFKA_BROKERS=192.168.0.99:9092

Server

Start Zookepper and Kafka servers

@thekalinga
thekalinga / spring-data-in-a-nutshell.md
Last active March 3, 2019 15:18
Spring Data in a nutshell

WIP

  • Either extend your interface from CrudRepository<T, ID> (or more specific variant of it, if the classpath contains repositories for multiple types of data sources say JpaRepository<T, ID>, MongoRepository<T, ID>)
  • (or) copy paste selective methods from CrudRepository/JpaRepository interface and annotate your interface with @RepositoryDefinition(domainClass = T.class, idClass = Id.class). With this your repository interface would be quite clean
  • @EnableJpaRepositories (+ its persistence specific variants) at the @Configuration class level
  • Expects the transaction manager to be named transactionManager & entityManagerFactory to be named entityManagerFactory by default. If not, we have to explicitly specify the actual names
  • For all default method, you can find implementation in SimpleJpaRepository, which is internally annotated with @Repository (gives exception translation) & @Transactional(readOnly = true) for transactional guarantee

Queries