Skip to content

Commit 84d089b

Browse files
committed
Add protocol exceptions.
1 parent d66d0a9 commit 84d089b

File tree

3 files changed

+64
-27
lines changed

3 files changed

+64
-27
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ch.ethz.ssh2;
2+
3+
import java.io.IOException;
4+
5+
/**
6+
* @version $Id:$
7+
*/
8+
public class PacketTypeException extends IOException {
9+
10+
public PacketTypeException() {
11+
}
12+
13+
public PacketTypeException(final String message) {
14+
super(message);
15+
}
16+
17+
public PacketTypeException(final int packet) {
18+
super(String.format("The SFTP server sent an unexpected packet type (%d)", packet));
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ch.ethz.ssh2;
2+
3+
import java.io.IOException;
4+
5+
/**
6+
* @version $Id:$
7+
*/
8+
public class RequestMismatchException extends IOException {
9+
10+
public RequestMismatchException() {
11+
super("The server sent an invalid id field.");
12+
}
13+
14+
public RequestMismatchException(final String message) {
15+
super(message);
16+
}
17+
}

src/main/java/ch/ethz/ssh2/SFTPv3Client.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
import java.io.IOException;
99
import java.io.InputStream;
1010
import java.io.OutputStream;
11+
import java.net.SocketException;
1112
import java.nio.charset.Charset;
1213
import java.nio.charset.UnsupportedCharsetException;
1314
import java.util.ArrayList;
1415
import java.util.HashMap;
1516
import java.util.List;
1617
import java.util.Map;
17-
import java.util.Vector;
1818

1919
import ch.ethz.ssh2.channel.Channel;
2020
import ch.ethz.ssh2.log.Logger;
@@ -159,7 +159,7 @@ public void setCharset(String charset) throws IOException
159159
{
160160
if (charset == null)
161161
{
162-
charsetName = charset;
162+
charsetName = null;
163163
return;
164164
}
165165

@@ -239,11 +239,11 @@ private void readBytes(byte[] buff, int pos, int len) throws IOException
239239
int count = is.read(buff, pos, len);
240240
if (count < 0)
241241
{
242-
throw new IOException("Unexpected end of sftp stream.");
242+
throw new SocketException("Unexpected end of sftp stream.");
243243
}
244244
if ((count == 0) || (count > len))
245245
{
246-
throw new IOException("Underlying stream implementation is bogus!");
246+
throw new SocketException("Underlying stream implementation is bogus!");
247247
}
248248
len -= count;
249249
pos += count;
@@ -396,7 +396,7 @@ public SFTPv3FileAttributes fstat(SFTPv3FileHandle handle) throws IOException
396396
int rep_id = tr.readUINT32();
397397
if (rep_id != req_id)
398398
{
399-
throw new IOException("The server sent an invalid id field.");
399+
throw new RequestMismatchException();
400400
}
401401

402402
if (t == Packet.SSH_FXP_ATTRS)
@@ -406,7 +406,7 @@ public SFTPv3FileAttributes fstat(SFTPv3FileHandle handle) throws IOException
406406

407407
if (t != Packet.SSH_FXP_STATUS)
408408
{
409-
throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
409+
throw new PacketTypeException(t);
410410
}
411411

412412
int errorCode = tr.readUINT32();
@@ -435,7 +435,7 @@ private SFTPv3FileAttributes statBoth(String path, int statMethod) throws IOExce
435435
int rep_id = tr.readUINT32();
436436
if (rep_id != req_id)
437437
{
438-
throw new IOException("The server sent an invalid id field.");
438+
throw new RequestMismatchException();
439439
}
440440

441441
if (t == Packet.SSH_FXP_ATTRS)
@@ -445,7 +445,7 @@ private SFTPv3FileAttributes statBoth(String path, int statMethod) throws IOExce
445445

446446
if (t != Packet.SSH_FXP_STATUS)
447447
{
448-
throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
448+
throw new PacketTypeException(t);
449449
}
450450

451451
int errorCode = tr.readUINT32();
@@ -511,7 +511,7 @@ public String readLink(String path) throws IOException
511511
int rep_id = tr.readUINT32();
512512
if (rep_id != req_id)
513513
{
514-
throw new IOException("The server sent an invalid id field.");
514+
throw new RequestMismatchException();
515515
}
516516

517517
if (t == Packet.SSH_FXP_NAME)
@@ -528,7 +528,7 @@ public String readLink(String path) throws IOException
528528

529529
if (t != Packet.SSH_FXP_STATUS)
530530
{
531-
throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
531+
throw new PacketTypeException(t);
532532
}
533533

534534
int errorCode = tr.readUINT32();
@@ -549,12 +549,12 @@ private void expectStatusOKMessage(int id) throws IOException
549549
int rep_id = tr.readUINT32();
550550
if (rep_id != id)
551551
{
552-
throw new IOException("The server sent an invalid id field.");
552+
throw new RequestMismatchException();
553553
}
554554

555555
if (t != Packet.SSH_FXP_STATUS)
556556
{
557-
throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
557+
throw new PacketTypeException(t);
558558
}
559559

560560
int errorCode = tr.readUINT32();
@@ -671,7 +671,7 @@ public String canonicalPath(String path) throws IOException
671671
int rep_id = tr.readUINT32();
672672
if (rep_id != req_id)
673673
{
674-
throw new IOException("The server sent an invalid id field.");
674+
throw new RequestMismatchException();
675675
}
676676

677677
if (t == Packet.SSH_FXP_NAME)
@@ -690,7 +690,7 @@ public String canonicalPath(String path) throws IOException
690690

691691
if (t != Packet.SSH_FXP_STATUS)
692692
{
693-
throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
693+
throw new PacketTypeException(t);
694694
}
695695

696696
int errorCode = tr.readUINT32();
@@ -723,7 +723,7 @@ private List<SFTPv3DirectoryEntry> scanDirectory(byte[] handle) throws IOExcepti
723723
int rep_id = tr.readUINT32();
724724
if (rep_id != req_id)
725725
{
726-
throw new IOException("The server sent an invalid id field.");
726+
throw new RequestMismatchException();
727727
}
728728

729729
if (t == Packet.SSH_FXP_NAME)
@@ -750,7 +750,7 @@ private List<SFTPv3DirectoryEntry> scanDirectory(byte[] handle) throws IOExcepti
750750

751751
if (t != Packet.SSH_FXP_STATUS)
752752
{
753-
throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
753+
throw new PacketTypeException(t);
754754
}
755755

756756
int errorCode = tr.readUINT32();
@@ -785,7 +785,7 @@ public final SFTPv3FileHandle openDirectory(String path) throws IOException
785785
int rep_id = tr.readUINT32();
786786
if (rep_id != req_id)
787787
{
788-
throw new IOException("The server sent an invalid id field.");
788+
throw new RequestMismatchException();
789789
}
790790

791791
if (t == Packet.SSH_FXP_HANDLE)
@@ -796,7 +796,7 @@ public final SFTPv3FileHandle openDirectory(String path) throws IOException
796796

797797
if (t != Packet.SSH_FXP_STATUS)
798798
{
799-
throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
799+
throw new PacketTypeException(t);
800800
}
801801

802802
int errorCode = tr.readUINT32();
@@ -819,7 +819,7 @@ private String expandString(byte[] b, int off, int len)
819819
}
820820
else
821821
{
822-
sb.append("{0x" + Integer.toHexString(c) + "}");
822+
sb.append(String.format("{0x%s}", Integer.toHexString(c)));
823823
}
824824
}
825825

@@ -1231,7 +1231,7 @@ public SFTPv3FileHandle openFile(String fileName, int flags, SFTPv3FileAttribute
12311231
int rep_id = tr.readUINT32();
12321232
if (rep_id != req_id)
12331233
{
1234-
throw new IOException("The server sent an invalid id field.");
1234+
throw new RequestMismatchException();
12351235
}
12361236

12371237
if (t == Packet.SSH_FXP_HANDLE)
@@ -1242,7 +1242,7 @@ public SFTPv3FileHandle openFile(String fileName, int flags, SFTPv3FileAttribute
12421242

12431243
if (t != Packet.SSH_FXP_STATUS)
12441244
{
1245-
throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
1245+
throw new PacketTypeException(t);
12461246
}
12471247

12481248
int errorCode = tr.readUINT32();
@@ -1396,7 +1396,7 @@ public int read(SFTPv3FileHandle handle, long fileOffset, byte[] dst, int dstoff
13961396
OutstandingReadRequest req = pendingReadQueue.remove(tr.readUINT32());
13971397
if (null == req)
13981398
{
1399-
throw new IOException("The server sent an invalid id field.");
1399+
throw new RequestMismatchException();
14001400
}
14011401
// Evaluate the answer
14021402
if (t == Packet.SSH_FXP_STATUS)
@@ -1458,7 +1458,7 @@ else if (t == Packet.SSH_FXP_DATA)
14581458
}
14591459
else
14601460
{
1461-
throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
1461+
throw new PacketTypeException(t);
14621462
}
14631463
}
14641464
// Should never reach here.
@@ -1539,7 +1539,7 @@ private void readStatus() throws IOException
15391539
OutstandingStatusRequest status = pendingStatusQueue.remove(tr.readUINT32());
15401540
if (null == status)
15411541
{
1542-
throw new IOException("The server sent an invalid id field.");
1542+
throw new RequestMismatchException();
15431543
}
15441544

15451545
// Evaluate the answer
@@ -1560,7 +1560,7 @@ private void readStatus() throws IOException
15601560
listener.read(msg);
15611561
throw new SFTPException(msg, code);
15621562
}
1563-
throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
1563+
throw new PacketTypeException(t);
15641564
}
15651565

15661566
private void readPendingReadStatus() throws IOException
@@ -1575,7 +1575,7 @@ private void readPendingReadStatus() throws IOException
15751575
OutstandingReadRequest status = pendingReadQueue.remove(tr.readUINT32());
15761576
if (null == status)
15771577
{
1578-
throw new IOException("The server sent an invalid id field.");
1578+
throw new RequestMismatchException();
15791579
}
15801580

15811581
// Evaluate the answer
@@ -1600,7 +1600,7 @@ private void readPendingReadStatus() throws IOException
16001600
listener.read(msg);
16011601
throw new SFTPException(msg, code);
16021602
}
1603-
throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
1603+
throw new PacketTypeException(t);
16041604
}
16051605

16061606
/**

0 commit comments

Comments
 (0)