use of com.trilead.ssh2.SFTPv3FileHandle in project hudson-2.x by hudson.
the class SFTPClient method writeToFile.
/**
* Creates a new file and writes to it.
*/
public OutputStream writeToFile(String path) throws IOException {
final SFTPv3FileHandle h = createFile(path);
return new OutputStream() {
private long offset = 0;
public void write(int b) throws IOException {
write(new byte[] { (byte) b });
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
SFTPClient.this.write(h, offset, b, off, len);
offset += len;
}
@Override
public void close() throws IOException {
closeFile(h);
}
};
}
use of com.trilead.ssh2.SFTPv3FileHandle in project hudson-2.x by hudson.
the class SFTPClient method read.
public InputStream read(String file) throws IOException {
final SFTPv3FileHandle h = openFileRO(file);
return new InputStream() {
private long offset = 0;
public int read() throws IOException {
byte[] b = new byte[1];
if (read(b) < 0)
return -1;
return b[0];
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
int r = SFTPClient.this.read(h, offset, b, off, len);
if (r < 0)
return -1;
offset += r;
return r;
}
@Override
public long skip(long n) throws IOException {
offset += n;
return n;
}
@Override
public void close() throws IOException {
closeFile(h);
}
};
}
use of com.trilead.ssh2.SFTPv3FileHandle in project intellij-community by JetBrains.
the class SFTPv3Client method read.
/**
* Read bytes from a file. No more than 32768 bytes may be read at once.
* Be aware that the semantics of read() are different than for Java streams.
* <p>
* <ul>
* <li>The server will read as many bytes as it can from the file (up to <code>len</code>),
* and return them.</li>
* <li>If EOF is encountered before reading any data, <code>-1</code> is returned.
* <li>If an error occurs, an exception is thrown</li>.
* <li>For normal disk files, it is guaranteed that the server will return the specified
* number of bytes, or up to end of file. For, e.g., device files this may return
* fewer bytes than requested.</li>
* </ul>
*
* @param handle a SFTPv3FileHandle handle
* @param fileOffset offset (in bytes) in the file
* @param dst the destination byte array
* @param dstoff offset in the destination byte array
* @param len how many bytes to read, 0 < len <= 32768 bytes
* @return the number of bytes that could be read, may be less than requested if
* the end of the file is reached, -1 is returned in case of <code>EOF</code>
* @throws IOException
*/
public int read(SFTPv3FileHandle handle, long fileOffset, byte[] dst, int dstoff, int len) throws IOException {
checkHandleValidAndOpen(handle);
if ((len > 32768) || (len <= 0))
throw new IllegalArgumentException("invalid len argument");
int req_id = generateNextRequestID();
TypesWriter tw = new TypesWriter();
tw.writeString(handle.fileHandle, 0, handle.fileHandle.length);
tw.writeUINT64(fileOffset);
tw.writeUINT32(len);
if (debug != null) {
debug.println("Sending SSH_FXP_READ...");
debug.flush();
}
sendMessage(Packet.SSH_FXP_READ, req_id, tw.getBytes());
byte[] resp = receiveMessage(34000);
TypesReader tr = new TypesReader(resp);
int t = tr.readByte();
int rep_id = tr.readUINT32();
if (rep_id != req_id)
throw new IOException("The server sent an invalid id field.");
if (t == Packet.SSH_FXP_DATA) {
if (debug != null) {
debug.println("Got SSH_FXP_DATA...");
debug.flush();
}
int readLen = tr.readUINT32();
if ((readLen < 0) || (readLen > len))
throw new IOException("The server sent an invalid length field.");
tr.readBytes(dst, dstoff, readLen);
return readLen;
}
if (t != Packet.SSH_FXP_STATUS)
throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
int errorCode = tr.readUINT32();
if (errorCode == ErrorCodes.SSH_FX_EOF) {
if (debug != null) {
debug.println("Got SSH_FX_EOF.");
debug.flush();
}
return -1;
}
String errorMessage = tr.readString();
throw new SFTPException(errorMessage, errorCode);
}
use of com.trilead.ssh2.SFTPv3FileHandle in project intellij-community by JetBrains.
the class SFTPv3Client method fsetstat.
/**
* Modify the attributes of a file. Used for operations such as changing
* the ownership, permissions or access times, as well as for truncating a file.
*
* @param handle a SFTPv3FileHandle handle
* @param attr A SFTPv3FileAttributes object. Specifies the modifications to be
* made to the attributes of the file. Empty fields will be ignored.
* @throws IOException
*/
public void fsetstat(SFTPv3FileHandle handle, SFTPv3FileAttributes attr) throws IOException {
checkHandleValidAndOpen(handle);
int req_id = generateNextRequestID();
TypesWriter tw = new TypesWriter();
tw.writeString(handle.fileHandle, 0, handle.fileHandle.length);
tw.writeBytes(createAttrs(attr));
if (debug != null) {
debug.println("Sending SSH_FXP_FSETSTAT...");
debug.flush();
}
sendMessage(Packet.SSH_FXP_FSETSTAT, req_id, tw.getBytes());
expectStatusOKMessage(req_id);
}
use of com.trilead.ssh2.SFTPv3FileHandle in project Payara by payara.
the class SFTPClient method writeToFile.
/**
* Creates a new file and writes to it.
*/
public OutputStream writeToFile(String path) throws IOException {
path = normalizePath(path);
final SFTPv3FileHandle h = createFile(path);
return new OutputStream() {
private long offset = 0;
public void write(int b) throws IOException {
write(new byte[] { (byte) b });
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
SFTPClient.this.write(h, offset, b, off, len);
offset += len;
}
@Override
public void close() throws IOException {
closeFile(h);
}
};
}
Aggregations