aadhar sharma

Ranch Hand
+ Follow
since Oct 09, 2006
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by aadhar sharma

I am loosing data when I try to convert a stream to a CharBuffer. The following can be verified from the output below.

I see the code perfectly fine and the following works 99% of the times when the file size is small how ever the data is truncated for huge files. (3,50,000 Lines approx or more)

The behaviour is consistent and the file is cut off every single time from a specific character.

Do the CharBuffer and ByteBuffer have a limitation on the size ? If yes is there a better way to re-write this code and return CharBuffer.



Any help appreciated.


InputStreamReader reader = null;
CharBuffer cbuf = null;
// get the blob to access its length
Blob blob = getBlob(attachment);
int capacity = (int)blob.length();
System.out.println("Capacity is this "+capacity);
System.out.println("Capacity is this long"+blob.length());
Log.info(this, "BinaryAttachmentManager getBufferredReader() allocating "+capacity+" buffer with characterset "+charsetName);
cbuf = CharBuffer.allocate(capacity);

BufferedInputStream bis = new BufferedInputStream(blob.getBinaryStream());
if(null!=charsetName) {
reader = new InputStreamReader(bis,charsetName);
} else {
reader = new InputStreamReader(bis);
}
long start = System.currentTimeMillis();
Log.info(this, "BinaryAttachmentManager getBufferredReader() reading Blob bytes into CharBuffer");
int count = reader.read(cbuf);
cbuf.limit(count); //truncate any unused space
Log.info(this, "BinaryAttachmentManager getBufferredReader() read "+count+" characters, elapsed="+(System.currentTimeMillis()-start)+" msec");


return cbuf;
------------------------------------------------------------------------------------------Out Put ----------------------------------------------------------------------------

06:34:51,190 INFO BinaryAttachmentManager,main:46 - BinaryAttachmentManager getBufferredReader() allocating 16666465 buffer with characterset null
06:34:51,331 INFO BinaryAttachmentManager,main:46 - BinaryAttachmentManager getBufferredReader() reading Blob bytes into CharBuffer
06:34:53,394 INFO BinaryAttachmentManager,main:46 - BinaryAttachmentManager getBufferredReader() read 16654336 characters, elapsed=2063 msec

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


12 years ago
Is it possible to update an xml using a SAX parser.

As per my knowledge the sax parser loads the xml in chunks is it still possible to update the xml using sax if yes where can i find a sample example for the same.

My sample code for DOM which works on smaller files is below , I am unable to figure out how the following code needs to be changed so that it can work on files of size 18 MB



static List voList = new ArrayList();

public static void main(String argv[]) {
try {

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("c:\\xmlfiles\\first.xml"));

// normalize text representation
doc.getDocumentElement().normalize();
System.out.println("Root element of the doc is "
+ doc.getDocumentElement().getNodeName());

NodeList listOfBodies = doc
.getElementsByTagName("ShippedPtvDevice");
int totalBodies = listOfBodies.getLength();
System.out.println("Total no of Body Tags are : " + totalBodies);

for (int s = 0; s < listOfBodies.getLength(); s++) {

Node firstPersonNode = listOfBodies.item(s);
// MyIdsVo myIdVo = (MyIdsVo) voList.get(s);
if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {

Element firstPersonElement = (Element) firstPersonNode;
NodeList firstNameList = firstPersonElement
.getElementsByTagName("PtvID");

Element firstNameElement = (Element) firstNameList.item(0);

// firstNameElement.setTextContent(myIdVo.getAuthCode());

NodeList lastNameList = firstPersonElement
.getElementsByTagName("IntegratedReceiverDeviceSN");
Element lastNameElement = (Element) lastNameList.item(0);

// lastNameElement.setTextContent(myIdVo.getDeviceID());

NodeList textLNList = lastNameElement.getChildNodes();
System.out
.println("Last Name : "
+ ((Node) textLNList.item(0))
.getNodeValue().trim());

}

// set up a transformer
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();

// create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
String xmlString = sw.toString();

OutputStream f0;
byte buf[] = xmlString.getBytes();
f0 = new FileOutputStream("c:\\xmlfiles\\first.xml");
for (int i = 0; i < buf.length; i++) {
f0.write(buf[i]);
}
f0.close();
buf = null;
}// end of if clause
} catch (Exception e) {
System.out.println(e);
}

}

