Skip to content

Commit

Permalink
API Improvements, sending JAR files fixed, and other fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Guilherme Schmidt committed Nov 4, 2010
1 parent 86468b6 commit bdb2be7
Show file tree
Hide file tree
Showing 16 changed files with 525 additions and 432 deletions.
25 changes: 21 additions & 4 deletions src/com/lhf/jobexftp/StandAloneApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.Iterator;
Expand Down Expand Up @@ -177,7 +178,12 @@ private void listFolder(String[] args) {
if (args != null && args.length > 0 && !args[0].equalsIgnoreCase("")) {
c.changeDirectory(args[0], false);
}
System.out.print(new String(c.loadFolderListing().getListing()));
if (write) {
System.out.print(c.loadFolderListing().getListing());
} else {
System.out.print(new String(c.loadFolderListing().getContents()));

}
if (args != null && args.length > 0 && !args[0].equalsIgnoreCase("")) {
c.changeDirectory("/", false);
}
Expand All @@ -195,6 +201,7 @@ private void uploadFile(String[] args) {
String path = "/";
File f;
if (!args[0].equalsIgnoreCase("")) {
System.out.println("Sending " + args[0]);
f = new File(args[0]);
filenameS = f.getName();
} else {
Expand All @@ -208,8 +215,16 @@ private void uploadFile(String[] args) {
path = Utility.removeLastFolder(args[1]);
c.changeDirectory(path, false);
}

OBEXFile file = new OBEXFile(filenameS);
file.setInputStream(new FileInputStream(f));
InputStream is = new FileInputStream(f);
int size = is.available();
// byte[] contents = new byte[size];
System.out.println("Size "+size);
file.setInputStream(is);
// is.read(contents);
// file.setContents(contents);

c.writeFile(file);
c.changeDirectory("/", false);
}
Expand Down Expand Up @@ -237,9 +252,11 @@ private void deleteFile(String[] args) {
String filenameS;
String path = "/";
filenameS = Utility.getLastFolder(args[0]);
path = Utility.removeLastFolder(args[0]);
path = Utility.preparePath(Utility.removeLastFolder(args[0]));
c.changeDirectory(path, false);
c.removeObject(new OBEXFile(filenameS));
boolean b = c.removeObject(new OBEXFile(Utility.createSimbolicFolderTree(path), filenameS));
System.out.println("Removed " + filenameS + " in " + c.getCurrentFolder().getPath() + (b ? " with success" : "with error"));

c.changeDirectory("/", false);
}
} catch (IOException ex) {
Expand Down
24 changes: 21 additions & 3 deletions src/com/lhf/obexftplib/etc/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package com.lhf.obexftplib.etc;

import com.lhf.obexftplib.fs.OBEXFolder;
import com.lhf.obexftplib.io.obexop.GetResponse;
import com.lhf.obexftplib.io.obexop.Header;
import com.lhf.obexftplib.io.obexop.Response;
Expand Down Expand Up @@ -69,8 +70,6 @@ public static CommPortIdentifier addPortToRxTx(final String connPortPath) throws
return CommPortIdentifier.getPortIdentifier(connPortPath);
}



/**
* Checks for the combination looking from end to begining, in the whole array
* @param b
Expand All @@ -88,7 +87,6 @@ public static boolean arrayContainsOK(final byte[] b) {
return false;
}


/**
* convert integer to byte array
*
Expand Down Expand Up @@ -409,6 +407,26 @@ public static String removeLastFolder(String path) {
}
return path;
}

/**
* This function is used when the Path is known, but there is no OBEXFolder referenciating it.
* So it makes easier to user OBEXFolder to movein or create folders.
* @param absolutePath an absolutpath, starting with a:/ or /
* @return the last level OBEXFolder of the path specified.
*/
public static OBEXFolder createSimbolicFolderTree(String absolutePath) {
if (!(absolutePath.startsWith("/") || absolutePath.startsWith("a:"))) {
absolutePath = "a:/" + absolutePath;
}
OBEXFolder folder = OBEXFolder.ROOT_FOLDER;
String pathList[] = absolutePath.split("/");
for (int i = 0; i < pathList.length; i++) {
if (!pathList[i].startsWith("a:")) {
folder = folder.addFolder(pathList[i]);
}
}
return folder;
}
/**
* DateFormat for getTime
* @see Utility#getTime(java.util.Date)
Expand Down
17 changes: 8 additions & 9 deletions src/com/lhf/obexftplib/fs/OBEXFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.lhf.obexftplib.etc.Utility;
import com.lhf.obexftplib.io.obexop.Header;
import com.lhf.obexftplib.io.obexop.HeaderSet;
import com.lhf.obexftplib.io.obexop.PutRequest;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -33,19 +32,19 @@
* Get operations usually fill all the available data in server, and set the contents as String, that can be obtained with getContents() or getInputStream();
* @author Ricardo Guilherme Schmidt <ricardo@lhf.ind.br>
*/
public class OBEXFile extends OBEXObject {
public final class OBEXFile extends OBEXObject {

private byte[] size;
private OBEXFolder parentFolder;
private InputStream inputStream;
private String path = null;

public OBEXFile(OBEXFolder parentFolder, String filename) {
public OBEXFile(final OBEXFolder parentFolder, final String filename) {
super(parentFolder);
setName(filename);
}

public OBEXFile(String filename) {
public OBEXFile(final String filename) {
this(ROOT_FOLDER, filename);
}

Expand Down Expand Up @@ -74,7 +73,7 @@ public HeaderSet getHeaderSet() {
* @see JOBEXFile#setContents(java.lang.String) ()
*/
@Override
public void setContents(byte[] contents) throws IOException {
public void setContents(final byte[] contents) throws IOException {
setSize(contents.length);
super.setContents(contents);
}
Expand Down Expand Up @@ -102,7 +101,7 @@ public byte[] getSize() {
/**
* @param size the size of the contents.
*/
public void setSize(int size) {
public void setSize(final int size) {
this.size = Utility.intToBytes(size, 4);
}

Expand All @@ -123,7 +122,7 @@ public InputStream getInputStream() {
* @param inputStream the inputStream used for reading the contents of the file. Inputstream must be ready to use when setted.
* @see JOBEXObject#setContents(java.lang.String)
*/
public void setInputStream(InputStream inputStream) {
public void setInputStream(final InputStream inputStream) {
try {
setSize(inputStream.available());
} catch (IOException ex) {
Expand All @@ -138,7 +137,7 @@ public String getSizeString() {
}

@Override
public boolean equals(Object compobj) {
public boolean equals(final Object compobj) {
if (super.equals(compobj) && compobj instanceof OBEXFile) {
OBEXFile file = (OBEXFile) compobj;
return (file.size == this.size);
Expand All @@ -154,7 +153,7 @@ public int hashCode() {
}

@Override
protected void threatHeader(Header header) {
protected void threatHeader(final Header header) {
switch (header.getId()) {
case Header.LENGTH:
this.size = header.getValue();
Expand Down
60 changes: 45 additions & 15 deletions src/com/lhf/obexftplib/fs/OBEXFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,58 +37,64 @@
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

/**
*
* An obex folder object representation in java.
* @author Ricardo Guilherme Schmidt <ricardo@lhf.ind.br>
*/
public final class OBEXFolder extends OBEXObject {

private String path = null;
private Map<String, OBEXObject> subobjects = new TreeMap<String, OBEXObject>();
private final Map<String, OBEXObject> subobjects = new TreeMap<String, OBEXObject>();
private static final OBEXFolderListingParser PARSER = new OBEXFolderListingParser();

public OBEXFolder(OBEXFolder parentFolder, String filename) {
public OBEXFolder(final OBEXFolder parentFolder, final String filename) {
super(parentFolder);
setName(filename);
}

public OBEXFolder(String filename) {
public OBEXFolder(final String filename) {
this(ROOT_FOLDER, filename);
}

/**
* Builds the path string based in the parentfolder and in this folder.
* @return
*/
@Override
public String getPath() {
if (path != null) {
return path + "/" + name;
} else if (getParentFolder() == null) {
return name;
} else {
return getParentFolder().getPath() + "/" + name;
if (path == null) {
if (getParentFolder() == null) {
path = "";
} else {
path = getParentFolder().getPath() + "/";
}
}
return path + name;
}

public void add(OBEXObject object) {
public void add(final OBEXObject object) {
subobjects.put(object.name, object);
}

public Map<String, OBEXObject> getSubobjects() {
return subobjects;
}

public OBEXFolder getChildFolder(String name) {
public OBEXFolder getChildFolder(final String name) {
OBEXObject obj = subobjects.get(name);
if (obj != null && obj instanceof OBEXFolder) {
return (OBEXFolder) obj;
}
return null;
}

public OBEXFile getChildFile(String name) {
public OBEXFile getChildFile(final String name) {
OBEXObject obj = subobjects.get(name);
if (obj != null && obj instanceof OBEXFolder) {
return (OBEXFile) obj;
Expand All @@ -97,12 +103,20 @@ public OBEXFile getChildFile(String name) {
}

public OBEXFolder addFolder(String filename) {
filename = filename.replace('/', ' ').trim();
if (filename.isEmpty()) {
return this;
}
OBEXFolder folder = new OBEXFolder(this, filename);
add(folder);
return folder;
}

public OBEXFile addFile(String filename) {
filename = filename.replace('/', ' ').trim();
if (filename.isEmpty()) {
return null;
}
OBEXFile file = new OBEXFile(filename);
add(file);
return file;
Expand Down Expand Up @@ -137,7 +151,7 @@ public String getListing() {
}

@Override
protected void threatHeader(Header header) {
protected void threatHeader(final Header header) {
switch (header.getId()) {
case Header.NAME:
setName(header.getValue());
Expand Down Expand Up @@ -177,6 +191,22 @@ protected void onReset() {
public String getSizeString() {
return "<DIR>";
}

@Override
public boolean equals(Object compobj) {
if (compobj instanceof OBEXFolder) {
OBEXFolder compfol = (OBEXFolder) compobj;
return compfol.getPath().equalsIgnoreCase(this.getPath());
}
return false;
}

@Override
public int hashCode() {
int hash = 7;
hash = 73 * hash + (this.getPath() != null ? this.getPath().hashCode() : 0);
return hash;
}
}

/**
Expand Down Expand Up @@ -235,7 +265,7 @@ public void startDocument() throws SAXException {
}

@Override
public void startElement(String uri, String localName, String qName, org.xml.sax.Attributes attributes) throws SAXException {
public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException {
OBEXObject childObject = null;
if (qName.equalsIgnoreCase("folder")) {
childObject = new OBEXFolder(folder, attributes.getValue("name"));
Expand Down
Loading

0 comments on commit bdb2be7

Please sign in to comment.