-1

there is a problem with byte[] byteData = new byte[32]; if I keep this inside a while loop. the output prints the exact data without repetition of the last line of the data .. why is that

    @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // TODO Auto-generated method stub

    //req.getInputStream().
    resp.addHeader("Access-Control-Allow-Origin", "*");
    resp.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, HEAD");
    resp.addHeader("Access-Control-Allow-Headers", "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept");
    resp.addHeader("Access-Control-Max-Age", "1728000");

    byte[]  byteData = new byte[32];
    InputStream inputStream = req.getInputStream();
    String res="";

    while(inputStream.read(byteData) != -1)
    {
        res=res+(new String(byteData)); 

    }

    System.out.println("Data: " + res);

    String fileName = "ServletFile.txt";

    String fileLocation = Property.getProperty("FileLocation");

    WriteFile write = new WriteFile();
    write.writeToFile(fileLocation+fileName, res);

    }

}

Expected Output:

{"graph":{"cells":[{"type":"devs.RectangleModel","inPorts":[],"outPorts":[],"size":{"width":100,"height":100},"ports":{"groups":{"in":{"position":{"name":"left"},"attrs":{".port-label":{"fill":"#000"},".port-body":{"fill":"#fff","stroke":"#000","r":10,"magnet":true}},"label":{"position":{"name":"left","args":{"y":10}}}},"out":{"position":{"name":"right"},"attrs":{".port-label":{"fill":"#000"},".port-body":{"fill":"#fff","stroke":"#000","r":10,"magnet":true}},"label":{"position":{"name":"right","args":{"y":10}}}},"top":{"z":2,"position":{"name":"top","args":{"x":50,"y":-7}},"attrs":{".port-label":{"fill":"#000"},".port-body":{"r":6,"stroke":"gray","fill":"lightgray","magnet":"active"}}}},"items":[{"group":"top","id":"I"}]},"position":{"x":128.9930419921875,"y":135.8055419921875},"angle":0,"id":"101_top","controlType":"PLAY","embeds":["101_yes","101_no"],"z":1,"attrs":{}},{"type":"devs.ChildRectangleModel","inPorts":[],"outPorts":[],"size":{"width":100,"height":30},"ports":{"groups":{"in":{"position":{"name":"left"},"attrs":{".port-label":{"fill":"#000"},".port-body":{"fill":"#fff","stroke":"#000","r":10,"magnet":true}},"label":{"position":{"name":"left","args":{"y":10}}}},"out":{"position":{"name":"right"},"attrs":{".port-label":{"fill":"#000"},".port-body":{"fill":"#fff","stroke":"#000","r":10,"magnet":true}},"label":{"position":{"name":"right","args":{"y":10}}}},"right":{"z":2,"position":{"name":"right","args":{"x":107,"y":15}},"attrs":{".port-label":{"fill":"#000"},".port-body":{"r":6,"stroke":"gray","fill":"lightgray","magnet":"active"}}}},"items":[{"group":"right","id":"R"}]},"position":{"x":128.9930419921875,"y":175.8055419921875},"angle":0,"id":"101_yes","parent":"101_top","z":2,"attrs":{}},{"type":"devs.ChildRectangleModel","inPorts":[],"outPorts":[],"size":{"width":100,"height":30},"ports":{"groups":{"in":{"position":{"name":"left"},"attrs":{".port-label":{"fill":"#000"},".port-body":{"fill":"#fff","stroke":"#000","r":10,"magnet":true}},"label":{"position":{"name":"left","args":{"y":10}}}},"out":{"position":{"name":"right"},"attrs":{".port-label":{"fill":"#000"},".port-body":{"fill":"#fff","stroke":"#000","r":10,"magnet":true}},"label":{"position":{"name":"right","args":{"y":10}}}},"right":{"z":2,"position":{"name":"right","args":{"x":107,"y":15}},"attrs":{".port-label":{"fill":"#000"},".port-body":{"r":6,"stroke":"gray","fill":"lightgray","magnet":"active"}}}},"items":[{"group":"right","id":"R"}]},"position":{"x":128.9930419921875,"y":205.8055419921875},"angle":0,"id":"101_no","parent":"101_top","z":3,"attrs":{}}]},"data":[[101,{"id":101,"controlType":"PLAY","dataMap":{"playType":"","legId":"","previousKey":"","source":"","nextKey":"","itemType":"","infoRepeatCounter":"","isInterruptable":"","nodeDescription":""}}]]}

Output what Iam getting :