private void getParameters() {
String str, tmp, tmp1, tmp2, csvFileName = "c:\\Dummy_Devices_3_20000.prn";
StringTokenizer tok;
int lineCount = 0;

try {
FileInputStream fis = new FileInputStream(csvFileName);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
do {
str = br.readLine();
if (str == null) {
break;
}
lineCount++;
// taking tokens based on "," token separator and adding them to
// two vectors
tok = new StringTokenizer(str, ",");
try {
MyIdsVo vo = new MyIdsVo();
tmp = tok.nextToken();
vo.setAuthCode(tmp);
tmp1 = tok.nextToken();
vo.setDeviceID(tmp1);
tmp2 = tok.nextToken();
System.out.println(" " + tmp2);
voList.add(vo);

} catch (NoSuchElementException ex) {
System.out.println(ex);
}
} while (true);
fis.close();
} catch (FileNotFoundException fnofe) {
System.out.println(fnofe);
} catch (Exception e) {
System.out.println(e);
}

}

14 years ago
There should be some way of reading huge xml files in java

I guess this might not be the way but there should be a way to do it
14 years ago
Hi,

Code :

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("c:\\xmlfiles\\first.xml"));

I am trying to open an XML file using the above mentioned code and I receive the exception below, I changed my eclipse settings to
C:\eclipse_new\eclipse\eclipse.exe -vmargs -Xms256m -Xmx512M
but even this doesnt help.


Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at org.apache.xerces.dom.DeferredDocumentImpl.createChunk(Unknown Source)
at org.apache.xerces.dom.DeferredDocumentImpl.ensureCapacity(Unknown Source)
at org.apache.xerces.dom.DeferredDocumentImpl.createNode(Unknown Source)
at org.apache.xerces.dom.DeferredDocumentImpl.createDeferredTextNode(Unknown Source)
at org.apache.xerces.parsers.AbstractDOMParser.characters(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanContent(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at MySampleCode.main(MySampleCode.java:36)
14 years ago
Hi Friends,

If I change (update/edit) a Java file in my workspace and do a ANT build the changes are not reflected on the server.

However the JSP changes are reflected.

Is there a way I can update the changes to the server when my server is on.

I am using JBOSS 4.x

I know this can be done when the debugger is on.

Regards
Aadhar
15 years ago
Hay Mario Thanks,

This works fine and hence I had put this in my code but then the Quality guy got hold of me when the same date comparision failed.

since you have taken both the Date instances using the cal.set
the result is fine.

But the problem occurs when one date is from the data base and other
is made by cal.set
and then the comparision is made

hope you understand what i am saying

the cal.set has a time lag of few hundred millseconds

Date From Cal.set 1220207400321
Date From the database 1220207400000

see the difference above
15 years ago
Tried doing the same as you have suggested.
I have used the same format cal.set(2008, Calendar.SEPTEMBER, 1);
but still the problem exists


This is like 1==1 returning false.
15 years ago
I was working with the compareDates method to compare 2 dates
for some reason my code returns the getTime() with a difference

The code does not works fine when the dates are same
I have a date object from the database and the second one is created this way

Calendar cal = Calendar.getInstance();
cal.set(2008, 8, 01,0,0,0);
Date dateConstant = cal.getTime();


I know there is a mismatch between the time in milliseconds which causes the failure
I also know after can reslove my problem
but my question is why this difference ?

new date is this 1220207400345
effective date is this 1220207400000

this is my code

private static boolean compareDates(Date effectiveDate)
{
Calendar cal = Calendar.getInstance();
cal.set(2008, 8, 01);
Date dateConstant = cal.getTime();
if (dateConstant.compareTo(effectiveDate) < 0)
return true;
else if (dateConstant.compareTo(effectiveDate) > 0)
return false;
else
return true;
}
15 years ago
Keep on changing the page event

HeaderFooterPageNumber marker2 = new HeaderFooterPageNumber(payload);
writer.setPageEvent(marker2);
Hi All


I am a new user of Itext. Currently I am using Inetsoft StyleReports but we are getting performance issues so we decided to evaluate Itext. I have gone through the docs/api's but still I am a beginner.

In my application we have different reports generated based on user selection(The screen has about 5 checkboxes). Each checkbox generates a particular report and if a user selects multiple checkboxes, a combined report is generated.

Each report has its own header and footer which repeats on every page of that particular document. I have seen there is a PDFTemplate class which can be used to set a template, but my problem is if the user selects multiple checkboxes, then each report will have a different PDFTemplate. I am using a listner which attaches the PDFTemplate to a document in onStartPage() method. How can I attach multiple templates to a document.

One possible solution is to generate all pdf's separately and then combine them at the end, but it is not the best approach keeping performance in mind. What I am planning to do is to pass document object to different methods based on user's selection of checkboxes and generate the report. But I cant find a way to attach different templates.

It would be great if some one sends me an example

Thanks in Advance
Aadhar Sharma
Thanks a Ton Jai

Worked out for me
Hi,

I am creating a stateless session bean using JBoss 4 server and JDK 6.0

I receive the folowing error when I try to call the EJb from my Java Application

below is the trace i have attached my jboss.xml jndi and ejb-jar.xml.


Exception in thread "main" javax.naming.CommunicationException [Root exception is java.io.InvalidClassException: org.jboss.ejb3.session.BaseSessionRemoteProxy; local class incompatible: stream classdesc serialVersionUID = 8310915813626447181, local class serialVersionUID = 2609262789016232311]
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:722)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
at javax.naming.InitialContext.lookup(Unknown Source)
at client.Client.main(Client.java:26)
Caused by: java.io.InvalidClassException: org.jboss.ejb3.session.BaseSessionRemoteProxy; local class incompatible: stream classdesc serialVersionUID = 8310915813626447181, local class serialVersionUID = 2609262789016232311
at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at java.rmi.MarshalledObject.get(Unknown Source)
at org.jnp.interfaces.MarshalledValuePair.get(MarshalledValuePair.java:72)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:652)
... 3 more




