Changing normal application to thread in java [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have to change my basic application into threads
my application behaviour
read the files from a folder
Get extensions from the file
through if else condition checks whether it belongs to (doc,ppt,pptx) & does the operation
After processing it will starts from step 1.
can you tell me how to proceed since i'm new to threads
I dont need entire code i need just steps to follow
(like in which area i can use thread)
thanks in advance

I did something like this long before. Sorting jpeg,pdf, png, mp3 files to different folders using threads. I used the magic number of files for identifying rather than using the extension.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author Zac
*/
public class Sorting extends Thread{
private static final int MAGIC_PNG[] = new int[] { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
private static final int MAGIC_JPG[] = new int[] { 0xFF, 0xD8, 0xFF };
private static final int MAGIC_PDF[] = new int[] { 0x25, 0x50, 0x44, 0x46 };
private static final int MAGIC_MP3[] = new int[] { 0x49, 0x44, 0x33 };
private String stagingDirectory,targetDirectory,preferenceFileDir;
private File thePrefs;
public Sorting(){
preferenceFileDir = System.getProperty( "user.home" );
thePrefs = new File(preferenceFileDir+"/Staging/prefs.txt");
}
private static boolean isPng(File filename) throws Exception {
FileInputStream ins = new FileInputStream(filename);
try {
for(int i = 0; i < MAGIC_PNG.length; ++i) {
if(ins.read() != MAGIC_PNG[i]) {
return false;
}
}
return true;
} finally {
ins.close();
}
}
private static boolean isJpg(File filename) throws Exception {
FileInputStream ins = new FileInputStream(filename);
try {
for(int i = 0; i < MAGIC_JPG.length; ++i) {
if(ins.read() != MAGIC_JPG[i]) {
return false;
}
}
return true;
} finally {
ins.close();
}
}
private static boolean isMp3(File filename) throws Exception {
FileInputStream ins = new FileInputStream(filename);
try {
for(int i = 0; i < MAGIC_MP3.length; ++i) {
if(ins.read() != MAGIC_MP3[i]) {
return false;
}
}
return true;
} finally {
ins.close();
}
}
private static boolean isPdf(File filename) throws Exception {
FileInputStream ins = new FileInputStream(filename);
try {
for(int i = 0; i < MAGIC_PDF.length; ++i) {
if(ins.read() != MAGIC_PDF[i]) {
return false;
}
}
return true;
} finally {
ins.close();
}
}
private void getPreference(){
FileInputStream fis = null;
try{
fis = new FileInputStream(thePrefs);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
stagingDirectory= br.readLine();
targetDirectory = br.readLine();
}
catch(Exception e){
}
}
public void run() {
//System.out.print("in run \n");
int len;
getPreference();
File stDir = new File(stagingDirectory);
File[] listOfFiles = stDir.listFiles();
//System.out.print(listOfFiles);
if(listOfFiles == null)
len =0;
else
len = listOfFiles.length;
try{
while(true){
System.out.println(len);
for(int i = 0;i<len;i++){
if(isJpg(listOfFiles[i])){
System.out.println(listOfFiles[i].getName()+ " is jpeg");
listOfFiles[i].renameTo(new File(targetDirectory+"/jpeg/"+listOfFiles[i].getName()+".jpeg"));
}
else if(isPdf(listOfFiles[i])){
System.out.println(listOfFiles[i].getName()+ " is pdf");
listOfFiles[i].renameTo(new File(targetDirectory+"/pdf/"+listOfFiles[i].getName()+".pdf"));
}
else if(isMp3(listOfFiles[i])){
System.out.println(listOfFiles[i].getName()+ " is mp3");
listOfFiles[i].renameTo(new File(targetDirectory+"/mp3/"+listOfFiles[i].getName()+".mp3"));
}
else if(isPng(listOfFiles[i])){
System.out.println(listOfFiles[i].getName()+ " is png");
listOfFiles[i].renameTo(new File(targetDirectory+"/png/"+listOfFiles[i].getName()+".png"));
}
}
listOfFiles = stDir.listFiles();
len = listOfFiles.length;
Thread.sleep(3000);//sleeps for 3 seconds
}
}catch(Exception e){
System.out.println("interrupted"+e);
}
}
}
In my public static void main(String[] args)
obj= new Sorting();
obj.start(); //calls the run method
here's the github link for the project.
Sorting out files using java
Hope it helps. Project was done in netbeans / java 1.7

Related

How to encapsulate a SCTP packet in UDP and send it over UDP channel in java

I have to transfer a file using SCTP protocol. I have written the code in java but the code is not working when I am using 4G hotspot network. So I came across this RFC which talks about UDP encapsulation of SCTP. I want to know if there is an implementation which I can use to encapsulate SCTP packet in UDP and send it over UDP channel so that it can traverse heavily NATted network. My current code for sending the data packet is as follows:
import java.io.*;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.*;
import com.sun.nio.sctp.MessageInfo;
import com.sun.nio.sctp.SctpChannel;
import com.sun.nio.sctp.SctpServerChannel;
public class Main {
SctpChannel connectionChannelPrimary;
SctpChannel connectionChannelSecondary;
InetSocketAddress serverSocketAddressPrimary;
InetSocketAddress serverSocketAddressSecondary;
String directoryPath;
public Main() {
serverSocketAddressPrimary = new InetSocketAddress(6002);
serverSocketAddressSecondary = new InetSocketAddress(6003);
}
public void setDirectoryPath(String directoryPath) {
this.directoryPath = directoryPath;
}
public String getDirectoryPath() {
return directoryPath;
}
public void establishConnection(int connId) throws IOException {
SctpServerChannel sctpServerChannel = SctpServerChannel.open();
if (connId == 0) {
sctpServerChannel.bind(serverSocketAddressPrimary);
connectionChannelPrimary = sctpServerChannel.accept();
System.out.println("connection established for primary");
} else {
sctpServerChannel.bind(serverSocketAddressSecondary);
connectionChannelSecondary = sctpServerChannel.accept();
System.out.println("connection established for helper");
}
}
ArrayList<String> getAllFiles() {
File directory = new File(this.directoryPath);
ArrayList<String> fileNames = new ArrayList<>();
for (File fileEntry : Objects.requireNonNull(directory.listFiles())) {
if (fileEntry.isFile()) {
fileNames.add(fileEntry.getName());
}
}
Collections.sort(fileNames);
return fileNames;
}
public byte[] readFile(String filename) throws IOException {
String extraString = "\n\n\n\nNRL\n\n\n";
File file = new File(filename);
FileInputStream fl = new FileInputStream(file);
ByteBuffer finalBuffer = ByteBuffer.allocate((int) (file.length() + extraString.length()));
byte[] arr = new byte[(int) file.length()];
int res = fl.read(arr);
if (res < 0) {
System.out.println("Error in reading file");
fl.close();
return null;
}
fl.close();
finalBuffer.put(arr);
finalBuffer.put(extraString.getBytes());
byte[] tmp = new byte[extraString.length()];
finalBuffer.position((int) (file.length() - 1));
finalBuffer.get(tmp, 0, tmp.length);
return finalBuffer.array();
}
public void sendBytes(String filename, int connId) throws IOException {
byte[] message = readFile(filename);
assert message != null;
System.out.println(message.length);
int tmp = 0;
int cntIndex = 60000;
int prevIndex = 0;
boolean isBreak = false;
while (!isBreak) {
byte[] slice;
if (prevIndex + 60000 >= message.length) {
slice = Arrays.copyOfRange(message, prevIndex, message.length);
isBreak = true;
} else {
slice = Arrays.copyOfRange(message, prevIndex, cntIndex);
prevIndex = cntIndex;
cntIndex = cntIndex + 60000;
}
final ByteBuffer byteBuffer = ByteBuffer.allocate(64000);
final MessageInfo messageInfo = MessageInfo.createOutgoing(null, 0);
byteBuffer.put(slice);
byteBuffer.flip();
tmp += slice.length;
try {
if (connId == 0) connectionChannelPrimary.send(byteBuffer, messageInfo);
else connectionChannelSecondary.send(byteBuffer, messageInfo);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(tmp);
}
public static void main(String[] args) throws IOException {
String bgFilePath = "/home/iiitd/Desktop/background/";
String fgFilePath = "/home/iiitd/Desktop/foreground/";
Main myObj = new Main();
myObj.setDirectoryPath("/home/iiitd/Desktop/tmp/");
myObj.establishConnection(1);
myObj.establishConnection(0);
ArrayList<String> files = myObj.getAllFiles();
for (String tmpFile : files) {
String cntFilePath = myObj.getDirectoryPath() + tmpFile;
myObj.sendBytes(cntFilePath,0);
}
}
}
RFC Link: https://datatracker.ietf.org/doc/html/draft-ietf-tsvwg-sctp-udp-encaps-09
In C, I think that usrsctp is a popular implementation of SCTP over UDP. If I understand correctly, it was used by Google Chrome at some point (though I see they mentioned moving to "dcsctp" at some point). Also I have seen it in a mirror of the Firefox sources in 2016, not sure what's the state today.
So one solution would be to wrap usrsctp with JNI. And it appears that this is exactly what jitsi-sctp is doing. I haven't used it, but I would have a look.

ObjectInputStream wont load Array of Objects after second Run - Java

I've been trying to apply the followed code in a wider scheme. Right now I am just trying to understand the concept of ObjectInput/outputStreams and how they handle Array of objects.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class WritFile {
private FileInputStream output;
private ObjectInputStream outPut;
private FileOutputStream input;
private ObjectOutputStream inPut;
private Haha obj[];
public static void main(String args[]) throws Exception {
WritFile obj = new WritFile();
obj.Shazam("src\\Aloha.txt");
}
public void Shazam(String path) {
obj = new Haha[30];
for (int i = 0; i < obj.length; i++) {
obj[i] = new Haha();
}
for (int i = 0; i < 5; i++) {
obj[i].q1 = " Name" + i;
obj[i].x = i;
}
int No = 5;
saveToFile(obj, path);
int counter = 0;
try {
for (int i = 0; i < ReadFromFile(path).length; i++) {
if (ReadFromFile(path)[i].q1 != null) {
counter++;
}
}
for (int i = 0; i < counter; i++) {
if (ReadFromFile(path)[i] != null) {
obj[No++].q1 = ReadFromFile(path)[i].q1;
}
}
} catch (Exception e) {
System.out.printf("error1 : %s", e.getMessage());
e.printStackTrace();
}
try {
for (int i = 0; i < ReadFromFile(path).length; i++) {
if (ReadFromFile(path)[i] != null) {
System.out.println(ReadFromFile(path)[i].q1);
}
}
} catch (Exception e) {
System.out.printf("error2 : %s", e.getMessage());
}
System.out.printf("%s %n", "*******************");
for (int i = 0; i < obj.length; i++) {
System.out.println(obj[i].q1);
}
saveToFile(obj, path);
}
public void saveToFile(Haha arr[], String path) {
try {
input = new FileOutputStream(path, true);
inPut = new ObjectOutputStream(input);
inPut.writeObject(arr);
inPut.close();
} catch (Exception e) {
System.out.printf("Crashd : %s", e.getMessage());
}
}
public Haha[] ReadFromFile(String path) throws Exception {
output = new FileInputStream(path);
outPut = new ObjectInputStream(output);
return ((Haha[]) outPut.readObject());
}
}
My goal with this code is to store some data into an array of objects (OBJ) then save that array to a file using the function I created. Then read the data from the file and store it in the First empty index in the same array and so on.
For some reason it doesn't seem to write the data to the file after the first run :( !
Help!
public class SerializableArray implements Serializable, AutoCloseable{
/**
*
*/
private static final long serialVersionUID = 1L;
private static File ardatei;
transient Integer[] currentarray;
private int cntoverwrite;
FileInputStream fi;
BufferedReader br;
ObjectInputStream in;
int cnt;
public SerializableArray(Integer[] integers, String filename) throws IOException {
ardatei = new File(filename);
currentarray = integers;
if (!ardatei.exists())
{
try {
ardatei.createNewFile();
System.out.println("your file is created....");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("could not create File");
System.exit(-1);
}
FileOutputStream fos = new FileOutputStream(ardatei);
ObjectOutputStream o = new ObjectOutputStream(fos);
o.writeObject(integers);
o.flush();
o.close();
} else if (ardatei.exists() || zustand > 0) {
FileOutputStream fos =
new FileOutputStream(ardatei);
ObjectOutputStream o =
new ObjectOutputStream(fos);
o.writeObject(integers);
o.writeObject("\n " + "your file was overwrite.");
System.out.println("your file exists and become overwritten");
o.flush();
o.close();
}
}
method to read data from given file
public SerializableArray(String filename) {
File f = new File (filename);
fi = null;
br= null;
in = null;
if (f.exists()){
try {
try {
fi = new FileInputStream(filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
throw new FileNotFoundException("Loading file failed...");
}
in = new ObjectInputStream(fi);
System.out.println("Read data....");
//Cast to integer because readObject returns a object
currentarray =(Integer[]) in.readObject();
} catch (IOException | ClassNotFoundException e) {
System.out.println("could not read file....);
}
} else {
throw new RuntimeException ("this file doesnt exist yet");
}
}
that code works in my case ^^

Hashing a .raw file shows NullPointerException in Java

I want to hash a file in Java by calling a file that ends with .raw. These are the codes I used:
FileSearch.java
public class FileSearch
{
private static final File file = null;
public static File findfile(File file) throws IOException
{
String drive = (new DetectDrive()).USBDetect();
Path start = FileSystems.getDefault().getPath(drive);
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
#Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
{
if (file.toString().endsWith(".raw"))
{
System.out.println(file);
}
return FileVisitResult.CONTINUE;
}
});
return file;
}
public static void main(String[] args) throws Exception
{
Hash hasher = new Hash();
FileSearch.findfile(file);
try
{
if (file.toString().endsWith("raw"))
{
hasher.hash(file);
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
Hash.java
public class Hash
{
public void hash(File file) throws Exception
{
MessageDigest md = MessageDigest.getInstance("MD5");
FileInputStream fis = new FileInputStream(file);
byte[] dataBytes = new byte[1024];
int nread = 0;
while ((nread = fis.read(dataBytes)) != -1)
{
md.update(dataBytes, 0, nread);
};
byte[] mdbytes = md.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < mdbytes.length; i++)
{
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
}
System.out.println("Digest(in hex format):: " + sb.toString());
}
}
The first code is used to find the file and perform hash by running the main method and the second code is the method for hashing the file (by MD5). However, when I run the it gives an ouput:
"name of raw file"
Exception in thread "main" java.lang.NullPointerException at FileSearch.main(FileSearch.java:33)
line 33 is the if (file.toString().endsWith("raw")) portion. Anyone knows how I can fix this?
You never initalize file with anything (Well, you initalize it with null)
private static final File file = null;
So when you call
if (file.toString().endsWith("raw"))
file can only be null.
What you probably want is just
file = FileSearch.findfile(file);
See:
What is a NullPointerException, and how do I fix it?

Java Large file to be saved in database - Object design

I am trying to figure out object design to implement large file(~600 MB) respository in the Database using hibernate.
Please suggest a correct approach/design?
class ModelClass{
String name; //meta data
...
Option 1.
byte[] file; // dont want to load the content of the entire file
// in memory by using this but hibernate recognizes
// this datatype
Option 2.
InputStream inputStream;
OutputStream outputStream;
// I can have the methods to provide the input or output stream
// but i dont think its a clean approach. I am not sure how
// I will be able to work with hibernate with streams
Option 3.
File fileHandle;
}
Any other options??
I would like to call save(Object) method of hibernateTemplate to save the object in Database. Dont know if I should have just the meta-data in the class and handle the file save and retreive seperately.
Thanks in advance.
Another workable solution is to use "Work" Interface. The purpose was to avoid loading the file content into memory.
session.doWork(new Work(){
#Override
public void execute(Connection conn) {
//direct sql queries go here
}
});
I have written a SerializableFile class that keeps data in a file. When the object is read, it creates a temporary file.
Here it is:
public class SerializableFile implements Serializable {
private static final File TEMP_DIR = getTempDir();
private transient boolean temporary;
private transient String name;
private transient File file;
public SerializableFile() {
}
public SerializableFile(File file) {
this.file = file;
this.name = file.getName();
this.temporary = false;
}
#Override
protected void finalize() throws Throwable {
dispose();
super.finalize();
}
public void dispose() {
if (temporary && file != null) {
file.delete();
file = null;
}
}
public File keep(String name) throws IOException {
if (temporary) {
temporary = false;
} else {
File newFile = new File(TEMP_DIR, name);
keepAs(newFile);
file = newFile;
}
return file;
}
public void keepAs(File outFile) throws IOException {
if ((temporary || file.equals(outFile)) && file.renameTo(outFile)) {
temporary = false;
file = outFile;
} else {
InputStream in = new FileInputStream(file);
try {
OutputStream out = new FileOutputStream(outFile);
try {
byte buf[] = new byte[4096];
for (int n = in.read(buf); n > 0; n = in.read(buf)) {
out.write(buf, 0, n);
}
} finally {
out.close();
}
} finally {
in.close();
}
outFile.setLastModified(file.lastModified());
}
}
public String getName() {
return name;
}
public File getFile() {
return file;
}
public long lastModified() {
return file.lastModified();
}
private void writeObject(ObjectOutputStream out) throws IOException {
int size = (int)file.length();
long date = file.lastModified();
out.writeUTF(name);
out.writeInt(size);
out.writeLong(date);
InputStream in = new FileInputStream(file);
try {
byte buf[] = new byte[4096];
while (size > 0) {
int n = in.read(buf);
if (n <= 0 || n > size) {
throw new IOException("Unexpected file size");
}
out.write(buf, 0, n);
size -= n;
}
} finally {
in.close();
}
}
private void readObject(ObjectInputStream in) throws IOException {
name = in.readUTF();
int size = in.readInt();
long date = in.readLong();
file = File.createTempFile("tmp", ".tmp", TEMP_DIR);
OutputStream out = new FileOutputStream(file);
try {
byte buf[] = new byte[4096];
while (size > 0) {
int n = in.read(buf, 0, size <= buf.length ? size : buf.length);
if (n <= 0 || n > size) {
throw new IOException("Unexpected file size");
}
out.write(buf, 0, n);
size -= n;
}
} finally {
out.close();
}
file.setLastModified(date);
temporary = true;
}
private static File getTempDir() {
File dir;
String temp = System.getProperty("com.lagalerie.live.temp-dir");
if (temp != null) {
dir = new File(temp);
} else {
String home = System.getProperty("user.home");
dir = new File(home, "temp");
}
if (!dir.isDirectory() && !dir.mkdirs()) {
throw new RuntimeException("Could not create temp dir " + dir);
}
return dir;
}
}
Open JPA supports a #Persistent annotation with some databases:
MySQL
Oracle
PostgreSQL
SQL Server
DB2
Even if you are still using an RDBMS as a data store, you should consider storing this binary data into a file system, and saving the directory / location of the path into the database, instead of storing this as a BLOB or CLOB into the database.

Java - ReadObject with nio

In a traditional blocking-thread server, I would do something like this
class ServerSideThread {
ObjectInputStream in;
ObjectOutputStream out;
Engine engine;
public ServerSideThread(Socket socket, Engine engine) {
in = new ObjectInputStream(socket.getInputStream());
out = new ObjectOutputStream(socket.getOutputStream());
this.engine = engine;
}
public void sendMessage(Message m) {
out.writeObject(m);
}
public void run() {
while(true) {
Message m = (Message)in.readObject();
engine.queueMessage(m,this); // give the engine a message with this as a callback
}
}
}
Now, the object can be expected to be quite large. In my nio loop, I can't simply wait for the object to come through, all my other connections (with much smaller workloads) will be waiting on me.
How can I only get notified that a connection has the entire object before it tells my nio channel it's ready?
You can write the object to a ByteArrayOutputStream allowing you to give the length before an object sent. On the receiving side, read the amount of data required before attempting to decode it.
However, you are likely to find it much simpler and more efficient to use blocking IO (rather than NIO) with Object*Stream
Edit something like this
public static void send(SocketChannel socket, Serializable serializable) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for(int i=0;i<4;i++) baos.write(0);
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(serializable);
oos.close();
final ByteBuffer wrap = ByteBuffer.wrap(baos.toByteArray());
wrap.putInt(0, baos.size()-4);
socket.write(wrap);
}
private final ByteBuffer lengthByteBuffer = ByteBuffer.wrap(new byte[4]);
private ByteBuffer dataByteBuffer = null;
private boolean readLength = true;
public Serializable recv(SocketChannel socket) throws IOException, ClassNotFoundException {
if (readLength) {
socket.read(lengthByteBuffer);
if (lengthByteBuffer.remaining() == 0) {
readLength = false;
dataByteBuffer = ByteBuffer.allocate(lengthByteBuffer.getInt(0));
lengthByteBuffer.clear();
}
} else {
socket.read(dataByteBuffer);
if (dataByteBuffer.remaining() == 0) {
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(dataByteBuffer.array()));
final Serializable ret = (Serializable) ois.readObject();
// clean up
dataByteBuffer = null;
readLength = true;
return ret;
}
}
return null;
}
Inspired by the code above I've created a (GoogleCode project)
It includes a simple unit test:
SeriServer server = new SeriServer(6001, nthreads);
final SeriClient client[] = new SeriClient[nclients];
//write the data with multiple threads to flood the server
for (int cnt = 0; cnt < nclients; cnt++) {
final int counterVal = cnt;
client[cnt] = new SeriClient("localhost", 6001);
Thread t = new Thread(new Runnable() {
public void run() {
try {
for (int cnt2 = 0; cnt2 < nsends; cnt2++) {
String msg = "[" + counterVal + "]";
client[counterVal].send(msg);
}
} catch (IOException e) {
e.printStackTrace();
fail();
}
}
});
t.start();
}
HashMap<String, Integer> counts = new HashMap<String, Integer>();
int nullCounts = 0;
for (int cnt = 0; cnt < nsends * nclients;) {
//read the data from a vector (that the server pool automatically fills
SeriDataPackage data = server.read();
if (data == null) {
nullCounts++;
System.out.println("NULL");
continue;
}
if (counts.containsKey(data.getObject())) {
Integer c = counts.get(data.getObject());
counts.put((String) data.getObject(), c + 1);
} else {
counts.put((String) data.getObject(), 1);
}
cnt++;
System.out.println("Received: " + data.getObject());
}
// asserts the results
Collection<Integer> values = counts.values();
for (Integer value : values) {
int ivalue = value;
assertEquals(nsends, ivalue);
System.out.println(value);
}
assertEquals(counts.size(), nclients);
System.out.println(counts.size());
System.out.println("Finishing");
server.shutdown();

Categories