Nitin Agrawal
Contact -
  • Home
  • Interviews
    • Secret Receipe
    • InterviewFacts
    • Resume Thoughts
    • Daily Coding Problems
    • BigShyft
    • Companies
    • Interviews Theory
  • Programming Languages
    • Java Script >
      • Tutorials
      • Code Snippets
    • Reactive Programming >
      • Code Snippets
    • R
    • DataStructures >
      • LeetCode Problems >
        • Problem10
        • Problem300
      • AnagramsSet
    • Core Java >
      • Codility
      • Program Arguments OR VM arguments & Environment variables
      • Java Releases >
        • Java8 >
          • Performance
          • NasHorn
          • WordCount
          • Thoughts
        • Java9 >
          • ServiceLoaders
          • Lambdas
          • List Of Objects
          • Code Snippets
        • Java14 >
          • Teeing
          • Pattern
          • Semaphores
        • Java17 >
          • Switches
          • FunctionalStreams
          • Predicate
          • Consumer_Supplier
          • Collectors in Java
        • Java21 >
          • Un-named Class
          • Virtual Threads
          • Structured Concurrency
      • Threading >
        • ThreadsOrder
        • ProducerConsumer
        • Finalizer
        • RaceCondition
        • Executors
        • Future Or CompletableFuture
      • Important Points
      • Immutability
      • Dictionary
      • Sample Code Part 1 >
        • PatternLength
        • Serialization >
          • Kryo2
          • JAXB/XSD
          • XStream
        • MongoDB
        • Strings >
          • Reverse the String
          • Reverse the String in n/2 complexity
          • StringEditor
          • Reversing String
          • String Puzzle
          • Knuth Morris Pratt
          • Unique characters
          • Top N most occurring characters
          • Longest Common Subsequence
          • Longest Common Substring
        • New methods in Collections
        • MethodReferences
        • Complex Objects Comparator >
          • Performance
        • NIO >
          • NIO 2nd Sample
        • Date Converter
        • Minimum cost path
        • Find File
      • URL Validator
    • Julia
    • Python >
      • Decorators
      • String Formatting
      • Generators_Threads
      • JustLikeThat
    • Go >
      • Tutorial
      • CodeSnippet
      • Go Routine_Channel
      • Suggestions
    • Methodologies & Design Patterns >
      • Design Principles
      • Design Patterns >
        • TemplatePattern
        • Adapter Design Pattern
        • Proxy
        • Lazy Initialization
        • CombinatorPattern
        • Singleton >
          • Singletons
        • Strategy
  • Frameworks
    • Apache Velocity
    • React Library >
      • Tutorial
    • Spring >
      • Spring Boot >
        • CustomProperties
        • ExceptionHandling
        • Custom Beans
        • Issues
      • Quick View
    • Rest WebServices >
      • Interviews
      • Swagger
    • Cloudera BigData >
      • Ques_Ans
      • Hive
      • Apache Spark >
        • ApacheSpark Installation
        • SparkCode
        • Sample1
        • DataFrames
        • RDDs
        • SparkStreaming
        • SparkFiles
    • Integration >
      • Apache Camel
    • Testing Frameworks >
      • JUnit >
        • JUnit Runners
      • EasyMock
      • Mockito >
        • Page 2
      • TestNG
    • Blockchain >
      • Ethereum Smart Contract
      • Blockchain Java Example
    • Microservices >
      • Messaging Formats
      • Design Patterns
    • AWS >
      • Honeycode
    • Dockers >
      • GitBash
      • Issues
      • Kubernetes
  • Databases
    • MySql
    • Oracle >
      • Interview1
      • SQL Queries
    • Elastic Search
  • Random issues
    • TOAD issue
    • Architect's suggestions
  • Your Views
kryo-2.21.jar
File Size: 363 kb
File Type: jar
Download File

Also check - https://www.baeldung.com/kryo
Below is the code written using kryo2.21 & jar is attached above. Benefits as per different online resources kryo serialization/deserialization is much faster than traditional use of Serializable interface. And I saw that I didn't require to implement Serializable interface to serialize the object of custom class as shown below and below the referenced class is also serialized by default, if required control on what to be serialized then implement KryoSerializable interface (similar to the java.io.Externalizable interface in the JDK).override the read & write methods of Kryo -


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

public class KryoUse {
 
 public static void main(String[] args) throws FileNotFoundException {
  Kryo kryo = new Kryo();

// concerned classes are register to speed up the serialization process as if the class is registered by some int value as shown below then class name is not serialized & below given id is used during serialization & deserialization.

kryo.register(Subject.class, 10);
kryo.register(Address.class, 11);


  byte[] buffer = new byte[51200];
  String sht = "helloworld....I am Nitin Agrawal";
  Subject sub1 = new Subject();
  sub1.name = "Nitin Agrawal";
  sub1.age = 30;

sub1.city = new Address();
sub1.city.city = "Pune";
  Output output = new Output();

// below buffer is required to be set as per the requirements else you will get exception like-

com.esotericsoftware.kryo.KryoException: Buffer overflow. Max capacity: 0, required: 1

  output.setBuffer(buffer);
  output.setOutputStream(new FileOutputStream(new File("D:\\Kryo.txt"), true));
  kryo.writeObject(output, sht);
  output.flush();
  Input input = new Input();
  input.setBuffer(buffer);
  input.setInputStream(new FileInputStream(new File("D:\\Kryo.txt")));

// fetching order must be same as the order of insertion of the elements
  Subject sub = (Subject)kryo.readObject(input, Subject.class);
  String sht2 = (String)kryo.readObject(input, String.class);
  System.out.println(sub.name);

System.out.println(sub.city.city);
  System.out.println(sht2);
 }
}

class Subject{
 String name;
 int age;
}



Powered by Create your own unique website with customizable templates.