ByteArrayContentRader.java :  » Google-tech » jrest4guice » org » jrest4guice » rest » reader » Java Open Source

Java Open Source » Google tech » jrest4guice 
jrest4guice » org » jrest4guice » rest » reader » ByteArrayContentRader.java
package org.jrest4guice.rest.reader;

import java.io.ByteArrayOutputStream;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import org.jrest4guice.client.ModelMap;

/**
 * 
 * @author <a href="mailto:zhangyouqun@gmail.com">cnoss (QQ:86895156)</a>
 * 
 */
@SuppressWarnings( { "unchecked", "unused" })
public abstract class ByteArrayContentRader extends ParameterPairContentRader {
  @Override
  public void readData(HttpServletRequest request, ModelMap params,
      String charset) {

    super.readData(request, params, charset);

    // http body
    try {
      ServletInputStream inputStream = request.getInputStream();
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      byte[] b = new byte[4096];
      for (int n; (n = inputStream.read(b)) != -1;) {
        baos.write(b);
      }
      this.processByteArray(params, baos.toByteArray(), charset);
      baos.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  protected abstract void processByteArray(ModelMap params, byte[] bytes,
      String charset);
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.