Refactor: Creating RemoteFile base class to be used as arguments.

This commit is contained in:
Samuel Carlsson 2014-03-20 11:33:03 +01:00
parent 6c01fcc86c
commit ac47d49cc7
5 changed files with 52 additions and 38 deletions

View File

@ -70,13 +70,13 @@ public class JadbDevice {
send("shell:" + shellLine.toString()); send("shell:" + shellLine.toString());
} }
public List<RemoteFile> list(String remotePath) throws IOException, JadbException { public List<RemoteFileRecord> list(String remotePath) throws IOException, JadbException {
ensureTransportIsSelected(); ensureTransportIsSelected();
SyncTransport sync = transport.startSync(); SyncTransport sync = transport.startSync();
sync.send("LIST", remotePath); sync.send("LIST", remotePath);
List<RemoteFile> result = new ArrayList<RemoteFile>(); List<RemoteFileRecord> result = new ArrayList<RemoteFileRecord>();
for (RemoteFile dent = sync.readDirectoryEntry(); dent != RemoteFile.DONE; dent = sync.readDirectoryEntry()) for (RemoteFileRecord dent = sync.readDirectoryEntry(); dent != RemoteFileRecord.DONE; dent = sync.readDirectoryEntry())
{ {
result.add(dent); result.add(dent);
} }

View File

@ -1,37 +1,17 @@
package se.vidstige.jadb; package se.vidstige.jadb;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
/** /**
* Created by vidstige on 2014-03-19. * Created by vidstige on 2014-03-20
*/ */
public class RemoteFile { public class RemoteFile {
public static final RemoteFile DONE = new RemoteFile("DONE", null, 0, 0, 0); private final String path;
private final String name; public RemoteFile(String path) { this.path = path;}
private final int mode;
private final int size;
private final long lastModified;
public RemoteFile(String id, String name, int mode, int size, long lastModified) { public String getName() { throw new NotImplementedException(); }
this.name = name; public int getSize() { throw new NotImplementedException(); }
this.mode = mode; public long getLastModified() { throw new NotImplementedException(); }
this.size = size; public boolean isDirectory() { throw new NotImplementedException(); }
this.lastModified = lastModified;
}
public String getName() {
return name;
}
public int getSize() {
return size;
}
public long getLastModified() {
return lastModified;
}
public boolean isDirectory() {
return (mode & (1 << 14)) == (1 << 14);
}
} }

View File

@ -0,0 +1,34 @@
package se.vidstige.jadb;
/**
* Created by vidstige on 2014-03-19.
*/
public class RemoteFileRecord extends RemoteFile{
public static final RemoteFileRecord DONE = new RemoteFileRecord(null, 0, 0, 0);
private final int mode;
private final int size;
private final long lastModified;
public RemoteFileRecord(String name, int mode, int size, long lastModified) {
super(name);
this.mode = mode;
this.size = size;
this.lastModified = lastModified;
}
@Override
public int getSize() {
return size;
}
@Override
public long getLastModified() {
return lastModified;
}
@Override
public boolean isDirectory() {
return (mode & (1 << 14)) == (1 << 14);
}
}

View File

@ -52,7 +52,7 @@ class SyncTransport {
return new String(buffer, Charset.forName("utf-8")); return new String(buffer, Charset.forName("utf-8"));
} }
public RemoteFile readDirectoryEntry() throws IOException { public RemoteFileRecord readDirectoryEntry() throws IOException {
String id = readString(4); String id = readString(4);
int mode = readInt(); int mode = readInt();
int size = readInt(); int size = readInt();
@ -60,8 +60,8 @@ class SyncTransport {
int nameLength = readInt(); int nameLength = readInt();
String name = readString(nameLength); String name = readString(nameLength);
if (!"DENT".equals(id)) return RemoteFile.DONE; if (!"DENT".equals(id)) return RemoteFileRecord.DONE;
return new RemoteFile(id, name, mode, size, time); return new RemoteFileRecord(name, mode, size, time);
} }
private void sendChunk(byte[] buffer, int offset, int length) throws IOException { private void sendChunk(byte[] buffer, int offset, int length) throws IOException {

View File

@ -6,7 +6,7 @@ import org.junit.Test;
import se.vidstige.jadb.JadbDevice; import se.vidstige.jadb.JadbDevice;
import se.vidstige.jadb.JadbConnection; import se.vidstige.jadb.JadbConnection;
import se.vidstige.jadb.RemoteFile; import se.vidstige.jadb.RemoteFileRecord;
public class RealDeviceTestCases { public class RealDeviceTestCases {
@ -30,8 +30,8 @@ public class RealDeviceTestCases {
{ {
JadbConnection jadb = new JadbConnection(); JadbConnection jadb = new JadbConnection();
JadbDevice any = jadb.getAnyDevice(); JadbDevice any = jadb.getAnyDevice();
List<RemoteFile> files = any.list("/"); List<RemoteFileRecord> files = any.list("/");
for (RemoteFile f : files) for (RemoteFileRecord f : files)
{ {
System.out.println(f.getName()); System.out.println(f.getName());
} }