Revision: 2485
http://sourceforge.net/p/swingme/code/2485
Author: yuranet
Date: 2021-06-28 13:53:39 +0000 (Mon, 28 Jun 2021)
Log Message:
-----------
use shared impl classes
Modified Paths:
--------------
AndroidME/src_MIDP/javax/microedition/io/Connector.java
Removed Paths:
-------------
AndroidME/src_Android/net/yura/android/io/AndroidFileConnection.java
AndroidME/src_Android/net/yura/android/io/HttpConnectionImpl.java
AndroidME/src_Android/net/yura/android/io/HttpsConnectionImpl.java
Deleted: AndroidME/src_Android/net/yura/android/io/AndroidFileConnection.java
===================================================================
--- AndroidME/src_Android/net/yura/android/io/AndroidFileConnection.java 2021-06-28 13:52:22 UTC (rev 2484)
+++ AndroidME/src_Android/net/yura/android/io/AndroidFileConnection.java 2021-06-28 13:53:39 UTC (rev 2485)
@@ -1,320 +0,0 @@
-package net.yura.android.io;
-
-import java.io.DataInputStream;
-import java.io.DataOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.FilenameFilter;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.Enumeration;
-import java.util.Vector;
-import java.util.regex.Pattern;
-import javax.microedition.io.file.FileConnection;
-import android.annotation.TargetApi;
-
-public class AndroidFileConnection implements FileConnection {
-
- private static final String SPECIAL_CHARACTERS = "*.^?[]\\";
-
- public static final File getParentDirectory(File file) {
- String path = file.getAbsolutePath();
- if (path.endsWith(File.separator)) {
- path = path.substring(0, path.length() - File.separator.length());
- }
- int index = path.lastIndexOf(File.separator);
- File result;
- if (index >= 0) {
- path = path.substring(0, index);
- result = new File(path);
- } else {
- result = null;
- }
- return result;
- }
-
- private static final String escape(String s) {
- StringBuffer result = new StringBuffer(s.length());
- for (int i = 0; i < s.length(); i++) {
- char c = s.charAt(i);
- if (SPECIAL_CHARACTERS.indexOf(c) >= 0) {
- result.append("\\");
- }
- result.append(c);
- }
- return result.toString();
- }
-
- private static final File getFile(String url) throws IOException {
- final String URL_FILE_ROOT = "file:///";
- if (url == null || !url.startsWith(URL_FILE_ROOT)) {
- throw new IOException("Invalid file url");
- }
-
- String filePath = url.substring(URL_FILE_ROOT.length());
- return new File(filePath);
- }
-
- private File file;
- private boolean open;
-
- public AndroidFileConnection(String url) throws IOException {
- this.file = getFile(url);
- this.open = true;
- }
-
- // Override
- public long availableSize() {
- // this isn't available
- return -1;
- }
-
- // Override
- public boolean canRead() {
- return this.file.canRead();
- }
-
- // Override
- public boolean canWrite() {
- return this.file.canWrite();
- }
-
- // Override
- public void create() throws IOException {
- if (!this.file.createNewFile()) {
- throw new IOException("file creation failed");
- }
- }
-
- // Override
- public void delete() throws IOException {
- if (!this.file.delete()) {
- throw new IOException("file deletion failed");
- }
- }
-
- // Override
- public long directorySize(boolean includeSubDirs) throws IOException {
- // why is this in the interface?
- return getDirectorySize(this.file, includeSubDirs);
- }
-
- private long getDirectorySize(File file, boolean recursive) {
- long size = 0;
- File[] children = file.listFiles();
- for (File child : children) {
- if (child.isDirectory()) {
- if (recursive) {
- size += getDirectorySize(child, recursive);
- }
- } else {
- size += child.length();
- }
- }
- return size;
- }
-
- // Override
- public boolean exists() {
- return this.file.exists();
- }
-
- // Override
- public long fileSize() throws IOException {
- return this.file.length();
- }
-
- // Override
- public String getName() {
- return this.file.getName();
- }
-
- // Override
- public String getPath() {
- return this.file.getPath();
- }
-
- // Override
- public String getURL() {
- return this.file.toURI().toString();
- }
-
- // Override
- public boolean isDirectory() {
- return this.file.isDirectory();
- }
-
- // Override
- public boolean isHidden() {
- return this.file.isHidden();
- }
-
- // Override
- public boolean isOpen() {
- return this.open;
- }
-
- // Override
- public long lastModified() {
- return this.file.lastModified();
- }
-
- // Override
- public Enumeration list() throws IOException {
- return list(null, false);
- }
-
- /**
- * An asterisk ("*") can be used as a wildcard to represent 0 or more occurrences of any character.
- */
- // Override
- public Enumeration list(String filter, boolean includeHidden)
- throws IOException {
- Pattern pattern;
- if (filter == null || "*".equals(filter)) {
- pattern = Pattern.compile(".*");
- } else {
- String[] literalParts = filter.split("\\*");
- StringBuffer sb = new StringBuffer(filter.length());
- for (int i = 0; i < literalParts.length; i++) {
- String part = literalParts[i];
- String literalPart = escape(part);
- if (i > 0) {
- sb.append("*");
- }
- sb.append(literalPart);
- }
- pattern = Pattern.compile(sb.toString());
- }
-
- final Pattern filterPattern = pattern;
- File[] a = this.file.listFiles(new FilenameFilter() {
-
- // Override
- public boolean accept(File dir, String name) {
- return filterPattern.matcher(name).matches();
- }
-
- });
-
- if (a==null) a = new File[0]; // avoid nullpointer
-
- Vector<String> v = new Vector<String>();
- for (File file : a) {
- String fileName = file.getName();
- if (file.isDirectory() && !fileName.endsWith(File.separator)) {
- fileName += File.separator;
- }
- v.add(fileName);
- }
-
- return v.elements();
- }
-
- // Override
- public void mkdir() throws IOException {
- if (!this.file.mkdir()) {
- throw new IOException("unable to create directory");
- }
-
- }
-
- // Override
- public DataInputStream openDataInputStream() throws IOException {
- return new DataInputStream(this.openInputStream());
- }
-
- // Override
- public DataOutputStream openDataOutputStream() throws IOException {
- return new DataOutputStream(this.openOutputStream());
- }
-
- // Override
- public InputStream openInputStream() throws IOException {
- return new FileInputStream(this.file);
- }
-
- // Override
- public OutputStream openOutputStream() throws IOException {
- return openOutputStream(0);
- }
-
- // Override
- public OutputStream openOutputStream(long byteOffset) throws IOException {
- boolean append;
- if (byteOffset == 0) {
- append = false;
- } else if (byteOffset < this.file.length()) {
- append = true;
- } else {
- throw new IOException("offsets not supported");
- }
- FileOutputStream fos = new FileOutputStream(this.file, append);
- return fos;
- }
-
- // Override
- public void rename(String newName) throws IOException {
- File directory = getParentDirectory(this.file);
- File targetFile = new File(directory, newName);
- this.file.renameTo(targetFile);
- }
-
- // Override
- public void setFileConnection(String fileName) throws IOException {
- if (fileName.equals("..")) {
- File directory = getParentDirectory(this.file);
- if (directory == null) {
- throw new IOException("no parent dir");
- }
- this.file = directory;
- } else {
- this.file = new File(this.file, fileName);
- }
- }
-
- // Override
- public void setHidden(boolean hidden) throws IOException {
- throw new IOException("unsupported");
- }
-
- // Override
- @TargetApi(9)
- public void setReadable(boolean readable) throws IOException {
- try {
- file.setReadable(readable); // ONLY API-9 (2.3+) GINGERBREAD
- }
- catch (NoSuchMethodError ex) { }
- }
-
- // Override
- @TargetApi(9)
- public void setWritable(boolean writable) throws IOException {
- try {
- file.setWritable(writable); // ONLY API-9 (2.3+) GINGERBREAD
- }
- catch (NoSuchMethodError ex) { }
- }
-
- // Override
- public long totalSize() {
- return this.file.length();
- }
-
- // Override
- public void truncate(long byteOffset) throws IOException {
- throw new IOException("unsupported");
- }
-
- // Override
- public long usedSize() {
- return this.file.length();
- }
-
- // Override
- public void close() throws IOException {
- this.open = false;
- }
-}
Deleted: AndroidME/src_Android/net/yura/android/io/HttpConnectionImpl.java
===================================================================
--- AndroidME/src_Android/net/yura/android/io/HttpConnectionImpl.java 2021-06-28 13:52:22 UTC (rev 2484)
+++ AndroidME/src_Android/net/yura/android/io/HttpConnectionImpl.java 2021-06-28 13:53:39 UTC (rev 2485)
@@ -1,240 +0,0 @@
-package net.yura.android.io;
-
-import java.io.DataInputStream;
-import java.io.DataOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.net.HttpURLConnection;
-import java.net.MalformedURLException;
-import java.net.URL;
-
-import javax.microedition.io.HttpConnection;
-
-public class HttpConnectionImpl implements HttpConnection {
-
- private static final int STATE_SETUP = 0;
- private static final int STATE_CONNECTED = 1;
-
- private int state = STATE_SETUP;
-
- private String urlString;
- private String requestMethod = GET;
-
- private URL url;
- private HttpURLConnection connection;
- private InputStream input;
- private OutputStream output;
-
- public HttpConnectionImpl(String url) {
- if (url == null) {
- throw new NullPointerException();
- }
- checkIsValidUrl(url);
- this.urlString = url;
- try {
- this.url = new URL(url);
- } catch (MalformedURLException e) {
- throw new IllegalArgumentException("invalid URL");
- }
- }
-
- public HttpConnectionImpl(String url, int mode) {
- this(url);
- }
-
- protected void checkIsValidUrl(String checkUrl) {
- if (checkUrl.indexOf("http:") != 0
- || (checkUrl.length() <= "http:".length())) {
- throw new IllegalArgumentException("invalid URL " + checkUrl);
- }
- }
-
- public void close() throws IOException {
- if (input != null) {
- input.close();
- }
- if (output != null) {
- output.close();
- }
- }
-
- public DataOutputStream openDataOutputStream() throws IOException {
- return new DataOutputStream(openOutputStream());
- }
-
- public OutputStream openOutputStream() throws IOException {
- if (output != null) {
- throw new IOException("already opened");
- }
- connect();
- connection.setDoOutput(true);
- output = this.connection.getOutputStream();
- return output;
- }
-
- // only in setup state
- public void setRequestMethod(String requestMethod) throws IOException {
- if (state == STATE_SETUP) {
- if (requestMethod.equals(GET)) {
- this.requestMethod = requestMethod;
- } else if (requestMethod.equals(POST)) {
- this.requestMethod = requestMethod;
- } else {
- throw new IllegalArgumentException("illegal request method "
- + requestMethod);
- }
- } else {
- throw new IOException("already connected");
- }
- }
-
- public void setRequestProperty(String key, String value) throws IOException {
- connect();
- connection.setRequestProperty(key, value);
- }
-
- // invoke at any time
- public String getRequestMethod() {
- return requestMethod;
- }
-
- public String getRequestProperty(String key) {
- try {
- connect();
- } catch (IOException e) {
- return "";
- }
- return connection.getRequestProperty(key);
- }
-
- public String getURL() {
- return urlString;
- }
-
- public String getQuery() {
- return url.getQuery();
- }
-
- public int getPort() {
- return url.getPort();
- }
-
- public String getHost() {
- return url.getHost();
- }
-
- public String getProtocol() {
- return url.getProtocol();
- }
-
- public String getFile() {
- return url.getFile();
- }
-
- public String getRef() {
- return url.getRef();
- }
-
- // these calls force transition to connected state
- public DataInputStream openDataInputStream() throws IOException {
- return new DataInputStream(openInputStream());
- }
-
- public InputStream openInputStream() throws IOException {
- connect();
- input = connection.getInputStream();
- return input;
- }
-
- public long getLength() {
- try {
- connect();
- } catch (IOException ex) {
- return 0;
- }
- return connection.getContentLength();
- }
-
- public String getType() {
- try {
- connect();
- } catch (IOException ex) {
- return "";
- }
- return connection.getContentType();
- }
-
- public String getEncoding() {
- try {
- connect();
- } catch (IOException ex) {
- return "";
- }
- return connection.getContentEncoding();
- }
-
- public String getHeaderField(String name) throws IOException {
- connect();
- return connection.getHeaderField(name);
- }
-
- public String getHeaderField(int n) throws IOException {
- connect();
- return connection.getHeaderField(n);
- }
-
- public int getResponseCode() throws IOException {
- connect();
- return connection.getResponseCode();
- }
-
- public String getResponseMessage() throws IOException {
- connect();
- return connection.getResponseMessage();
- }
-
- public int getHeaderFieldInt(String name, int def) throws IOException {
- connect();
- return connection.getHeaderFieldInt(name, def);
- }
-
- public long getHeaderFieldDate(String name, long def) throws IOException {
- connect();
- return connection.getHeaderFieldDate(name, def);
- }
-
- public String getHeaderFieldKey(int n) throws IOException {
- connect();
- return connection.getHeaderFieldKey(n);
- }
-
- public long getDate() throws IOException {
- connect();
- return connection.getDate();
- }
-
- public long getExpiration() throws IOException {
- connect();
- return connection.getExpiration();
- }
-
- public long getLastModified() throws IOException {
- connect();
- return connection.getLastModified();
- }
-
- protected synchronized void connect() throws IOException {
- if (state == STATE_CONNECTED) {
- if (connection == null) {
- throw new IOException(
- "Invalid State. No connection in state STATE_CONNECTED");
- }
- return;
- } else {
- state = STATE_CONNECTED;
- }
- connection = (HttpURLConnection) this.url.openConnection();
- connection.setRequestMethod(this.requestMethod);
- }
-}
Deleted: AndroidME/src_Android/net/yura/android/io/HttpsConnectionImpl.java
===================================================================
--- AndroidME/src_Android/net/yura/android/io/HttpsConnectionImpl.java 2021-06-28 13:52:22 UTC (rev 2484)
+++ AndroidME/src_Android/net/yura/android/io/HttpsConnectionImpl.java 2021-06-28 13:53:39 UTC (rev 2485)
@@ -1,36 +0,0 @@
-package net.yura.android.io;
-
-import java.io.IOException;
-
-import javax.microedition.io.Connector;
-import javax.microedition.io.HttpsConnection;
-import javax.microedition.io.SecurityInfo;
-
-public class HttpsConnectionImpl extends HttpConnectionImpl implements
- HttpsConnection {
-
- public HttpsConnectionImpl(String url) {
- this(url, Connector.READ_WRITE);
- }
-
- public HttpsConnectionImpl(String url, int mode) {
- super(url, mode);
- }
-
- protected void checkIsValidUrl(String checkUrl) {
- if (checkUrl.indexOf("https:") != 0
- || (checkUrl.length() <= "https:".length())) {
- throw new IllegalArgumentException("invalid URL " + checkUrl);
- }
- }
-
- public SecurityInfo getSecurityInfo() throws IOException {
- connect();
- // TODO implement security info
- return null;
- }
-
- public int getPort() {
- return super.getPort();
- }
-}
Modified: AndroidME/src_MIDP/javax/microedition/io/Connector.java
===================================================================
--- AndroidME/src_MIDP/javax/microedition/io/Connector.java 2021-06-28 13:52:22 UTC (rev 2484)
+++ AndroidME/src_MIDP/javax/microedition/io/Connector.java 2021-06-28 13:53:39 UTC (rev 2485)
@@ -1,20 +1,18 @@
package javax.microedition.io;
-
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-
import net.yura.android.io.AndroidAssetConnection;
import net.yura.android.io.AndroidURLConnection;
-import net.yura.android.io.AndroidFileConnection;
-import net.yura.android.io.HttpConnectionImpl;
-import net.yura.android.io.HttpsConnectionImpl;
import net.yura.android.io.ServerSocketConnection;
import net.yura.android.io.SocketConnection;
import net.yura.android.messaging.MessageConnectionImpl;
+import net.yura.midp.io.HttpConnectionImpl;
+import net.yura.midp.io.HttpsConnectionImpl;
+import net.yura.midp.io.JavaFileConnection;
public class Connector {
public static final int READ = 0x01;
@@ -40,7 +38,7 @@
connection = new AndroidAssetConnection(name.substring( PROTOCOL_ASSET.length() ));
}
else if (name.startsWith(PROTOCOL_FILE)) {
- connection = new AndroidFileConnection(name);
+ connection = new JavaFileConnection(name);
}
else if (name.startsWith(PROTOCOL_SOCKET)) {
connection = getSocketConnection(name);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|