Wednesday, March 25, 2009
Image compressor
package com;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
public class ImageCompression
{
public static void main(String[] args) throws IOException
{
Iterator writers;
BufferedImage bufferedImage;
ImageOutputStream imageOutputStream;
ImageWriter imageWriter;
ImageWriteParam pngparams;
// Read an image from the disk (First Argument)
// bufferedImage = ImageIO.read(new File(args[0]));
bufferedImage = ImageIO.read(new File("C:\\DSC0009111370.JPG"));
// Get all the PNG writers
writers = ImageIO.getImageWritersByFormatName("jpg");
// Fetch the first writer in the list
imageWriter = (ImageWriter) writers.next();
// Just to confirm that the writer in use is CLibPNGImageWriter
System.out.println("\n Writer used : " + imageWriter.getClass().getName() + "\n");
// Specify the parameters according to those the output file will be
// written
// Get Default parameters
pngparams = imageWriter.getDefaultWriteParam();
// Define compression mode
pngparams.setCompressionMode(javax.imageio.ImageWriteParam.MODE_EXPLICIT);
// Define compression quality
pngparams.setCompressionQuality(0.5F);
// Define progressive mode
pngparams.setProgressiveMode(javax.imageio.ImageWriteParam.MODE_COPY_FROM_METADATA);
// Deine destination type - used the ColorModel and SampleModel of the
// Input Image
pngparams.setDestinationType(new ImageTypeSpecifier(bufferedImage.getColorModel(), bufferedImage
.getSampleModel()));
// Set the output stream to Second Argument
// imageOutputStream = ImageIO.createImageOutputStream( new
// FileOutputStream(args[1]) );
imageOutputStream = ImageIO.createImageOutputStream(new FileOutputStream("C:\\new_test.jpg"));
imageWriter.setOutput(imageOutputStream);
// Write the changed Image
imageWriter.write(null, new IIOImage(bufferedImage, null, null), pngparams);
// Close the streams
imageOutputStream.close();
imageWriter.dispose();
}
}
Tuesday, March 17, 2009
SPLIT A TEXT FILE TO A SPECIFIED NO. OF SUBFILES
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class SplitFile
{
/*The method splitFile(File source,int noFile,int lsize) splits the file 'source' into 'noFile' no. of individual files, each file having 'lsize' no. of characters*/
public static File[] splitFile(File source,int noFile,int lsize)
{
File[] fileFragments = new File[noFile];
String[] frgfName = new String[noFile];
try{
String sourceFName = source.getName();
long sourceFSize = source.length();
FileInputStream fis = new FileInputStream(source);
System.out.println(noFile);
if (lsize != 0) {
noFile--;
}
System.out.println(noFile);
sourceFName = sourceFName.substring(0, sourceFName.lastIndexOf("."));
int j=0;
for (int i = 1; i <= noFile; i++) {
frgfName[i-1] = "D:\\"+sourceFName + String.valueOf(i)+".txt";
fileFragments[i-1] = new File(frgfName[i-1]);
FileOutputStream fos = new FileOutputStream(fileFragments[i - 1]);
byte[] data = new byte[lsize];
int count = fis.read(data);
fos.write(data);
fos.close();
String frgFileInfo = new String(frgfName[i-1] + "," + String.valueOf(lsize));
}
if (lsize != 0) {
System.out.println(noFile);
frgfName[noFile] = "D:\\"+sourceFName + String.valueOf(noFile+1)+".txt";
fileFragments[noFile] = new File(frgfName[noFile]);
FileOutputStream fos = new FileOutputStream(fileFragments[noFile]);
byte[] data = new byte[lsize];
int count = fis.read(data);
fos.write(data);
fos.close();
String frgFileInfo = new String(frgfName[noFile] + "," + String.valueOf(lsize));
}
} catch (Exception e) {
System.out.println("Error in Splitting:"+e);
return null;
}
return fileFragments;
}
public static void main(String[] args) {
File keyfile = new File("D:/test.txt"); // the path to the source file
File[] resultFiles=null;
resultFiles=splitFile(keyfile,5,30);
for(File fileObj:resultFiles){
System.out.println(fileObj.getAbsolutePath());
}
}
}
Wednesday, January 21, 2009
use of cascade in hbm file
2) cascade="save-update" tells Hibernate to navigate the association when transaction is committed and when an object is passed to save() or
update() and save newly instantiated transient instances and persist changes to
detached instances.
3) cascade=""delete
instances when an object is passed to delete().
4) cascade="all" means to cascade both save-update and delete, as well as
calls to evict and lock.
5) cascade="all-delete-orphan" means the same as cascade="all" but, in addition,
Hibernate
(dereferenced) from the association (for example, from a collection).
6) cascade="delete-orphan" Hibernate will delete any persistent
instance that has been removed (dereferenced) from the association (for
example, from a collection).
Monday, December 22, 2008
FETCH DISTICT ROWS USING HIBERNATE
------------------------------
DetachedCriteria criteria = DetachedCriteria.forClass(DATATABLE.class);
ProjectionList projList=Projections.projectionList();
// add the restrictions to the criteria
criteria.add(Restrictions.like("teamName",
CloConstants.PERCENTAGE + name.toUpperCase() + CloConstants.PERCENTAGE));
// distinct on teamId, also adding the two properties teamId and TeamName will return only two properties from the DB.
projList.add(Projections.distinct(Projections.property("teamId)));
projList.add(Projections.property("teamName"));
// hibernateTemplate for springs
List
System.out.println("teamsList :"+teamList.size());
// The List, teamList returned will be a list of Object. In order to retrieve a list of DATATABLE objects, use setResultTransformer. But this retrieves all the columns from the table DATATABLE. criteria.setProjection(projList).setResultTransformer(Transformers.aliasToBean(DATATABLE.class));
Using Named Query
------------------------------------------
var names = (from dr in dataTable.Rows
select (string)dr["Name"]).Distinct().OrderBy(name => name);
Using Query by Example
----------------------------------------
DataTable dataTable = new DataTable ();
//Search by the name
dataTable.setName("I%");
dataTable.setTeamName (CloConstants.PERCENTAGE + teamSearchName.toUpperCase() + CloConstants.PERCENTAGE);
Example example= Example.create(dataTable);
//exclude a particular column in the table
example.enableLike();
criteria.add(example);
Monday, October 20, 2008
Both SAX and DOM are used to parse the XML document. Both has advantages and disadvantages and can be used in our programming depending on the situation
SAX:
· A SAX parser serves the client application always only with pieces of the document at any given time.
1. Parses node by node
2. Doesn't store the XML in memory
3. We cant insert or delete a node
4. Top to bottom traversing
The SAX protocol requires a lot more programming than the Document Object Model (DOM). It’s an event-driven model (you provide the callback methods, and the parser invokes them as it reads the XML data), which makes it harder to visualize. Finally, you can’t “back up” to an earlier part of the document, or rearrange it, any more than you can back up a serial data stream or rearrange characters you have read from that stream.
DOM:
· A DOM parser is rich in functionality. It creates a DOM tree in memory and allows you to access any part of the document repeatedly and allows you to modify the DOM tree. But it is space inefficient when the document is huge, and it takes a little bit longer to learn how to work with it.
1. Stores the entire XML document into memory before processing
2. Occupies more memory
3. We can insert or delete nodes
4. Traverse in any direction.
If we need to find a node and doesn’t need to insert or delete we can go with SAX itself otherwise DOM provided we have more memory.
Saturday, October 18, 2008
difference between getHibernateTemplate().load and getHibernateTemplate().get
load() just creates a proxy and does not hit the database while get() does.
With load you need to re-attach the owning session somehow, which is only possible by locking it to the saved object. You don't really want to do that if your using the OpenSessionInViewFilter / Interceptor for your views as you will end up with a session remaining locked open
get() will return null if an object is not found while load() will always return a non-null object which is a proxy. If the underlying object does not exist, the proxy will thrown ObjectNotFoundException.
load() should be used when you are sure that the object exits while get() when you're not.
Wednesday, August 20, 2008
Query Caching using Detached criteria
DetachedCriteria criteria = DetachedCriteria.forClass(/*classname.class*/);
criteria = criteria.setProjection(
Projections.distinct(Projections.property(/*Criteria*/))).addOrder(Order.asc(/*Order by criteria*/));
// Enable query caching.
criteria.getExecutableCriteria(getSession()).setCacheable(true);
List
Using sessions :
session = HibernateSessionFactory.currentSession();
List
REFACTORING
What is Refactoring? A software is built initially to serve a purpose, or address a need. But there is always a need for enhancement, fixin...
-
Both SAX and DOM are used to parse the XML document. Both has advantages and disadvantages and can be used in our programming depending on t...
-
If you have ever filled an online form, you are bound to have encountered a CAPTCHA. CAPTCHA stands for Completely Automated Public Turin...
-
Logging is an important aspect of software development, one that is many a time ignored by developers. it's important to have clean an...