InputStreamRead read(char[] cbuf, int offset, int length) method hangs

This method hangs for the following values
read(xchar, 0, 1)
Does not throw any exception.
The inputstream does have data in it.
the characterset is cp037.

So the problem is probably in your implementation of InputStream. Do you implement the available() method? What will your class do if you attempt to read more bytes than there are? (say, call read(chars, 0, 1000000))
The InputStreamReader keeps an internal buffer (of 1024 bytes?) which it uses for decoding the bytesto characters..if your stream blocks when InputStreamReader is filling its buffer you'd get this sort of behaviour.

Similar Messages

  • InputStreamReader read(char[] cbuf, int offset, int length) method hangs

    Hi all,
    I am posting this message again.
    I would appreciate any help, as I am stuck with a customer issue.
    This method hangs for the following values
    read(xchar, 0, 1)
    Does not throw any exception.
    The inputstream does have data in it.
    the characterset is cp037.
    I know for sure that the InputStream has data in it. In fact the read method reads couple of fields with length greater than 1 and hangs when it tries to read data with length 1.
    Bug # 4401798 talks about some error similar to this being fixed. Any one has any idea, which jdk had this problem and which one has a fix for this?
    Thanks.

    You should have continued in your original thread, rather than starting a new one. What you have posted in this new thread could have gone in the old one:
    http://forum.java.sun.com/thread.jspa?threadID=665303

  • How to user bufferedreader.read(char[] cbuf,int off,int len)

    how to user bufferedreader.read(char[] cbuf,int off,int len)?
    can you give me an sample code? i dont understand the use of offset here... thanks!

    The offset simply gives you the ability to read in data starting at some point in the buffer other than the first character.
    If, for example, you are reading from some stream that is being filled slower than you are reading, then you can write a read loop that will fill the buffer with successive reads - the first at character 0, then next offset past the end of the first set of data, etc.

  • Need official interpretation - java.io.Reader read(char[] cbuf) method

    Hello Everyone,
    I need Sun API's official interpretation on java.io.Reader read(char[] cbuf) method;
    Say, I provide this read method a char[] of size 8096, could the implementing class return even before reading into the whole buffer array? Say, the implementing class just reads 1024 chars and return 1024 as the method output, has the implementing class fulfilled its obligation?
    In my mind, the read method should attempt to read and fill the buffer array until of course i/o error occurs or end of stream is reached? See BufferedReader source for this type of implementation.
    Or could the read method just attempt to read into the buffer (with no obligation) and can return before completely filling the buffer?
    Sincere Regards.

    This is the situation: (and we are caught in between arbitrating on whom to ask to make the fix)
    Please note that I am not taking any names here, but remember these vendors are the biggest in their market.
    Vendor Db A has an API wherein it returns a stream and its length;
    Vendor B consumers this API via the java.io.Reader class as follows:
    char[] data = new char[(int) VendorAStream.length()];
    VendorAStream.read(data);
    Vendor B contends that per java.io.Reader API, it is responsibility of Vendor A to implement the read method correctly!!! (which is to read the data completely, not partially); Vendor B further contends that their code works fine with all the other Db vendors (except Vendor A); [Quite *tupid reasoning in my mind ;-) ]
    Vendor A says per java API, we can partially fill the buffer (they are specifically only reading 1024 chars from the stream)
    Initially, I thought, Vendor B must fix the code to handle partial reads. But no where it is mentioned clearly that Reader.read only attempts to read the chars and can return before filling the buffer.
    Dannyyates,
    (*My interpretation*) The reason it says "*maximum* number of characters to read" is for the situation when end of stream is reached before fully filling the buffer. In that case, the length of buffer and number of actually chars read wont match. This explanation covers for your observation (c)
    BIJ001, No problem in your explanation. I understand and I am merely pointing you to alternate interpretation of the API.
    Please comment. Thanks for your time to discuss this.
    Sincere Regards.

  • Memory leak in String(byte[] bytes, int offset, int length)

    Has anyone run into memory leak problem using this String(byte[] bytes, int offset, int length) class? I am using it to convert byte array to string, and I am showing memory leak using this class. Any idea what is going on?

    Hi,
    If you post in Native methods forum I assume you are using this constructor in the native side.
    Be aware that getting char * from jstring eats memory that you must free before returning from native with env->ReleaseStringUTFChars().
    --Marc (http://jnative.sf.net)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Question about public void characters(char []ch, int start, int length) thr

    Can anyone tell me how you would keep all the characters from within one pair of tags together each time characters() is called?
    the character() method doesn't read all the character data at once, and i would like to create a buffer of what it has read.
    thank you.

    I recommend using getNodeName and/or/combination
    with
    getNodeValue to get the strings.i have tried using a buffer, and i have got that to work!
    At long last I am seeing the string I would like to see as it is in the xml file!
    thank you for your help
    here is an example of my code:
    private boolean isDescription = false;
    StringBuffer sb = new StringBuffer();
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
         if(localName.equals("description")) {
              sb = new StringBuffer();
              isDescription = true;
    public void characters(char []ch, int start, int length) throws SAXException {
         if(isDescription == true) {
              String desc = new String(ch, start, length);
              sb.append(desc);
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
         if(localName.equals("description")) {
              isDescription == false;
              System.out.println(sb.toString());
    }

  • Converting chars to ints, get a strange math error...

    Here's the code I have.
    import java.util.*;
    public class Encryption {
         public static void main(String[] args) {
              Scanner sc1 = new Scanner(System.in);
              System.out.print("Enter a four-digit integer to be encrypted: ");
              String encrypt = sc1.nextLine();
              char a = encrypt.charAt(0);
              char b = encrypt.charAt(1);
              char c = encrypt.charAt(2);
              char d = encrypt.charAt(3);
              int as = new Integer(a).intValue();
              int bs = new Integer(b).intValue();
              int cs = new Integer(c).intValue();
              int ds = new Integer(d).intValue();
              int codea = (as + 7);
              while (codea > 9) {codea = codea - 10;}
              int codeb = (bs + 7);
              while (codeb > 9) {codeb = codeb - 10;}
              int codec = (cs + 7);
              while (codec > 9) {codec = codec - 10;}
              int coded = (ds + 7);
              while (coded > 9) {coded = coded - 10;}
              int finala = codec;
              int finalb = coded;
              int finalc = codea;
              int finald = codeb;
              String encrypted = ("" + finala + "" + finalb + "" + finalc + "" + finald + "");
              //System.out.println ("The encrypted number is " + encrypted + ""); set aside while debugging
              System.out.println(" " + a + " " + b + " " + c + " " + d + " ");
              System.out.println(" " + as + " " + bs + " " + cs + " " + ds + " ");
    }Now here's the console.
    Enter a four-digit integer to be encrypted: 1234
    1 2 3 4
    49 50 51 52
    When I went from char to int, 1 became 49, 2 became 50, etc...how did this happen?

    class Fubar1
        public static void main(String[] args)
            System.out.println("'1' = " + '1'); // prints the char
            System.out.println("(int)'1' = " + (int)'1'); // prints the ASCII (unicode?) number that represents that char
    }

  • InputStreamReader: reads too much?

    i read text from an InputStreamReader:
    int count;
    char[] chars = new char[2048];
    while ((count = isr.read(chars, 0, chars.length)) != -1) {
        textArea.append(new String(chars));
    }and noticed that too much text is read! I i increase chars (e.g. 4096 bytes), more additional text is read. It seems, that the read() method for the last call does not clear 'chars' and therefore has additional (old) characters in the 'chars' buffer(?). how can this be avoided?
    (note: I need to read from inputstreamreader, not e.g. from a BufferedReader, as ProgressMonitorInputStream can't be used with Readers.)

    well, again: of course, it appends the complete array
    chars[] but it doesn't clear the contents before each
    read! a workaround would be:
    int count;
    char[] chars = new char[2048];
    while ((count = isr.read(chars, 0, chars.length)) !=
    -1) {
    textArea.append(new String(chars));
    chars = new char[2048]; //fix: clear content for
    for next read
    }to not have the content of the previous read in the
    chars[] array. but this should be done by the read()
    method in the first place.No, the approach should be -
    int count;
    char[] chars = new char[2048];
    while ((count = isr.read(chars, 0, chars.length)) != -1) {
    textArea.append(new String(chars, 0, count));
    Roger

  • Reading char from keyboard to stop threads?

    I want to kill all the threads an terminate my program execution when I enter some char, for example 'S'.
    BUT, I dont want the execution flow to wait in the sentence 'prompt.read()' until I enter some character.
    does somebody knows the solution?
    this is my program:
    import java.io.*;
    public class Finalizadora extends Thread {
         public static boolean ejecucion = true;
         public void run() {
              while (ejecucion) {
                   try {
                        sleep(1000);
                   } catch (Exception e) {
                        // Si falla el sleep lo simulamos
                        for (int i = 0; i < 1000 * 10; i++) {
                        } // fin del for
                   } // fin del try
                   try {
                        BufferedReader prompt = new BufferedReader(new InputStreamReader (System.in));
                        if (prompt.read() == 'S') {
                            ejecucion = false;
                        } // fin del if
                   } catch (Exception e) {
                        System.out.println("SOME FAILS!!");
                   //System.out.println("--------------------");
              } // fin del while
              System.out.println("ENDING...");          
         } // fin de parsear
    } // fin de Finalizadorathanks!

    Hello.I cant read chars from a file.I am trying to fing to character with Max Occurrence in a file but when I run it it stops abruptly.This is my code.Can you help me please.Thank you
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    * @author Dimitrios
    /*Assuming that the char with the max frequency in an standard English test is 'E' we can guess
    * that the char with the max frequency in the cipher text is E.This will
    * lead us to an approximation of the Key by shifting */
    import java.io.*;
    public class Frequency_Decprypt {
    public static int Occurrence(String str, char ch) {
    int occur=0;//the occurrence of char ch in the string
    for(int i=0;i<str.length();i++){
    if(str.charAt(i)==ch){
    occur++;
    return occur;
    public static char Frequency() throws IOException{
    BufferedReader in;
    FileReader fr;
    int key=0;
    char Max_FreqChar=' ';//the char with the max frequency
    int Max_Freq=0;// the max frequency of a char
    try{
    fr=new FileReader("Encrypted.txt");//the input file to read
    in=new BufferedReader(fr);
    //read a line from file
    String s=in.readLine();
    while(s!=null){
    for(int i=0;i<s.length();i++){
    if(Occurrence(s,s.charAt(i))>=Max_Freq){
    Max_Freq=Occurrence(s,s.charAt(i));
    Max_FreqChar=s.charAt(i);
    s=in.readLine();
    fr.close();
    //key=(int)('e'-Max_FreqChar);
    //key=Math.abs(key);
    } catch (FileNotFoundException e)
    { System.out.println("File not found");}
    return Max_FreqChar;
    }

  • Reading char fail

    Hi everybody!
    Reading one char at a time from a file and saves where i last read.
    Next time when i start reading chars from where i last read in the file i
    only get char "?", and it reads chars like that the entire time. Any advice ?
    br.skip(last_read_in_file);
    while(readed_char != '\n'){
         if((int)(readed_char = (char) br.read()) != -1){
              if(readed_char != '\n'){
                   line_x = readed_line;
                   readed_line = new char[readed_line.length + 1];
                   for(int i=0;i<line_x.length;++i)
                        readed_line[i] = line_x;
                   readed_line[readed_line.length - 1] = readed_char;
         if(read_no_of_chars >= size_of_file){
              readed_char = '\n';
         System.out.println("readed_char: " + readed_char);
         ++read_no_of_chars;

    The FIRST time i read the file there is no problem when i'm reading chars.
    The SECOND time it returns "?" or 65535. There has to be something with skip(long n) ?
    fx = new File("C:\\Temp\\Log.txt");
    // Get the size of the file.
    noOfChars = (int)fx.length();
    // If last_read_in_file is smaller then the size of
    // of the file, it means that new errors
    // has been added to the file. Otherwise,
    // jump out of this error-detecting function.
    if (last_read_in_file >= noOfChars) {
         logger.info(fx.getName() + " - done, no changes in file.");
         return;
    // Initiate file-readers.
    in = new FileInputStream(fx);
    fr = new InputStreamReader(in, charset);
    br = new BufferedReader(fr);
    // Jump to the latest read line.
    read_no_of_chars = last_read_in_file;
    try {
         in.skip(last_read_in_file);
         fr.skip(last_read_in_file);
         br.skip(last_read_in_file);
    } catch (IOException e1) {
         e1.printStackTrace();
    // Reading a line from the file which, perhaps,
    // contains errors. In this while-loop a char is
    // being read one at the time and is being added
    // in a char-array. Thanks to the CharSet, when
    // we're at the end of the file, it won't
    // return the value -1. So instead, we have a
    // word-counter that we compare with 'noOfChars'
    // to see if we're at the end of the file.
    while(readed_char != '\n'){
         if((int)(readed_char = (char) br.read()) != -1){
              if(readed_char != '\n'){
                   line_x = readed_line;
                   readed_line = new char[readed_line.length + 1];
                   for(int i=0;i<line_x.length;++i)
                        readed_line[i] = line_x;
                   readed_line[readed_line.length - 1] = readed_char;
         if(read_no_of_chars >= noOfChars){
              readed_char = '\n';
         System.out.println("readed_char: " + readed_char + " (" + (int)readed_char + ")");
         ++read_no_of_chars;
    temp = new String(readed_line);

  • Problem while using read(char[]) method of stream

    Hi I am trying to read different xml files, , hence i am using a buffered reader, and the read(char[]) method in the bufferereader.
    //rawContent - is the xml file content
          BufferedReader br = new BufferedReader(new InputStreamReader(rawContent,
              ENCODING_FORMAT));
          int ascii = 0;
          char ch[]= new char[200];
          while((ascii = br.read(ch)) != -1) {       
            rawContentBuffer.append(ch);
          }This is the error that i get when i execute the above piece of code. I have checked the files they have no white spaces after the xml contents.
    Please tell me how i can overcome this.
    2006-02-17 19:43:52,826 ERROR [com.test.web.taglib.LightTag] - Error in parsing the XHTMLContent is not allowed in trailing section.
    org.xml.sax.SAXParseException: Content is not allowed in trailing section.
          at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
          at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
          at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
          at com.test.web.taglib.LightTag.renderField(LightTag.java:293)
          at com.test.web.taglib.LightTag.doStartTag(LightTag.java:128)

    Maybe it isn't. I don't know what 'rawContentBuffer' is, but you're assuming that 'ch' is always full when you call rawContentBuffer.append(ch). You need to call rawContentBuffer.append(ch,0,ascii) or some such form if there is one, or make some other arrangement for handling short reads.

  • ABEND RS_EXCEPTION (000): Part-field access (offset = 45, length = 45) ...

    Hi BI Experts,
    We are getting error below when run the query in BEx Broadcaster whereas the same query can be run without error in RSRT. One of the object used in the query is 0TCTBISBOBJ (BI Application Obj). When i remove this object from the query, i am able to execute the query in BEx Broadcaster. Please advice.
    Error: com.sap.ip.bi.base.application.exceptions.AbortMessageRuntimeException
    com.sap.ip.bi.base.exception.BIBaseRuntimeException ERROR
    com.sap.ip.bi.base.application.exceptions.AbortMessageRuntimeException
    Log ID: 00144F25CCDC303C0000010600005CBD00046ADC8504A84D
    Initial cause
    ABEND RS_EXCEPTION (000): Part-field access (offset = 45, length = 45) to a data object of the size 60 exceeds valid boundaries.
      MSGV1: Part-field access (offset = 45, length = 45) to a
      MSGV2: data object of the size 60 exceeds valid
      MSGV3: boundaries.
    Stack trace: com.sap.ip.bi.base.application.exceptions.AbortMessageRuntimeException: Termination message sent
    We have recently upgraded to SAP BI7.0 SP19.
    Thanks in advance.
    Best Regards,
    Pei Fung

    Can someone throw some light on this? Thanks.

  • How to read char from console

    Hi can any body help me..
    I want to read char from keybord. withought pressing ENTER.
    I am not sure how i can do it. I can't use
    BufferedReader br = new  BufferedReader(new InputStreamReader ( System.in )) ;
    char key ;
    key = (char )br.read() ;
    cuz i have to press ENTER every time to read char.
    Thanks in advance.

    try this,I' have found a part on Internet
    package exercises;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    * questa � la classe d' esempio per leggere l'input dalla console*/
    public class Echo {
    public static void main(String args[]) throws Exception{
    // This is where the magic happens. We have a plain old InputStream
    // and we want to read lines of text from the console.
    // To read lines of text we need a BufferedReader but the BufferedReader
    // only takes Readers as parameters.
    // InputStreamReader adapts the API of Streams to the API of Readers;
    // receives a Stream and creates a Reader, perfect for our purposes.
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String input = "";
    while(true){
    System.out.print("ECHO< ");
    //As easy as that. Just readline, and receive a string with
    //the LF/CR stripped away.
    input = in.readLine();
    //Is a faster alternative to: if (input == null || input.equals(""))
    //The implementation of equals method inside String checks for
    // nulls before making any reference to the object.
    // Also the operator instance of returns false if the left-hand operand is null
    if ("".equals(input)){
    break;
    }else
    // Here you place your command pattern code.
    if ("ok".equals(input)){
    System.out.println("OK command received: do something �");
    //Output in uppercase
    System.out.println("ECHO> " + input.toUpperCase());
    System.out.println("ECHO> bye bye");
    //We exit without closing the Reader, since is standard input,
    // you shouldn't try to do it.
    // For all other streams remember to close before exit.
    }

  • Best way to read chars from InputStream

    Hope this is not a too newbie question.
    Suppose I have an unbuffered InputStream inputStream, what is the best way to read chars from it (in terms of performance)?
    Reader reader = new BufferedReader(new InputStreamReader(inputStream));
    reader.read()
    or
    Read reader = new InputStreamReader(new BufferedInputStream(inputStream))
    reader.read()
    Is there a difference between the two and if so, which one is better?
    thanks.

    If you are reading using a buffer of your own, then adding a buffer for binary data is a bad idea.
    However for text, using a BufferedInputStream could be better as it reduces calls to the OS.
    If it really matters, I suggest you do a simple performance test which runs for at least a few seconds to see what the difference is. (You should runt he test mroe than once)
    Edited by: Peter__Lawrey on 20-Feb-2009 21:37

  • Read char from text file

    I am trying to read in questions, possible answers and the correct answer from a text file.
    The text file looks like this
    1. what is blah blah blah?
    a. something
    b. something else
    c. none of the above
    b
    where the last line of the file is a character which is the correct answer.
    I can read all these in as a string but I have to read the last line in as a character .
    can you please help.

    I'm not sure if this is what you need but, take a look:
    import java.io.*;
    public class questions
    public static void main(String[] args)
    try
    Question a=new Question();
    ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(new File("answers.txt")));
    out.writeObject(a);
    ObjectInputStream in=new ObjectInputStream(new FileInputStream(new File("answers.txt")));
    Question b=(Question)in.readObject();
    char answer=b.getAnswer();
    System.out.println(answer);
    catch(IOException e)
    System.out.println(e);
    catch(ClassNotFoundException e)
    System.out.println(e);
    class Question implements Serializable
    String q;
    String a1;
    String a2;
    String a3;
    char c;
    public Question()
    q=new String("The Question");
    a1=new String("1st possible choise");
    a2=new String("2st possible choise");
    a3=new String("3st possible choise");
    c='c';
    public void setQuestion(String que)
    q=que;
    public void setAnswer1(String an1)
    a1=an1;
    public void setAnswer2(String an2)
    a2=an2;
    public void setAnswer3(String an3)
    a3=an3;
    public void setAnswer(char an)
    c=an;
    public char getAnswer()
    return c;
    Ofcource all methods in Question class should be adapted to the needs of your program.
    If i helped help me too.. (duke $)

Maybe you are looking for

  • How to updata data from SAP EP into SAP R/3 ?

    Hi, How to create the material from SAP CE 7.1 into SAP R/3. Is BAPI  the only way for this or there is some other solution. Also, please explain how to use BAPI in this scenario. Regards, Yogita.

  • OPC fieldpoint VT_BOOL gives "Bad Value" in Citect

    We run Citects OPC-client and tries to communicate with the Fieldpoint OPC-server. Analog values AI/AO work all fine but DIO-550 I/O-values do not. The data type is VT_BOOL. The error message is "Bad value". The client can be configured to skip "bad

  • Warning Triangle

    How can I tell what the warning triangle in Server Admin listing means. I have one to the left of VPN. Clicking on it tells me nothing. There is also no sign in VPN log of trouble.

  • Acrobat Standard X, editing portfolio

    I need to edit a portfolio.  Acrobat.com help tells me to right click and select "edit portfolio". Unfortunately, this is not coming up in any right-click menus ANYWHERE on the screen.  Any ideas? Evelyn

  • OSB 11g - list Proxy services with enabled tracing

    Hello. I went through OSB API and haven't found out a way, how to by script (Java or WLST): - list Proxy service with enabled 'Execution Tracing' - enabled and disable 'Execution Tracing' Is there a way, how to do this by script somehow? I know, how