public void write(SelectionKey key) throws IOException {
		loggerNetworkClient.info("WRITE: ");
		
		FileService fileService;
		ByteBuffer wBuff = ByteBuffer.allocate(NetworkInfo.BUF_SIZE);
		SocketChannel socketChannel = (SocketChannel) key.channel();

		int toWrite;

		synchronized (key) {
			while (wbuf.size() > 0) {
				byte[] bbuf = wbuf.get(0);
				wbuf.remove(0);

				// write all the array
				if (bbuf.length < NetworkInfo.BUF_SIZE) {
					wBuff.clear();
					wBuff.put(bbuf);
					wBuff.flip();
				} else {
					// write only the size of the buffer
					toWrite = NetworkInfo.BUF_SIZE;
					wBuff.clear();
					wBuff.put(bbuf,0,toWrite);
					wBuff.flip();
				}

				int numWritten = socketChannel.write(wBuff);
				loggerNetworkClient.info("I wrote " + numWritten
						+ " bytes on the socket associated with the key" + key);
				
				//update progress bar
				if (this.dataToSend instanceof FileService) {
					fileService = (FileService) dataToSend;
					offset += numWritten;
					network.med.acceptFileTransfer(fileService.serviceName, 100 * offset / sizeToWrite);
				}
				
				if (numWritten < bbuf.length) {
					byte[] newBuf = new byte[bbuf.length - numWritten];

					// Copy data which is still unwritten into newBuf
					for (int i = numWritten; i < bbuf.length; i++) {
						newBuf[i - numWritten] = bbuf[i];
					}

					wbuf.add(0, newBuf);
					break;
				}

				if (wbuf.size() == 0) {
					key.interestOps(SelectionKey.OP_READ);
				}
			}
		}
	}