tikhomirov@618: /* tikhomirov@618: * Copyright (c) 2013 TMate Software Ltd tikhomirov@618: * tikhomirov@618: * This program is free software; you can redistribute it and/or modify tikhomirov@618: * it under the terms of the GNU General Public License as published by tikhomirov@618: * the Free Software Foundation; version 2 of the License. tikhomirov@618: * tikhomirov@618: * This program is distributed in the hope that it will be useful, tikhomirov@618: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@618: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@618: * GNU General Public License for more details. tikhomirov@618: * tikhomirov@618: * For information on how to redistribute this software under tikhomirov@618: * the terms of a license other than GNU General Public License tikhomirov@618: * contact TMate Software at support@hg4j.com tikhomirov@618: */ tikhomirov@618: package org.tmatesoft.hg.internal; tikhomirov@618: tikhomirov@618: import java.io.File; tikhomirov@618: import java.io.IOException; tikhomirov@618: import java.nio.ByteBuffer; tikhomirov@618: tikhomirov@618: import org.tmatesoft.hg.core.HgCommitCommand; tikhomirov@618: import org.tmatesoft.hg.core.HgIOException; tikhomirov@618: import org.tmatesoft.hg.repo.HgDataFile; tikhomirov@618: import org.tmatesoft.hg.repo.HgInvalidFileException; tikhomirov@618: import org.tmatesoft.hg.repo.HgInvalidStateException; tikhomirov@618: import org.tmatesoft.hg.repo.HgRepository; tikhomirov@618: import org.tmatesoft.hg.util.ByteChannel; tikhomirov@618: import org.tmatesoft.hg.util.CancelledException; tikhomirov@618: tikhomirov@618: /** tikhomirov@618: * Access content of the working copy. The difference with {@link FileContentSupplier} is that this one doesn't need {@link File} tikhomirov@618: * in the working directory. However, provided this class is used from {@link HgCommitCommand} when "modified" file was detected, tikhomirov@618: * it's odd to expect no file in the working dir. tikhomirov@618: * tikhomirov@618: * @author Artem Tikhomirov tikhomirov@618: * @author TMate Software Ltd. tikhomirov@618: */ tikhomirov@618: public class WorkingCopyContent implements DataSerializer.DataSource { tikhomirov@618: private final HgDataFile file; tikhomirov@618: tikhomirov@618: public WorkingCopyContent(HgDataFile dataFile) { tikhomirov@618: file = dataFile; tikhomirov@618: if (!dataFile.exists()) { tikhomirov@618: throw new IllegalArgumentException(); tikhomirov@618: } tikhomirov@618: } tikhomirov@618: tikhomirov@618: public void serialize(final DataSerializer out) throws HgIOException { tikhomirov@618: final HgIOException failure[] = new HgIOException[1]; tikhomirov@618: try { tikhomirov@618: // TODO #workingCopy API is very limiting, CancelledException is inconvenient, tikhomirov@618: // and absence of HgIOException is very uncomfortable tikhomirov@618: file.workingCopy(new ByteChannel() { tikhomirov@618: tikhomirov@618: public int write(ByteBuffer buffer) throws IOException { tikhomirov@618: try { tikhomirov@618: if (buffer.hasArray()) { tikhomirov@618: out.write(buffer.array(), buffer.position(), buffer.remaining()); tikhomirov@618: } tikhomirov@618: int rv = buffer.remaining(); tikhomirov@618: buffer.position(buffer.limit()); // pretend we've consumed the data tikhomirov@618: return rv; tikhomirov@618: } catch (HgIOException ex) { tikhomirov@618: failure[0] = ex; tikhomirov@618: throw new IOException(ex); tikhomirov@618: } tikhomirov@618: } tikhomirov@618: }); tikhomirov@618: } catch (HgInvalidFileException ex) { tikhomirov@618: if (failure[0] != null) { tikhomirov@618: throw failure[0]; tikhomirov@618: } tikhomirov@618: throw new HgIOException("Write failure", ex, new File(file.getRepo().getWorkingDir(), file.getPath().toString())); tikhomirov@618: } catch (CancelledException ex) { tikhomirov@618: throw new HgInvalidStateException("Our channel doesn't cancel here"); tikhomirov@618: } tikhomirov@618: } tikhomirov@618: tikhomirov@618: public int serializeLength() { tikhomirov@618: return file.getLength(HgRepository.WORKING_COPY); tikhomirov@618: } tikhomirov@618: }