Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Limit character read using Reader?  RSS feed

 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have to read a Clob from oracle, and using Reader API to read. I want to limit amount of character to read because some of them are huge, and when it render to the browser it stop .The following code that I have is read the whole thing, How do i set the limit of character on this
Many thanks in advance


Please help thanks
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to read from a large CLOB and send the data to the browser? Then avoid solutions which store the entire CLOB in memory, or you will run out of memory. Just read from the CLOB and write to the browser.
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right now it works fine, but I jsut got some huge CLOB and want to limit on the character or whatever length, so how do I do it using my block of code above
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. In your loop keep track of how many characters you have read so far. If that number exceeds some cutoff value then break out of the loop.
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply, but is there a API that can do it already in the Reader API , I try to use the getSubString(start,end) and return a String, but not working.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could write a wrapper class for Reader that did that. But why would you want to go to all that trouble when you could just add four simple lines of code to what you have there?
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so I tried this but it not work
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Sigh - this was written before your last post]

The code you have so far doesn't really make sense - it won't even compile. It also doesn't make any sense to close the reader inside the while loop - if there's any chance of needing to read again from the reader, you should close the reader after you're done with it. Thus, close the reader outside the while loop, after it's done. First get your loop to work correctly at all - then modify it to count how many characters it's read, and exit the loop once some limit has been exceeded.
[ April 23, 2008: Message edited by: Jim Yingst ]
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I modified my code; and this is what I have, it does compile and spit out the output that read from the CLOB,

so the character read is getting from the count , but when I print it out it is 1024 and inside the loop it finally get to -1

THANKS IN ADVANCE

 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you executing charread++? Let's say you just read 100 chars. Executing charread++ makes it 101. What does that 101 mean? What's it good for? (Answer: nothing, really.) If charread is the number of chars that were read in a single read, then I think you need a second variable to keep track of the total number of chars read in all reads. Every time you execute the loop, add the new value of charread to the total.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sigh. Once again, you posted new code immediately after I started writing a post. My last post applies to your previous post, with charread++ of course. However the suggestion about getting a second variable to keep track of the total from all reads, that's still valid.
[ April 23, 2008: Message edited by: Jim Yingst ]
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim but still not work
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, once again, the code you're showing us can't possibly be something that compiled. The "break();" tells me that. Please, show us the code you're actually using, not some code that looks sort of similar to it.

Also, when your program "doesn't work", what happens? Do you get a compile error? Runtime error? No error, but the result is incorrect? Whichever is the case, what does the output say?

Your main problem is probably here:

This is not adding a number to the total - it's just replacing the old "total" with a new value. To add the count, you need to add the count to the current value of total:


Another issue is that the break statement probably isn't in the right place. If you read more than 200 characters, you still should probably write some of those characters before you break out of the loop. Can you put that break statement in a slightly different place that allows you to write the results first?

Also:

That line can read up to 1024 characters, not just one. The read() methods that accept an array parameter are capable of reading many characters at once.

Something to consider: if you want to stop reading after 200 characters, why would you need a char[] buffer that takes 1024 characters? That seems a little strange.
[ April 23, 2008: Message edited by: Jim Yingst ]
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks you Jim: but i want to point out that the code WORKS, There is no compile error, it ran alright with the posted code.
<b> The only thing now I need to get the count when each character got read ?? </b>
I do want to read the whole CLOB, but if some clob is huge , I need to limit it back to certain characters and write it out. The "200" is just an arbitrary number .

Thanks in advanceI NEED TO GET THE COUNT WHEN EACH CHARACTER GOT READ
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Jay]: Thanks you Jim: but i want to point out that the code WORKS, There is no compile error, it ran alright with the posted code.

I believe that you had some code that compiled successfully, but that is not the code that you posted. There is no way you got this to compile:

Because break is a keyword, it can't be a method name. And you have it looking like a method name, followed by parentheses. That will never ever compile in Java. Please, just copy and paste the code - don't change it.

[Jay]: The only thing now I need to get the count when each character got read ?? </b>
I do want to read the whole CLOB, but if some clob is huge , I need to limit it back to certain characters and write it out. The "200" is just an arbitrary number .


OK, that makes sense. Just for testing purposes, you might try reducing the size of the buffer to something smaller than 200 - like 10 for example. Then you can test your code reading somethign with more than 10 characters (but less than 200) to see how it works.

[Jay]: I NEED TO GET THE COUNT WHEN EACH CHARACTER GOT READ[/QB]

Well, my previous comments about count and total still apply. You're getting the count fine, but you need to add the count to the total. I even showed what I meant, in code. Have you tried that?
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim: I am going to retry this morning,
Many thanks!!!
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim:
This is the code the works, no error, giving me the limit of character read, the only thing that it does not gave me back the position 1 of character to 200 , it give back the middle or somewhere else in the document.

Let say my CLOB that I read have the following:
one two three four five six seven eight nine ten eleven twelve, thirteen..

Using the belowed code it spit back me like start at "three four five six", not the beginning of the string?

any ideas? Many thanks in advance

 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Jay]: This is the code the works, no error

No, it isn't. This is getting tiresome. How hard can it be to copy and paste?

The way your code is now, the only time you write a result is when the count is over 200. What should happen if it's less?
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Jim, I work on the different machine, where the actual code located and no internet connection, therefore I have to retype the code itself again
So, now I change the condition to If (count < 200) then spit out, so this count represent how many character has been read up too, right? so I am trying to do if it read up to 200 character, then print out character 0-200 to the writer. Am I make sense? Any sugesstion? HInt..

Greatly greatly appreciated
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And in that code, what condition causes you to break out of the loop?
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The loop will break when the cnt <==200, then write to the writer , break out of the while loop?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what happens if there was an earlier read where the count was only 100? That data never got written.

It may be time to step back and write a fresh algorithm. What about something like this (pseudocode):
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jay lai:
The loop will break when the cnt <==200, then write to the writer , break out of the while loop?
Yes, that's what happens. Is that what should happen?

I suggest that calling the variable "cnt" was not a helpful choice. What does "cnt" mean anyway? If you had called it "totalCharactersReadSoFar" you might find it easier to understand what it means for breaking out of the loop to happen when "totalCharactersReadSoFar <= 200".
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!