{"graph":{"cells":[{"type":"devs.RectangleModel","inPorts":[],"outPorts":[],"size":{"width":100,"height":100},"ports":{"groups":{"in":{"position":{"name":"left"},"attrs":{".port-label":{"fill":"#000"},".port-body":{"fill":"#fff","stroke":"#000","r":10,"magnet":true}},"label":{"position":{"name":"left","args":{"y":10}}}},"out":{"position":{"name":"right"},"attrs":{".port-label":{"fill":"#000"},".port-body":{"fill":"#fff","stroke":"#000","r":10,"magnet":true}},"label":{"position":{"name":"right","args":{"y":10}}}},"top":{"z":2,"position":{"name":"top","args":{"x":50,"y":-7}},"attrs":{".port-label":{"fill":"#000"},".port-body":{"r":6,"stroke":"gray","fill":"lightgray","magnet":"active"}}}},"items":[{"group":"top","id":"I"}]},"position":{"x":128.9930419921875,"y":135.8055419921875},"angle":0,"id":"101_top","controlType":"PLAY","embeds":["101_yes","101_no"],"z":1,"attrs":{}},{"type":"devs.ChildRectangleModel","inPorts":[],"outPorts":[],"size":{"width":100,"height":30},"ports":{"groups":{"in":{"position":{"name":"left"},"attrs":{".port-label":{"fill":"#000"},".port-body":{"fill":"#fff","stroke":"#000","r":10,"magnet":true}},"label":{"position":{"name":"left","args":{"y":10}}}},"out":{"position":{"name":"right"},"attrs":{".port-label":{"fill":"#000"},".port-body":{"fill":"#fff","stroke":"#000","r":10,"magnet":true}},"label":{"position":{"name":"right","args":{"y":10}}}},"right":{"z":2,"position":{"name":"right","args":{"x":107,"y":15}},"attrs":{".port-label":{"fill":"#000"},".port-body":{"r":6,"stroke":"gray","fill":"lightgray","magnet":"active"}}}},"items":[{"group":"right","id":"R"}]},"position":{"x":128.9930419921875,"y":175.8055419921875},"angle":0,"id":"101_yes","parent":"101_top","z":2,"attrs":{}},{"type":"devs.ChildRectangleModel","inPorts":[],"outPorts":[],"size":{"width":100,"height":30},"ports":{"groups":{"in":{"position":{"name":"left"},"attrs":{".port-label":{"fill":"#000"},".port-body":{"fill":"#fff","stroke":"#000","r":10,"magnet":true}},"label":{"position":{"name":"left","args":{"y":10}}}},"out":{"position":{"name":"right"},"attrs":{".port-label":{"fill":"#000"},".port-body":{"fill":"#fff","stroke":"#000","r":10,"magnet":true}},"label":{"position":{"name":"right","args":{"y":10}}}},"right":{"z":2,"position":{"name":"right","args":{"x":107,"y":15}},"attrs":{".port-label":{"fill":"#000"},".port-body":{"r":6,"stroke":"gray","fill":"lightgray","magnet":"active"}}}},"items":[{"group":"right","id":"R"}]},"position":{"x":128.9930419921875,"y":205.8055419921875},"angle":0,"id":"101_no","parent":"101_top","z":3,"attrs":{}}]},"data":[[101,{"id":101,"controlType":"PLAY","dataMap":{"playType":"","legId":"","previousKey":"","source":"","nextKey":"","itemType":"","infoRepeatCounter":"","isInterruptable":"","nodeDescription":""}}]]} ","nodeDescription":""

the last word of the data "nodeDescription" is repeated and why is that?

Rishi Jagati
  • 626
  • 1
  • 6
  • 28
Shriya D.S
  • 57
  • 1
  • 6
  • Add the definition of `WriteFile` class... – OneCricketeer Apr 10 '18 at 03:54
  • 1
    Did you knew that there is a `StringBuilder` class? – Aniket Sahrawat Apr 10 '18 at 03:55
  • @cricket_007, there is a problem with byte[] byteData = new byte[32]; if u keep this inside a while loop . the output prints the exact data without repitition of the last line of the data .. why is that ? – Shriya D.S Apr 10 '18 at 03:56
  • Well, when you put that in the loop, you're clearing the array content... In any case, this post does what you're trying https://stackoverflow.com/q/309424/2308683 – OneCricketeer Apr 10 '18 at 04:01
  • @cricket_007, if i dont put that inside the loop , the last word of the data is repeated in the output .. why is that? – Shriya D.S Apr 10 '18 at 04:07
  • Please look at the post I linked to... Please explain why you need a byte array... Where did you copy that code from? – OneCricketeer Apr 10 '18 at 04:10
  • @cricket_007, I have used Byte Array for writing a file using servlets. when the written file gets displayed the last word of a file is repeated along with that. why is it getting displayed again, when byte[] byteData = new byte[32]; is put inside the loop, the repitition doesnt occur , if its is not put inside the while loop the repitition of the last word of a file occurs once – Shriya D.S Apr 10 '18 at 04:18

1 Answers1

0
byte[]  byteData = new byte[32];
InputStream inputStream = req.getInputStream();
String res="";

while(inputStream.read(byteData) != -1)
{
    res=res+(new String(byteData)); 

}

inputStream.read(byteData) reads up to 32 bytes from inputstream to buffer. When you read last bytes, it could be less then 32 bytes. So only bytes that were read will be rewritten and other bytes remain the same. What you need to use additional count variable.

int count;
while((count = inputStream.read(byteData)) != -1)
{
    res=res+(new String(byteData, 0, count)); 

}
  • byte[] byteData = new byte[32]; InputStream inputStream = req.getInputStream(); String res=""; while(inputStream.read(byteData) != -1) { res=res+(new String(byteData)); byteData = new byte[32]; } if i put this inside a loop , the last word is not rewritten why is that? – Shriya D.S Apr 10 '18 at 04:32
  • You clear the buffer. Operator new creates new buffer filled with zeros. They looks like spaces. – Bogdan Lukiyanchuk Apr 10 '18 at 04:42