/** * List the contents of a directory. * * @param dirName See the {@link SFTPv3Client comment} for the class for more details. * @return A Vector containing {@link SFTPv3DirectoryEntry} objects. * @throws IOException */ public List<SFTPv3DirectoryEntry> ls(String dirName) throws IOException { SFTPv3FileHandle handle = openDirectory(dirName); List<SFTPv3DirectoryEntry> result = scanDirectory(handle.fileHandle); closeFile(handle); return result; }
/** * Create a file and open it for reading and writing. * Same as {@link #createFile(String, SFTPv3FileAttributes) createFile(fileName, null)}. * * @param fileName See the {@link SFTPv3Client comment} for the class for more details. * @return a SFTPv3FileHandle handle * @throws IOException */ public SFTPv3FileHandle createFile(String fileName) throws IOException { return createFile(fileName, null); }
/** * Create a file (truncate it if it already exists) and open it for reading and writing. * Same as {@link #createFileTruncate(String, SFTPv3FileAttributes) createFileTruncate(fileName, null)}. * * @param fileName See the {@link SFTPv3Client comment} for the class for more details. * @return a SFTPv3FileHandle handle * @throws IOException */ public SFTPv3FileHandle createFileTruncate(String fileName) throws IOException { return createFileTruncate(fileName, null); }
/** * List the contents of a directory. * * @param dirName See the {@link SFTPv3Client comment} for the class for more details. * @return A Vector containing {@link SFTPv3DirectoryEntry} objects. * @throws IOException */ public Vector ls(String dirName) throws IOException { byte[] handle = openDirectory(dirName); Vector result = scanDirectory(handle); closeHandle(handle); return result; }
/** * Close a file. * * @param handle a SFTPv3FileHandle handle * @throws IOException */ public void closeFile(SFTPv3FileHandle handle) throws IOException { try { while (!pendingReadQueue.isEmpty()) { this.readPendingReadStatus(); } while (!pendingStatusQueue.isEmpty()) { this.readStatus(); } if (!handle.isClosed) { closeHandle(handle.fileHandle); } } finally { handle.isClosed = true; } } }
private final void closeHandle(byte[] handle) throws IOException { int req_id = generateNextRequestID(); TypesWriter tw = new TypesWriter(); tw.writeString(handle, 0, handle.length); sendMessage(Packet.SSH_FXP_CLOSE, req_id, tw.getBytes()); expectStatusOKMessage(req_id); }
/** * 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)); log.debug("Sending SSH_FXP_FSETSTAT..."); sendMessage(Packet.SSH_FXP_FSETSTAT, req_id, tw.getBytes()); expectStatusOKMessage(req_id); }
checkHandleValidAndOpen(handle); int req_id = generateNextRequestID(); sendMessage(Packet.SSH_FXP_FSTAT, req_id, tw.getBytes()); byte[] resp = receiveMessage(34000); return readAttrs(tr);
private SFTPv3FileHandle openFile(String fileName, int flags, SFTPv3FileAttributes attr) throws IOException int req_id = generateNextRequestID(); tw.writeBytes(createAttrs(attr)); sendMessage(Packet.SSH_FXP_OPEN, req_id, tw.getBytes()); byte[] resp = receiveMessage(34000);
/** * Write bytes to a file. If <code>len</code> > 32768, then the write operation will * be split into multiple writes. * * @param handle a SFTPv3FileHandle handle. * @param fileOffset offset (in bytes) in the file. * @param src the source byte array. * @param srcoff offset in the source byte array. * @param len how many bytes to write. * @throws IOException */ public void write(SFTPv3FileHandle handle, long fileOffset, byte[] src, int srcoff, int len) throws IOException { checkHandleValidAndOpen(handle); // Send the next write request OutstandingStatusRequest req = new OutstandingStatusRequest(); req.req_id = generateNextRequestID(); TypesWriter tw = new TypesWriter(); tw.writeString(handle.fileHandle, 0, handle.fileHandle.length); tw.writeUINT64(fileOffset); tw.writeString(src, srcoff, len); log.debug("Sending SSH_FXP_WRITE..."); sendMessage(Packet.SSH_FXP_WRITE, req.req_id, tw.getBytes()); pendingStatusQueue.put(req.req_id, req); // Only read next status if parallelism reached while (pendingStatusQueue.size() >= parallelism) { this.readStatus(); } }
checkHandleValidAndOpen(handle); req.req_id = generateNextRequestID(); req.serverOffset = serverOffset; req.len = (remaining > len) ? len : remaining; remaining -= req.len; sendReadRequest(req.req_id, handle, req.serverOffset, req.len); byte[] resp = receiveMessage(34000); TypesReader tr = new TypesReader(resp); req.req_id = generateNextRequestID(); req.serverOffset += readLen; req.len -= readLen; sendReadRequest(req.req_id, handle, req.serverOffset, req.len);
sendMessage(Packet.SSH_FXP_INIT, 0, tw.getBytes()); debug.println("Waiting for SSH_FXP_VERSION..."); TypesReader tr = new TypesReader(receiveMessage(34000)); /* Should be enough for any reasonable server */ debug.println("SSH_FXP_VERSION: extension: " + name + " = '" + expandString(value, 0, value.length) + "'");
SFTPv3FileAttributes attr = new SFTPv3FileAttributes(); attr.permissions = Integer.valueOf(permissions); handle = client.createFileTruncate(path, attr); } else { handle = client.openFileRW(path); client.write(handle, fileOffset, buf, 0, r); } catch (IOException e) { throw e; client.closeFile(handle);
@Override public InputStream readFile(String path) { SFTPv3Client client = getClient(); String realPath = getRealPath(path); int startid = 0; try { if (!isDir(path)) { SFTPv3FileHandle handle = client.openFileRO(realPath); byte[] result = new byte[0]; byte[] tempResult = new byte[ONCE_MAX_BYTES]; int len = client.read(handle, startid, tempResult, 0, ONCE_MAX_BYTES); while (len == ONCE_MAX_BYTES) { result = byteMerger(result, tempResult); startid += len; len = client.read(handle, startid, tempResult, 0, ONCE_MAX_BYTES); } if (len > 0){ byte[] lastResult = new byte[len]; System.arraycopy(tempResult, 0, lastResult, 0, len); result = byteMerger(result, lastResult); } client.closeFile(handle); return new ByteArrayInputStream(result); } return null; } catch (IOException e) { LOG.error(e.getMessage()); return null; } }
public void write(int b) throws IOException { try { client.write(finalHandle, offset, new byte[] { (byte) b }, 0, 1); } catch (IOException ioe) { throw ioe; } catch (Exception e) { IOException ioException = new IOException(); ioException.initCause(e); throw ioException; } }
public void close() throws IOException { client.closeFile(handle); } };
public int read(byte[] b, int off, int len) throws IOException { if (fileOffset >= file.getSize()) { return -1; } int r = client.read(handle, fileOffset, buf, 0, buf.length); if (r > 0) { fileOffset += r; } return r; }
final SftpFile file = stat(path); try { final SFTPv3FileHandle handle = client.openFileRO(path); try { final byte[] buf = new byte[32768]; client.closeFile(handle);
SFTPv3FileAttributes attr = new SFTPv3FileAttributes(); attr.permissions = Integer.valueOf(permissions); handle = client.createFileTruncate(path, attr); } else { handle = client.openFileRW(path);
/** * Close a file. * * @param handle a SFTPv3FileHandle handle * @throws IOException */ public void closeFile(SFTPv3FileHandle handle) throws IOException { if (handle == null) throw new IllegalArgumentException("the handle argument may not be null"); try { if (handle.isClosed == false) { closeHandle(handle.fileHandle); } } finally { handle.isClosed = true; } } }