Results 1 to 4 of 4
  1. #1
    lars.fi is offline Member
    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    Default Parsing a multipart-message with help of ByteArrayInputStream, loosing information?

    Hi folks,

    i just wrote a small but working code to parse a multipart message with two files binary encoded.
    Problem is, after splitting the content of the files is reduced to normal "alphabetic" digits, and i dont know why.

    I just appended my source code and a test file ( multipart ). And the result of my parsing. (part_0 = json, part_1 = file, part_2 = file)
    Unfortunately, i dont know, if the Spring FW provides an easier way of doing so at all. Haven't found it yet.

    Source:

    Java Code:
    			String requestUrl = "http://localhost:8888/";
    			RestTemplate restTemplate = new RestTemplate();
    			restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
    			HttpEntity<String> entity = new HttpEntity<String>("test");
    			ResponseEntity<byte[]> response = restTemplate.exchange(requestUrl, HttpMethod.GET, entity, byte[].class);
    
    			Files.write(Paths.get("/tmp/multipart"), response.getBody());
    
    
    			ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(response.getBody());
    			byte b;
    
    			ByteArrayOutputStream current = new ByteArrayOutputStream();
    			ByteArrayOutputStream partContent = new ByteArrayOutputStream();
    			ByteArrayOutputStream boundary = new ByteArrayOutputStream();
    
    			int part = 0;
    			while( byteArrayInputStream.available() > 0 ) {
    
    				current.reset();
    
    
    				while( (b = (byte) byteArrayInputStream.read()) > 0 && b != 0x0d) {
    					current.write(b);
    				}
    
    
    				if ( boundary.size() == 0) { // first line is boundary
    					boundary.write(current.toByteArray());
    				} else {
    
    
    					if ( current.toString().contains(boundary.toString()) ) {
    						Files.write(Paths.get("/tmp/part_" + part), partContent.toByteArray());
    						partContent.reset();
    						part++;
    					} else {
    						partContent.write(current.toByteArray());
    					}
    
    				}
    
    
    			}
    
    		} catch (Exception e) {
    			log.info("exception ", e);
    		}
    Attached Files Attached Files
    Last edited by lars.fi; 02-20-2014 at 06:36 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,358
    Rep Power
    6

    Default Re: Parsing a multipart-message with help of ByteArrayInputStream, loosing informatio

    You think the wrong way. It is not Spring's responsibility to provide you with solutions to every little requirement you can conjure up. You are programming using Java as your platform, and that's where you need to find your solution. You might be able to find a proper parser as part of the JavaMail API as it needs to be able to do these kind of things too, or else you can probably find a few open source third party parsers on the net. Rarely do you need to implement these kind of low level things yourself.

    Regardless, doing these things is still a good learning exercise. I don't really understand your problem description though.

    Problem is, after splitting the content of the files is reduced to normal "alphabetic" digits
    Huh? Can you give an example of what you are getting now and what you would expect to see?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    lars.fi is offline Member
    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    Default Re: Parsing a multipart-message with help of ByteArrayInputStream, loosing informatio

    Hi, thank you for your answer.

    F.E. :I just expected to see this:

    ˇˇJFIFC

    But i do get this:

    JFIFC


    But this was my error, when i just transformed every byte in Integer to byte[], when i read the stream...
    I switched to mime4j now, maybe I'll have some luck writing my own contentHandler.
    Last edited by lars.fi; 02-20-2014 at 08:21 PM.

  4. #4
    lars.fi is offline Member
    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    Default Re: Parsing a multipart-message with help of ByteArrayInputStream, loosing informatio

    Hi,

    i just need help after i finally got my parser "ready". Problem is, that my source is not a standard multipart and that's why no multipart-implementation works, and i have to use my own :/

    So, after i implemented the following source, i compared my binary parsing result with the binary image-source on vimdiff and attached a screenshot of the first lines.
    I just dont know, why I have those slight differences.

    Do i handle the streams current and partContent wrong?
    Every idea is welcome, thank you!
    Java Code:
    			ByteArrayOutputStream current = new ByteArrayOutputStream();
    			ByteArrayOutputStream partContent = new ByteArrayOutputStream();
                            int part = 0;
    			String boundary = null;
    
    			int b;
    			int prevB = 0;
    			while( byteArrayInputStream.available() > 0 ) { // byteArrayInputStream 
    				current.reset();
    				while( (b = byteArrayInputStream.read()) > 0 ) {
    
    					if ( b == 10 && prevB == 13 ) { // check if b == LF and prevB == CR
    						current.write(10);
    						break; // break loop
    					}
    					else current.write(b);
    
    					prevB = b;
    				}
    
    				if ( boundary == null) { // first line is boundary
    					boundary = current.toString();
    					boundary = boundary.substring(0, boundary.length()-2); // remove carriage return
    				} else {
    					if ( current.toString().contains(boundary) ) {
    						switch (part) {
    							case 0:
    								Files.write(Paths.get("/tmp/json.bin"), partContent.toByteArray()); // first part is a json value
    								break;
    							default:
    								Files.write(Paths.get("/tmp/" + part+".bin"), partContent.toByteArray()); // write binary file content
    								break;
    							}
    
    						partContent.reset();
    						part++;
    					} else {
    						partContent.write(current.toByteArray());
    					}
    
    				}
    			}
    Attached Thumbnails Attached Thumbnails Parsing a multipart-message with help of ByteArrayInputStream, loosing information?-screen-shot-2014-02-23-20.33.10.png  

Similar Threads

  1. Parsing information from a header?
    By hla40 in forum Java Servlet
    Replies: 1
    Last Post: 06-09-2011, 07:51 AM
  2. Using ByteArrayInputStream
    By nitishjtm in forum New To Java
    Replies: 8
    Last Post: 10-03-2010, 10:36 PM
  3. Chess, Variable Loosing it.s Value
    By michail in forum New To Java
    Replies: 6
    Last Post: 01-24-2010, 02:54 PM
  4. Parsing of information on EMBL
    By akshayshah in forum Advanced Java
    Replies: 5
    Last Post: 12-04-2009, 10:14 AM
  5. ByteArrayInputStream and ByteArrayOutputStream
    By new_2_java in forum New To Java
    Replies: 6
    Last Post: 07-31-2008, 11:02 PM

Posting Permissions

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