<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
version="3.0">
<description>JBoss Stateless Session Bean Tutorial</description>
<display-name>JBoss Stateless Session Bean Tutorial</display-name>
<enterprise-beans>
<session>
<ejb-name>Calculator</ejb-name>
<remote>statelessExamples.CalculatorRemote</remote>
<local>statelessExamples.CalculatorLocal</local>
<ejb-class>statelessExamples.CalculatorBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>



<?xml version="1.0"?>
<jboss
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://www.jboss.org/j2ee/schema/jboss_5_0.xsd"
version="3.0">
<enterprise-beans>
<session>
<ejb-name>Calculator</ejb-name>
<jndi-name>statelessExamples.CalculatorRemote</jndi-name>
<local-jndi-name>statelessExamples.CalculatorLocal</local-jndi-name>
</session>
</enterprise-beans>
</jboss>




package client;

import java.util.Properties;

import javax.naming.InitialContext;

import stateless.Calculator;
import stateless.CalculatorRemote;

public class Client {
public static void main(String[] args) throws Exception {

Properties p = new Properties();
p.put("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
p.put("java.naming.factory.url.pkgs",
"org.jboss.naming rg.jnp.interfaces");
p.put("java.naming.provider.url", "localhost:1099");

// System.setProperty("java.naming.factory.initial",
// "org.jnp.interfaces.NamingContextFactory");
// System.setProperty("java.naming.factory.url.pkgs","org.jboss.naming rg.jnp.interfaces");
// System.setProperty("java.naming.provider.url", "localhost:1099");

InitialContext ctx = new InitialContext(p);
Calculator calculator = (Calculator) ctx.lookup("statelessExamples.CalculatorRemote");

System.out.println("1 + 1 = " + calculator.add(1, 1));
System.out.println("1 - 1 = " + calculator.subtract(1, 1));
}
}



Thanks and Regards

Aadhar Sharma