All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.daioware.net.http.client.FileHandler Maven / Gradle / Ivy

There is a newer version: 1.1.3-RELEASE
Show newest version
package com.daioware.net.http.client;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import com.daioware.file.MyRandomAccessFile;
import com.daioware.net.http.HttpResponse;

public class FileHandler implements ResponseHandler{

	private File file;
	private HttpSender sender;
	private MyRandomAccessFile raFile;
	
	public void handleHeaders(byte[] bytes, int offset, int length)throws HandlingException {
		
	}
	@Override
	public void handleBody(byte[] bytes, int offset, int length)throws HandlingException {
		try{
			raFile.write(bytes, offset, length);
		}catch(IOException e) {
			throw new HandlingException(e);
		}
	}
	@Override
	public void open(HttpSender sender)throws HandlingException  {
		this.sender=sender;
		if(raFile!=null) {
			return;
		}
		try {
			file=new File(sender.getRequest().getFileName());
		} catch (UnsupportedEncodingException e1) {
			throw new HandlingException(e1);
		}
		try {
			raFile=new MyRandomAccessFile(file, "rw");
		}catch(IOException e) {
			throw new HandlingException(e);
		}
		
	}
	@Override
	public void close()throws HandlingException  {
		HttpResponse resp;
		if(raFile==null) {
			return;
		}
		try {
			raFile.close();
			raFile=null;
			resp=sender.getResponse();
			if(resp!=null) {
				resp.setFile(file);
			}
		} catch (IOException e) {
			throw new HandlingException(e);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy