@Override
	public long read(T target) throws IOException {
		long totalRead = 0;
		
		while (target.remainingSpace() > 0) {
			if (buffer.remainingData() > 0)
				totalRead += buffer.read(target);
			else if (parentMightBeEmpty && totalRead > 0)
				break;
			else {
				long read = parent.read(buffer);
				if (buffer.remainingSpace() > 0)
					parentMightBeEmpty = true;
				else
					parentMightBeEmpty = false;
				if (read == -1) {
					closed = true;
					break;
				}
				else if (read == 0)
					break;
			}
		}
		return totalRead == 0 && closed ? -1 : totalRead;
	}