
// Project: webCDwriter - Network CD Writing
// Author: Jörg Haeger

import java.io.*;

public class PutTracks extends PutFiles
{
	public PutTracks(DirTreeNode CDRootNode, int sessionSize)
	{
		super(CDRootNode, sessionSize);
	}

	protected void putFile(String dirName, DirTreeNode node)
		throws java.io.IOException, CommandError
	{
		SecFile file = (SecFile)node.getFile();
		String fileName = dirName + "/" + node.toString();
		fileName = spaceToEsc(fileName);
		Version.showDebugInfo(1, fileName + " " + file);
		view.setMessage(fileName);

		FileInputStream inFile;
		try
		{
			inFile = new FileInputStream(file);
		}
		catch (java.io.IOException e)
		{
			System.out.println("can't read " + file);
			return;
		}

		String trackType = "image";
		if (file.isWAV())
			trackType = "wav";
		else
		if (file.getName().toLowerCase().endsWith(".mp3"))
			trackType = "mp3";

		while (!cancel)
		{
			byte bs[] = new byte[16*1024];
			int len = inFile.read(bs);
			if (len == -1)
				break;
			outStream.writeBytes("putTrack "
				+ trackType + " " + len
				+ "\n");
			outStream.write(bs, 0, len);
			bytesTransmitted += len;
			if (len % 1024 > 0)
				bytesTransmitted += 1024 - len % 1024;
			view.setProgress(bytesTransmitted);

			readResult();
			if (!lastResultOK())
				throw new CommandError();

			trackType = "continue";
		}
	}
}

