Results 1 to 12 of 12
  1. #1
    divakarnvs is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default Problem with PipedInputStream

    Hi..

    Here my task is to consume the input stream and convert to byte array, and write to the log (LOG.i)

    Meanwhile, I need to reconstruct a new input stream and return to the caller..


    While reading the data from PipedInputStream, after reaching End of Stream it is hanging. I am not able to come out of that loop.

    Can anybody please help me to resolve this issue.


    public static InputStream logPayload(InputStream is,String LOGNAME) throws ServiceClientException{

    InputStream inputStream = null;

    if(is instanceof PipedInputStream){

    try{

    PipedInputStream pis = (PipedInputStream)is;

    BufferedInputStream bis = new BufferedInputStream(pis,1024);

    StringBuffer sb = new StringBuffer();

    int p = 0;
    byte[] b = new byte[1024];
    try {
    while ((p = bis.read(b, 0, 1024)) != -1)
    sb.append(new String(b, 0 , p));

    } catch (IOException ioe) {}


    String content = sb.toString();

    Log.i(LOGNAME+" : "+"Request Payload",content);

    inputStream = new ByteArrayInputStream(content.getBytes("UTF-8"));


    bis.close();

    }catch (Exception e) {
    throw new ServiceClientException(e);
    }

    return inputStream ;
    }


    Thanks and Regards
    Divakar

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    Java Code:
    } catch (IOException ioe) {????}
    You shouldn't ignore exceptions. Add a printStackTrace here just in case.

    after reaching End of Stream it is hanging. I am not able to come out of that loop.
    How do you know when EoS happens? Is that when the -1 is returned by the read? I 'm not sure what you are saying.
    If the read returns a -1, the while loop should exit.
    Are you saying that EoS doesn't cause the read to return a -1?

    Have you looked at what is returned by available() before trying to read and being blocked waiting for input.

  3. #3
    divakarnvs is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    Hi Norm,

    Thanks for your Response.

    As you said, Before reading the Stream, I checked the size using available(),
    It is returning size as "304".

    I could able to workout the task using the following logic, but there I am using separate Thread to perform read operation and Thread.sleep() to get the resp back, then only it is working..

    But I shouldn't use the Thread.Sleep() as per the requirements. Thats why I strucked up.

    If I am using this following logic, I could able to see my request payload data in the logger console.

    Please look at the following code once.



    public static InputStream logPayload(InputStream is,String LOGNAME) throws ServiceClientException{

    Log.i(LOGNAME,"is.available() : "+is.available());
    InputStream inputStream = null;

    if(is instanceof PipedInputStream){

    try{
    PipedInputStream pis = (PipedInputStream)is;
    PipedOutputStream pos = new PipedOutputStream();

    final BufferedInputStream bis = new BufferedInputStream(pis);
    final BufferedOutputStream bos = new BufferedOutputStream(pos);
    final StringBuffer sb = new StringBuffer();



    new Thread(new Runnable() {
    public void run() {

    int p = 0;
    byte[] b = new byte[1024];
    try {
    while ((p = bis.read(b, 0, 1024)) != -1) {
    bos.write(b, 0 , p);
    sb.append(new String(b, 0 , p));
    }
    } catch (IOException ioe) {}
    }
    }).start();


    Thread.sleep(500);

    String content = sb.toString();

    Log.i(LOGNAME+" : "+"Request Payload",content);

    inputStream = new ByteArrayInputStream(content.getBytes("UTF-8"));


    bis.close();

    }catch (Exception e) {
    throw new ServiceClientException(e);
    }
    }
    }

    Regards,
    Divakar

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    That PipedInputStream is (most likely) coupled to a PipedOutputStream and won't return a -1 (end of stream indicator) until the output stream was closed. Otherwise it will simply block in a read operation.

    kind regards,

    Jos

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    catch (IOException ioe) {????}

  6. #6
    divakarnvs is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    Hi JosAH And Norm,

    Thanks a lot for your Help.
    Now I could able to resolve the issue with the following change.

    public static InputStream logPayload(InputStream is,String LOGNAME) throws ServiceClientException{

    InputStream inputStream = null;

    if(is instanceof PipedInputStream){

    try{
    PipedInputStream pis = (PipedInputStream)is;
    PipedOutputStream pos = new PipedOutputStream();

    final BufferedInputStream bis = new BufferedInputStream(pis);
    final BufferedOutputStream bos = new BufferedOutputStream(pos);
    final StringBuffer sb = new StringBuffer();

    int p = 0;
    byte[] b = new byte[1024];
    try {
    while (!(bis.available()<1)){
    p = bis.read(b, 0, 1024);
    bos.write(b, 0 , p);
    sb.append(new String(b, 0 , p));
    }

    } catch (IOException ioe) {
    ioe.printStackTrace();
    ioe.getMessage();
    }

    String content = sb.toString();

    Log.i(LOGNAME+" : "+"Request Payload",content);

    inputStream = new ByteArrayInputStream(content.getBytes("UTF-8"));


    bis.close();

    }catch (Exception e) {
    throw new ServiceClientException(e);
    }
    }

    }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    (!(bis.available()<1))
    interesting condition test.
    Are you intentionally trying to make the code hard to understand.?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    That code can never be a solution to your problem; i.e. you create a new PipedOutputStream but you never connect it to anything.

    kind regards,

    Jos

  9. #9
    divakarnvs is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    Sorry I didn't get u clearly.

    Is there any thing wrong in checking the condition (!(bis.available()<1))

    Please let me know..

    Regards,
    Srinivas

  10. #10
    divakarnvs is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    This PipedInputStream which is coming as a paramter to this method is already connected to the PipedOutputStream at callers end. So it is working..




    public static InputStream logPayload(InputStream is,String LOGNAME) throws ServiceClientException{

    InputStream inputStream = null;

    if(is instanceof PipedInputStream){

    try{
    PipedInputStream pipedInputStream = (PipedInputStream)is;
    StringBuffer stringBuffer = new StringBuffer();

    int bytesRead = 0;
    byte[] tempBuffer = new byte[1024];
    try {
    while (!(pipedInputStream.available()<1)){
    bytesRead = pipedInputStream.read(tempBuffer , 0, 1024);
    stringBuffer.append(new String(tempBuffer, 0 , bytesRead));
    }
    } catch (IOException ioe) {
    ioe.printStackTrace();
    ioe.getMessage();
    }

    String content = stringBuffer.toString();

    Log.i(LOGNAME+" : "+"Request Payload",content);

    inputStream = new ByteArrayInputStream(content.getBytes("UTF-8"));


    pipedInputStream.close();

    }catch (Exception e) {
    throw new ServiceClientException(e);
    }
    }
    }

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by divakarnvs View Post
    This PipedInputStream which is coming as a paramter to this method is already connected to the PipedOutputStream at callers end. So it is working..
    You changed your code again; in reply#6 you had this line near the start of your try block:

    Java Code:
    PipedOutputStream pos = new PipedOutputStream();
    ... which didn't make sense.

    kind regards,

    Jos

  12. #12
    divakarnvs is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    Thanks a lot for the help..

    finally the Issue is resolved and it is working fine..

    Regards,
    Divakar

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •