private File receiveFile(String queue, String printJob, int count,
			String name, InputStream clientInStream) throws IOException {

		// file name sanity check

		checkFileName(name);

		// create print job folder if it does not exist

		File printJobFolder = createPrintJobFolder(printJob, new File(
				queueFolderName, queue));

		// check if file exists in print job folder

		File file = new File(printJobFolder, name);
		if (file.exists()) {
			throw new IOException(
					"File already exists in print job folder, name: " + name);
		}

		// receive file to print job folder

		FileOutputStream fos = new FileOutputStream(file);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		try {
			int bytesRead = 0;
			while ((count > 0) && (bytesRead < count)) {
				int bytesLeft = count - bytesRead;
				if (bytesLeft > MAX_BUFFER_SIZE) {
					bytesLeft = MAX_BUFFER_SIZE;
				}
				byte[] buf = new byte[bytesLeft];
				int bread = clientInStream.read(buf);
				if (bread == -1) {
					break;
				}
				bos.write(buf, 0, bread);
				bytesRead += bread;
			}
			if (bytesRead != count) {
				throw new IOException("Expected " + count
						+ " byte for file but received " + bytesRead + " byte.");
			}
		} catch (SocketTimeoutException e) {
			if (count > 0) {
				throw e;
			}
		} finally {
			bos.close();
			fos.close();
		}
		return file;
	}