public void write(byte[] input) throws IOException {
		synchronized (chunks)
		{
			if (currentChunk == null)
			{
				currentChunk = new Chunk();
				chunks.add(currentChunk);
				lastChunk = currentChunk;
			}
			int toWrite = input.length;
			int posArray = 0;
			while (true)
			{
				if (positionWithinChunk + toWrite <= CHUNKSIZE)
				{
					System.arraycopy(input, posArray, currentChunk.chunk, positionWithinChunk, toWrite);
					positionWithinChunk += toWrite;
					int n = positionWithinChunk;
					if (n > currentChunk.filled)
						currentChunk.filled = n;
					if (isAtEnd() && positionWithinChunk == CHUNKSIZE)
					{
						Chunk newChunk = new Chunk();
						newChunk.index = currentChunk.index + 1;
						chunks.add(newChunk);
						lastChunk = newChunk;
						currentChunk = newChunk;
					}
					return;
				} else {
					int currentStep = CHUNKSIZE - positionWithinChunk;
					System.arraycopy(input, posArray, currentChunk.chunk, positionWithinChunk, currentStep);
					positionWithinChunk += currentStep;
					posArray += currentStep;
					toWrite -= currentStep;
					currentChunk.filled = CHUNKSIZE;
					
					if (isAtEnd())
					{
						Chunk newChunk = new Chunk();
						newChunk.index = currentChunk.index + 1;
						chunks.add(newChunk);
						lastChunk = newChunk;
						currentChunk = newChunk;
					} else {
						currentChunk = getNextChunk(currentChunk);
					}
					positionWithinChunk = 0;
				}
			}
		}
	}