#InputStream
Explore tagged Tumblr posts
learning-code-ficusoft · 4 months ago
Text
Provide insights into securing Java web and desktop applications.
Tumblr media
Securing Java web and desktop applications requires a combination of best practices, security libraries, and frameworks to prevent vulnerabilities like SQL injection, XSS, CSRF, and unauthorized access. Here’s a deep dive into key security measures:
1. Secure Authentication and Authorization
Use Strong Authentication Mechanisms
Implement OAuth 2.0, OpenID Connect, or SAML for authentication.
Use Spring Security for web applications.
Enforce multi-factor authentication (MFA) for added security.
Example (Spring Security Basic Authentication in Java Web App)java@Configuration @EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests(auth -> auth .requestMatchers("/admin/**").hasRole("ADMIN") .anyRequest().authenticated()) .httpBasic(); return http.build(); } }Implement Role-Based Access Control (RBAC)
Define roles and permissions for users.
Use JWT (JSON Web Tokens) for securing APIs.
Example (Securing API using JWT in Spring Boot)javapublic class JwtUtil { private static final String SECRET_KEY = "secureKey"; public String generateToken(String username) { return Jwts.builder() .setSubject(username) .setIssuedAt(new Date()) .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60)) .signWith(SignatureAlgorithm.HS256, SECRET_KEY) .compact(); } }
2. Secure Data Storage and Transmission
Use Secure Communication (HTTPS & TLS)
Use TLS 1.2+ for encrypting data in transit.
Enforce HSTS (HTTP Strict Transport Security).
Encrypt Sensitive Data
Store passwords using bcrypt, PBKDF2, or Argon2.
Use AES-256 for encrypting sensitive data.
Example (Hashing Passwords in Java)javaimport org.mindrot.jbcrypt.BCrypt;public class PasswordSecurity { public static String hashPassword(String password) { return BCrypt.hashpw(password, BCrypt.gensalt(12)); } public static boolean verifyPassword(String password, String hashedPassword) { return BCrypt.checkpw(password, hashedPassword); } }
Use Secure Database Connections
Use parameterized queries to prevent SQL injection.
Disable database user permissions that are not required.
Example (Using Prepared Statements in JDBC)javaPreparedStatement stmt = connection.prepareStatement("SELECT * FROM users WHERE username = ?"); stmt.setString(1, username); ResultSet rs = stmt.executeQuery();
3. Protect Against Common Web Vulnerabilities
Prevent SQL Injection
Always use ORM frameworks (Hibernate, JPA) to manage queries securely.
Mitigate Cross-Site Scripting (XSS)
Escape user input in web views using OWASP Java Encoder.
Use Content Security Policy (CSP) headers.
Prevent Cross-Site Request Forgery (CSRF)
Use CSRF tokens in forms.
Enable CSRF protection in Spring Security.
Example (Enabling CSRF Protection in Spring Security)javahttp.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
4. Secure File Uploads and Deserialization
Validate File Uploads
Restrict allowed file types (e.g., only images, PDFs).
Use virus scanning (e.g., ClamAV).
Example (Checking File Type in Java)javaif (!file.getContentType().equals("application/pdf")) { throw new SecurityException("Invalid file type"); }
Avoid Untrusted Deserialization
Use whitelisting for allowed classes.
Prefer JSON over Java serialization.
Example (Disable Unsafe Object Deserialization in Java)javaObjectInputStream ois = new ObjectInputStream(inputStream) { @Override protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { throw new InvalidClassException("Deserialization is not allowed"); } };
5. Secure Desktop Java Applications
Use Code Signing
Sign JAR files using Java Keytool to prevent tampering.
shjarsigner -keystore mykeystore.jks -signedjar SecureApp.jar MyApp.jar myaliasRestrict JavaFX/Swing Application Permissions
Use Java Security Manager (deprecated but useful for legacy apps).
Restrict access to file system, network, and system properties.
Encrypt Local Data Storage
Use AES encryption for storing local files.
Example (Encrypting Files with AES in Java)javaCipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES")); byte[] encrypted = cipher.doFinal(data);
6. Logging and Monitoring for Security
Use Secure Logging Frameworks
Use logback or SLF4J.
Avoid logging sensitive data like passwords.
Monitor for Anomalies
Implement Intrusion Detection Systems (IDS).
Use audit trails and security alerts.
7. Best Practices for Securing Java Applications
✅ Keep dependencies up to date (Use OWASP Dependency Check). ✅ Run security scans (SAST, DAST) using SonarQube, Checkmarx. ✅ Apply the principle of least privilege for database and API access. ✅ Enforce strong password policies (min length, special characters). ✅ Use API Gateway and rate limiting for public-facing APIs.
Conclusion
Securing Java web and desktop applications requires multi-layered security across authentication, data protection, and vulnerability mitigation. By following best practices like strong encryption, secure coding techniques, and continuous monitoring, developers can protect applications against cyber threats.
WEBSITE: https://www.ficusoft.in/core-java-training-in-chennai/
0 notes
macunan · 5 months ago
Text
Getting Kodi to work on Fedora41
Had some issues with Kodi not playing H264 and H265 and video plugins like Pluto Tv and Paramount this is how I think I fixed it. I think key was installing sudo dnf -y intel-media-driver for N100 GPU.
sudo dnf -y install kodi-inputstream-adaptive sudo dnf group install multimedia sudo dnf install x265-libs sudo dnf group upgrade --with-optional Multimedia sudo dnf -y install kodi-pvr-hts sudo dnf -y install kodi-pvr-nextpvr sudo dnf -y install ffmpeg sudo dnf -y install ffmpeg-free sudo dnf -y reinstall ffmpeg-free sudo dnf swap ffmpeg-free ffmpeg --allowerasing sudo dnf update @multimedia --setopt="install_weak_deps=False" --exclude=PackageKit-gstreamer-plugin sudo dnf install intel-media-driver
0 notes
infohub-24 · 6 months ago
Text
Trending in Nutrition: Lean Beef — The Protein Powerhouse for a Well-Balanced Diet
Tumblr media
Who would have thought lean beefcouldbe a source of protein, vitamins, and minerals in the health & nutrition world suitable for the diet-conscious occasional meat-eaters? Beef If you want to ensure that your meals are packed with protein and are ideal for muscle building, lean beef is another incredible meal prep idea at Fitfine24. In his article, Beef is Trending on Greatist. com, we discuss the reasons why and healthy ways to incorporate lean beef into your diet
What is lean beef at fitfine24?
Lean specifically refers to cuts of meat that have less total fat, perfect for someone watching their weight. Up to 100 grams of meat can be labeled lean by the USDA if they contain less than 10 grams total fat (4.5 g saturates) and less than 95 milligrams of cholesterol per amount (InputStream: Lean cuts such as sirloin, Read more...
#prepideaatFitfine24
#leanbeefatfitfine24
#proteinbenefits
#proteininbeef
#highqualityprotein
0 notes
gima326 · 1 year ago
Text
Protocols と Datatypes をば その4
Protocol を定義する場合。 地味だけど気がついたこと。Interface では名前に使えなかった横棒('-')が使えるんだな、と。 (defprotocol IOFactory  "A protocol for things that can be read from and written to."  (mk-rdr [this] "Creates a BufferedReader.")  (mk-wtr [this] "Creates a BufferedWriter.")) //============ extend-protocol を使って、既存の型( InputStream )にプロトコルの実装を追加することができる。 (extend-protocol IOFactory  InputStream  (mk-rdr [src]   (-> src InputStreamReader. BufferedReader.))  (mk-wtr [dst]   (throw (IllegalArgumentException.   "Can't open as an InputStream."))))
(extend-protocol IOFactory  OutputStream  (mk-rdr [src]   (throw (IllegalArgumentException.   "Can't open as an OutputStream.")))  (mk-wtr [dst]   (-> dst OutputStreamWriter. BufferedWriter.))) user=> (class (mk-rdr (FileInputStream. "./memo.txt"))) java.io.BufferedReader user=> (class (mk-wtr (FileOutputStream. "./memo.txt"))) java.io.BufferedWriter //============ 再度 extend-protocol する際に、 多層的に(再帰的に?)、定義済みの InputStream、OutputStream の実装( mk-rdr、mk-wtr )を呼び出せる。 (extend-protocol IOFactory  File  (mk-rdr [src]   (mk-rdr (FileInputStream. src)))  (mk-wtr [dst]   (mk-wtr (FileOutputStream. dst)))) user=> (class (mk-rdr (File. "./memo.txt"))) java.io.BufferedReader user=> (class (mk-wtr (File. "./memo.txt"))) java.io.BufferedWriter
0 notes
bigdataschool-moscow · 1 year ago
Link
0 notes
javainspires · 1 year ago
Link
FileInputStream in Java #JavaInspires #CodeSamples
0 notes
dotrinh1203 · 3 years ago
Text
Đọc file và ghi file vào bộ nhớ internal Android từ thư mục raw
Đọc file và ghi file vào bộ nhớ internal Android từ thư mục raw
Ghi toàn bộ file InputStream fileInputStream = getResources().openRawResource(getResources().getIdentifier("sample_ocean_with_audio", "raw", getPackageName())); try { int ac = fileInputStream.available(); try { FileOutputStream recStream = new FileOutputStream(Tool.getAppRootPath() + "/abc.mp4"); byte[] localBuffer = new byte[1024]; int bytesRead = 0; while ((bytesRead =…
Tumblr media
View On WordPress
0 notes
simplexianpo · 5 years ago
Text
Java : Excel tool class
Maven import package:
  <dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17-beta1</version>
</dependency>
 Code:
public class ExcelUtil {
private static int sheetSize = 5000;
/**
* @throws Exception
* @Title: listToExcel
* @Description: Write entity classes out to excel with io stream
* @param data List of data to be exported (data source)
* @param out excel: The destination to which the file will be output
* @param fields :There are column names and Chinese names corresponding to each attribute
* @return void : Return type
* @throws
*/
public static <T> void listToExcel(List<T> data,OutputStream out,Map<String,String> fields) throws Exception{
if(data==null||data.size()==0){
throw new Exception("Please pass in data for data export");
}
//xls stores 65,536 sheet at most
HSSFWorkbook workBook = new HSSFWorkbook();
//Calculate how many sheet are needed to store
int sheetNum  = data.size()/sheetSize;
if(data.size()%sheetSize!=0){
sheetNum++;
}
//Two arrays are used to store field names and Chinese names respectively
String[] fieldNames = new String[fields.size()];
String[] chinaNames = new String[fields.size()];
int count = 0;
for(Entry<String,String> entry : fields.entrySet()){
//Entry Map's internal class map < attribute name, Chinese name)
String fieldName = entry.getKey();
String chinaName = entry.getValue();
fieldNames[count]=fieldName;
chinaNames[count] = chinaName;
count++;
}
// padding data
for(int i = 0 ; i < sheetNum;i++){
int rowNum = 0; // Start with the first line
HSSFSheet sheet = workBook.createSheet();
//What is the subscript of the data that each sheet should store 0-4999 5000-9999 10000-14999
//sheet.addMergedRegion(CellRangeAddress.valueOf("$A1:"))
int start =i*sheetSize , end = (i+1)*sheetSize-1>data.size()?data.size():(i+1)*sheetSize-1;
//The first row of column names
HSSFRow row = sheet.createRow(rowNum);
for(int j = 0 ; j < chinaNames.length ; j++){
HSSFCell cell = row.createCell(j);
cell.setCellValue(chinaNames[j]);
}
rowNum++; // Move to the next line
// Fill in the data section
for(int index=start ; index<end;index++){
T item  = data.get(index);
row = sheet.createRow(rowNum);
rowNum++;
for(int j = 0; j < chinaNames.length ; j++){
//Obtain the corresponding value through reflection mechanism
Field field = item.getClass().getDeclaredField(fieldNames[j]);
field.setAccessible(true);
//Through this attribute object, get the attribute value in the specific item
Object o = field.get(item);
String value = o==null?"":o.toString();
HSSFCell cell = row.createCell(j);
cell.setCellValue(value);
}
}
}
workBook.write(out);
}
/**
*
* @Title: excelToList
* @Description: Encapsulate data in excel into entity classes
* @param file
* @param startRow
* @param fields
* @return    Setup file
* @return List<T>    Return type
* @throws
*/
public static <T> List<T> excelToList(Class<T> clazz,InputStream file,int startRow,String[] fields)throws Exception{
HSSFWorkbook workBook = new HSSFWorkbook(file);
List<T> data = new ArrayList<T>();
//for(HSSFSheet  sheet : workBook.iterator())
for(int i = 0; i < workBook.getNumberOfSheets() ;i++){
HSSFSheet sheet = workBook.getSheetAt(i);
if(sheet==null){
continue;
}
// Circular row
for(int j = startRow ; j <=sheet.getLastRowNum();j++){
HSSFRow row = sheet.getRow(j);
if(row==null){
continue;
}
T  item = clazz.newInstance();
for(int k = 0;k < fields.length;k++){
Field field = clazz.getDeclaredField(fields[k]);
field.setAccessible(true);
try{
String value = row.getCell(k).getStringCellValue();
field.set(item,row.getCell(k).getStringCellValue());
}catch(Exception ex){
//ex.printStackTrace();
System.out.println("Failed to set string value. Set it numerically!");
field.set(item,Integer.valueOf(row.getCell(k).getStringCellValue()));
}
}
data.add(item);
}
}
return data;
}
1 note · View note
blogdeprogramacion · 5 years ago
Text
Subir archivos a un servidor via FTP en Java
Subir archivos a un servidor via FTP en Java aparece primero en nuestro https://jonathanmelgoza.com/blog/subir-archivos-a-un-servidor-via-ftp-en-java/
Tumblr media
Muchas veces en el desarrollo de un software a la medida es necesario conectar a un servidor para subir archivos o cualquier otra cosa, hoy veremos cómo subir archivos a un servidor via FTP en Java mediante un sencillo ejemplo paso a paso de lo que tenemos que hacer.
Hace tiempo en el desarrollo de un software a la medida en Java tuve la necesidad de conectar via FTP a un servidor para subir documentos PDF.
El sistema en sí era para la gestión de cotizaciones para una empresa de motores, por supuesto un sistema así requiere almacenar las cotizaciones en formato PDF.
Los documentos se tenían que subir a una determinada carpeta en su servidor y el proceso de subida no debería de detener el flujo del programa.
Una vez que conocemos un poco la historia de la situación en la que estaba vamos a ver cómo conectarnos via FTP en Java a un servidor y subir un archivo.
Antes que nada necesitamo importar la librería de ftp4j en nuestro proyecto.
La podemos descargar desde este enlace:
http://www.sauronsoftware.it/projects/ftp4j/
Debemos crear nuestro objeto de conexión.
FTPClient client;
Y posteriormente debemos conectarnos a nuestro servidor FTP.
try client = new FTPClient(); client.connect("[Dirección del servidor]"); client.login("[usuario]", "[contraseña]"); client.setPassive(true); client.setType(FTPClient.TYPE_BINARY); client.changeDirectory("public_html/otracarpeta/otra/"); catch(Exception err) System.out.println("ERROR: "+err.getLocalizedMessage());
Aquí lo que hacemos es inicializar nuestro objeto de conexión.
Conectamos a nuestro servidor mediante su IP o nombre de dominio.
Nos logueamos mediante usuario y contraseña, asegurate de que estos datos sean correctos.
Establecemos nuestra conexión a pasiva y el tipo de conexión a binaria.
Inmediatamente despues nos cambiamos de directorio a la ubicación donde subiremos nuestro archivo.
Para comenzar la subida de un determinado archivo al servidor en la ruta establecida nos apoyaremos de un SwingWorker.
new SwingWorker<Double, String>() @Override protected Double doInBackground() throws Exception try File archivo = new File("[ruta de archivo]"); InputStream is = new FileInputStream(archivo); client.upload(nombre, is,0,0,new FTPDataTransferListener() @Override public void started() System.out.println("El archivo ha iniciado su subida "+nombre); @Override public void transferred(int i) //System.out.println("El archivo ha transferido su subida "+nombre); @Override public void completed() System.out.println("El archivo ha completado su subida "+nombre); @Override public void aborted() System.out.println("El archivo ha abortado su subida "+nombre); @Override public void failed() System.out.println("El archivo ha fallado su subida "+nombre); ); catch (Exception e) e.printStackTrace(); finally client.disconnect(true); @Override protected void done() .execute();
Y eso es todo!
En mi caso yo agregé una interfaz de usuario para cuando un archivo se estuviera subiendo no interviniera en el proceso del programa pero lo he limpiado para que ustedes solo puedan ver el codigo necesario para la funcionalidad.
Si este codigo sobre cómo subir archivos a un servidor via FTP en Java te fue de utilidad no olvides compartirlo en tus redes sociales y dejarnos un comentario en la sección de abajo si tienes cualquier duda al respecto de este tema, será un placer ayudarte.
¡Hasta luego!
1 note · View note
jagdogger2525 · 5 years ago
Text
How to Setup RetroPie & Kodi (with Netflix) on a Raspberry Pi via Raspbian
This is a full and extensive setup to have a desktop, Media Center, and Retro Gaming station in a single Raspberry Pi.
Note: Any time Pi is mentioned, it is referring to Raspberry Pi in the Hardware (that you hopefully purchased).
Hardware:
Raspberry Pi (preferably 4 4GB model yet is able to work with any Pi)
Pi Power Cable (Micro-USB [non-Pi 4] or USB-C [Pi 4])
HDMI Cable [and accompanying Adapter (Micro-HDMI Adapter for Pi 4 / Mini-HDMI Adapter for Pi Zero [0])]
Keyboard
Mouse
Generic Game Controller
32GB MicroSD card (256GB max)
Memory Card Reader [MCR]
A regular (and up-to-date & running) Windows/Macintosh/Linux PC to install the software onto the MicroSD card
A working router/switch with internet access
(Optional) CAT5e or CAT6 Ethernet Cable (Long enough to plug in the router and reach to plug into the Pi and if the router has no wifi)
(Optional) Micro-USB On-The-Go [OTG] cable (Pi Zero exclusively)
(Optional) 4 USB Hub (Pi Zero and A Models exclusively)
(Optional) USB LAN Adapter (with CAT5e/CAT6 Ethernet cable for use with Pi Zero, Pi 1, and Pi 2 exclusively)
(Optional) USB Wifi Adapter (for use with Pi Zero [exclusively with Micro-USB OTG cable & USB Hub], Pi 1, and Pi 2 exclusively)
Software:
Balena Etcher
Raspbian
RetroPie Games (Public Domain):
MAME
Non-MAME
PC Setup:
Download the latest Etcher and Raspbian builds
Once downloaded, insert the MicroSD card into the MCR and then into the computer
Launch/Execute/Start Etcher
Click 'Select image'
Locate the download folder and select the Rasbian build you just downloaded
The 'Select target' should already be chosen as the MicroSD card in the MCR, if not, click 'Select target' and find and select the MicroSD card
Click 'Flash' and enter your credentials
Once Etcher says that the flash is 'successful', eject/unmount the MicroSD card (should be called 'boot' and if it wasn't already ejected/unmounted already), and remove the MCR
Remove the MicroSD card and insert it into the Raspberry Pi (on the under side with the text facing up and the shiny spots facing down)
Raspberry Pi Setup:
Once the MicroSD card is inserted, plug in everything EXCEPT the power cable
Plug in the power cable last (so that everything is able to be seen by the Pi)
You'll be prompted to setup Raspbian for a First-Time Setup [FTS]
After the FTS is complete, right-click the Clock
Click 'Digital Clock Settings'
In 'Clock Format', replace '%R' with '%I:%M %p'
Click 'OK'
Right-Click the Task Bar
Click 'Panel Settings'
Select the radio/circle button that is next to 'Bottom'
(Optional) Select the 'Advanced' tab
(Optional) Check the box next to 'Minimize panel when not in use'
Click 'Close'
Once the initial Raspbian setup is done, open Terminal* and type:
sudo apt-get update && sudo apt full-upgrade
sudo apt autoremove
sudo apt-get install kodi kodi-peripheral-joystick kodi-pvr-iptvsimple kodi-inputstream-adaptive kodi-inputstream-rtmp build-essential python-pip python-dev libffi-dev libssl-dev libnss3 git lsb-release
sudo pip install pycryptodomex
wget https://github.com/castagnait/repository.castagnait/raw/master/repository.castagnait-1.0.1.zip
git clone --depth=1 https://github.com/retropie/retropie-setup.git
cd retropie-setup
chmod +x retropie_setup.sh
sudo ./retropie_setup.sh
Select 'Ok'
Select 'Basic Install'
Select 'Yes'
After the installation, select 'Configuration / tools'
Select 'autostart'
Select 'Boot to Desktop (auto login as pi)'
Select 'Ok'
Select 'Ok'
Select 'Cancel'
Select 'Back'
Select 'Perform reboot'
Select 'Yes'
Open Terminal back up and type:
kodi
Once Kodi is loaded up, hit the Cog/Gear up top
Select 'System'
Go down to 'Add-ons'
Turn 'Unknown sources' on
Select 'Yes' to the pop-up
To back to the previous menu (where 'System' was selected) and select 'Add-ons'
Select 'Install from zip file'
Go into the 'Home folder'
Select 'repository.castagnait-1.0.1.zip'
Go back one menu and select 'Install from Repository'
Select 'CastagnaIT Repository'
Select 'Video Add-ons'
Select 'Netflix'
Select the top most choice (if given multiple choices for version aka 1.1.0, 1.1.1, or 1.2.0)
Select 'Install'
Select 'Ok'
Go back to the main (or landing) menu
Select 'Add-ons' between 'Games' and 'Pictures’
Select 'Netflix'
Enter credentials
It is going to ask about installing Widevine and setting aside 3.1GB of memory, just select 'Install Widevine' and just keep accepting the other pop-ups
After this installation, both Kodi and RetroPie should be properly installed and able to be accessed
Note: To get back into RetroPie, in Terminal, just type:
emulationstation
*Terminal looks like 'Command Prompt' from Windows
1 note · View note
fullstackme · 6 years ago
Text
How To Read Lined JSON files with Java 8
Came across this, seeming trivial at a glance, task of parsing a relatively well-formatted data feed just recently. Sure, you may say, what could be easier than parsing a JSON format given that there are plenty of tools for that, especially for Java? Well, sorry, not exactly JSON... In effect, compared to other unstructured data sources I previously worked with, this feed used a lined JSON format (i.e. IJSON). Example:
{“id”: “us-cia-world-leaders.bc0...”, “type”: “individual”, ...} {“id”: “us-cia-world-leaders.924...”, “type”: “entity”, ...} {...}
Even though this format is widely used, mainstream JSON parsers such as Jackson cannot handle this structure since it’s not a valid JSON file. Looks like we have a little problem here?
Tackling IJSON with Java
A quick solution is to simply read the lined JSON file line by line and transform each line to a POJO entry. Combined with streamed input readers, the lined JSON format appeared to be more efficacious than the “classic” JSON, merely because we no longer need to preload the entire structure in memory and then transform it. With 30Mb+ files, the performance benefits are evidently noticeable.
The below code snippet illustrates how this can be achieved:
import com.fasterxml.jackson.databind.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; import java.util.stream.Stream; /** * Simple streamed reader to go through Lined JSON files, convert each line to POJO entry * and perform a specified action on every row. * @author Vladimir Salin */ public class LineBasedJsonReader {    private static final Logger log = LoggerFactory.getLogger(LineBasedJsonReader.class);    private ObjectMapper objectMapper;    public LineBasedJsonReader(ObjectMapper objectMapper) {        this.objectMapper = objectMapper;    }    /**     * Parses a provided input in a streamed way. Converts each line in it     * (which is supposed to be a JSON) to a specified POJO class     * and performs an action provided as a Java 8 Consumer.     *     * @param stream lined JSON input     * @param entryClass POJO class to convert JSON to     * @param consumer action to perform on each entry     * @return number of rows read     */    public int parseAsStream(final InputStream stream, final Class entryClass, final Consumer<? super Object> consumer) {        long start = System.currentTimeMillis();        final AtomicInteger total = new AtomicInteger(0);        final AtomicInteger failed = new AtomicInteger(0);        try (Stream<String> lines = new BufferedReader(new InputStreamReader(stream)).lines()) {            lines                    .map(line -> {                        try {                            total.incrementAndGet();                            return objectMapper.readerFor(entryClass).readValue(line);                        } catch (IOException e) {                            log.error("Failed to parse a line {}. Reason: {}", total.get()-1, e.getMessage());                            log.debug("Stacktrace: ", e);                            failed.incrementAndGet();                            return null;                        }                    })                    .filter(Objects::nonNull)                    .forEach(consumer);        }        long took = System.currentTimeMillis() - start;        log.info("Parsed {} lines with {} failures. Took {}ms", total.get(), failed.get(), took);        return total.get() - failed.get();    } }
As you can see, we simply need to pass a source as an InputStream, a POJO class for the JSON we want to parse to, a Java 8 Consumer to act on each parsed row, and that’s it. The above is just a simple snippet for illustrative purposes. In a production environment, one should add more robust error handling.
So why Lined JSON?
Indeed, with these numerous JSON parsing tools, why the heck someone decided to go Lined JSON? Is it any fancy writing every single line in this JSON-y object format?
Actually, yes, it is fancy. Just think of it for a second -- you read the line and get a valid JSON object. Let me put it this way: you load just one line into memory and get a valid JSON object you can work with in your code. Another line -- another object. Worked with it, released from memory, going next. And this is how you proceed through the entire file, no matter how long it is.
Just imagine a huge JSON array weighting a good couple of huundred MBs. Going straightforward and reading in full would take quite a bunch of memory. Going lined JSON approach would allow you iterating through each line and spending just a little of your precious memory. For sure, in some cases we need the whole thing loaded, but for others it's just fine to go one by one. So, lessons learned, another convenient data structure to use and to handle!
Originally posted in Reading Lined JSON files with Java 8
1 note · View note
bigdataschool-moscow · 2 years ago
Link
0 notes
illuminatimusic · 6 years ago
Video
youtube
In this episode, I show you how the InputStream class works, and how to use it's methods so that we can therefore use any input stream. #Java #IO #JavaIO Code: Docs: https://ift.tt/2NcTLtN Discord: https://ift.tt/2BMN0aQ Support: https://www.youtube.com/channel/UC_LtbK9pzAEI-4yVprLOcyA/join More Videos coming soon. Leave a comment for any future video suggestions.
1 note · View note
anocana · 6 years ago
Text
what in the goddamn FUCK are Java InputStreams
1 note · View note
zhlq · 2 years ago
Text
CAS Project evidence
===TileContent.java===
import java.awt.image.BufferedImage;
public class TileContent { BufferedImage img;String type;int x;int y;
public TileContent(BufferedImage img_,String type_,int x_,int y_) { img=img_;type=type_;x=x_;y=y_;
} }
===TileImagesConfig.java===
import java.awt.image.BufferedImage; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList;
import javax.imageio.ImageIO;
public class TileImagesConfig { InputStream t; BufferedImage slime; BufferedImage fireSlime; BufferedImage waterSlime; BufferedImage golem; BufferedImage blaze; BufferedImage theWorld; BufferedImage hpPotion; BufferedImage scorePotion;public TileImagesConfig(){ } public void preConfig() throws IOException { t = getClass().getResourceAsStream("imgs/monsters/slime.png"); slime = ImageIO.read(t); t = getClass().getResourceAsStream("imgs/monsters/slime2.png"); fireSlime = ImageIO.read(t); t = getClass().getResourceAsStream("imgs/monsters/slime3.png"); waterSlime = ImageIO.read(t); t = getClass().getResourceAsStream("imgs/monsters/golem.png"); golem = ImageIO.read(t); t = getClass().getResourceAsStream("imgs/monsters/blaze.png"); blaze = ImageIO.read(t); t = getClass().getResourceAsStream("imgs/monsters/potionHp.png"); hpPotion = ImageIO.read(t); t = getClass().getResourceAsStream("imgs/monsters/potionScore.png"); scorePotion = ImageIO.read(t); t = getClass().getResourceAsStream("imgs/monsters/theWorld.png"); theWorld = ImageIO.read(t); } public void config(ArrayList<BufferedImage> imgs,ArrayList<String> types) { //common for(int i=0;i<10;i++) { imgs.add(slime);types.add("slime");imgs.add(slime);types.add("slime2"); imgs.add(fireSlime);types.add("fireSlime");imgs.add(fireSlime);types.add("fireSlime"); imgs.add(waterSlime);types.add("waterSlime");imgs.add(waterSlime);types.add("waterSlime2"); } //rare for(int i=0;i<6;i++) { imgs.add(golem);types.add("golem"); imgs.add(blaze);types.add("blaze");imgs.add(blaze);types.add("blaze");imgs.add(blaze);types.add("blaze2"); } //legend imgs.add(hpPotion);types.add("hpPotion");imgs.add(hpPotion);types.add("hpPotion"); imgs.add(scorePotion);types.add("scorePotion"); imgs.add(theWorld);types.add("theWorld");imgs.add(theWorld);types.add("theWorld");imgs.add(theWorld);types.add("theWorld2"); }
}
0 notes
devsnews · 3 years ago
Link
This article from the "Clojure, The Essential Reference" book, gives us a good overview of the "clojure.java.io" package in the Clojure programming language which simplifies the interaction with the Java IO system from the Clojure code.
0 notes