Skip to content

Commit

Permalink
Siemens: Implement siemens.mp.io
Browse files Browse the repository at this point in the history
Rayman Golf now no longer crashes at boot, it destroys itself at
boot. Yay!

But the package itself seems to work okay, as the directory and
files get accessed no problem.
  • Loading branch information
AShiningRay committed Feb 23, 2025
1 parent 503b08a commit 6086cbc
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/com/siemens/mp/io/Connection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
This file is part of FreeJ2ME.
FreeJ2ME is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FreeJ2ME is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FreeJ2ME. If not, see http://www.gnu.org/licenses/
*/
package com.siemens.mp.io;

import org.recompile.mobile.Mobile;

public class Connection
{
String connectTo;
ConnectionListener listener;

Connection(String connectTo) { this.connectTo = connectTo; }

public void send(byte[] data)
{
Mobile.log(Mobile.LOG_WARNING, Connection.class.getPackage().getName() + "." + Connection.class.getSimpleName() + ": " + "send(byte[]) untested");
if (listener != null) { listener.receiveData(data); }
}

public void setListener(ConnectionListener listener) { this.listener = listener; }

public ConnectionListener getListener() { return listener; }
}
22 changes: 22 additions & 0 deletions src/com/siemens/mp/io/ConnectionListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
This file is part of FreeJ2ME.
FreeJ2ME is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FreeJ2ME is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FreeJ2ME. If not, see http://www.gnu.org/licenses/
*/
package com.siemens.mp.io;

public interface ConnectionListener
{
public void receiveData(byte[] data);
}
198 changes: 198 additions & 0 deletions src/com/siemens/mp/io/File.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/*
This file is part of FreeJ2ME.
FreeJ2ME is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FreeJ2ME is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FreeJ2ME. If not, see http://www.gnu.org/licenses/
*/
package com.siemens.mp.io;

//import java.io.File; // Can't import that here, as this class is also called "File"
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.recompile.mobile.Mobile;

public class File
{
public static final int INSIDE_STORAGE_PATH = 1;
public static final int OUTSIDE_STORAGE_PATH = 0;
public static final String STORAGE_DRIVE = "a:";

private static final Map<Integer, RandomAccessFile> openFiles = new HashMap<Integer, RandomAccessFile>();
private static int lastFileDescriptor;

public File() { }

public static String buildPath(String fileName)
{
Mobile.log(Mobile.LOG_ERROR, File.class.getPackage().getName() + "." + File.class.getSimpleName() + ": " + "buildPath(string) not implemented");
return fileName;
}

public static int checkFileName(String fileName)
{
return fileName.indexOf(':') == -1 ? INSIDE_STORAGE_PATH : OUTSIDE_STORAGE_PATH;
}

public int close(int fileDescriptor) throws IOException
{
RandomAccessFile closeFile = openFiles.get(fileDescriptor);
if (closeFile != null)
{
openFiles.remove(fileDescriptor);
closeFile.close();
return fileDescriptor;
}
Mobile.log(Mobile.LOG_ERROR, File.class.getPackage().getName() + "." + File.class.getSimpleName() + ": " + "could not close fileDescriptor " + fileDescriptor);
return -1;
}

public static int copy(String source, String dest) throws IOException
{
java.io.File sourceFile = findFile(source);
java.io.File destFile = findFile(dest);
FileInputStream fis = new FileInputStream(sourceFile);
try
{
FileChannel sourceChannel = fis.getChannel();
try
{
FileOutputStream fos = new FileOutputStream(destFile);
try
{
FileChannel destChannel = fos.getChannel();
try { destChannel.transferFrom(sourceChannel, 0, sourceChannel.size()); }
finally { destChannel.close(); }
}
finally { fos.close(); }
}
finally { sourceChannel.close(); }
}
finally { fis.close(); }

Mobile.log(Mobile.LOG_ERROR, File.class.getPackage().getName() + "." + File.class.getSimpleName() + ": " + "could not copy data from " + source + " to " + dest);
return 1;
}

public static int debugWrite(String fileName, String infoString) throws IOException
{
try (FileWriter writer = new FileWriter(fileName, true))
{
writer.write(infoString + System.lineSeparator());
writer.flush();
return 1;
}
catch (IOException e) { throw new IOException("Failed to write to the file: " + e.getMessage(), e); }
}

public static int delete(String fileName) throws IOException { return findFile(fileName).delete() ? 1 : -1; }

public static int exists(String fileName) throws IOException { return findFile(fileName).exists() ? 1 : -1; }

public static boolean isDirectory(String pathName) throws IOException { return findFile(pathName).isDirectory(); }

public int seek(int fileDescriptor, int seekpos) throws IOException
{
RandomAccessFile file = openFiles.get(fileDescriptor);
if (file == null) { return -1; }

file.seek(seekpos);
return (int) file.getFilePointer();
}

public int length(int fileDescriptor) throws IOException
{
RandomAccessFile file = openFiles.get(fileDescriptor);
if (file == null) { return -1; }

return (int) file.length();
}

public static String[] list(String pathName) throws IOException
{
String[] files = findFile(pathName).list();
if (files == null) { return new String[0]; }

Arrays.sort(files);
return files;
}

public int open(String fileName) throws IOException
{
System.out.println("Opening " + fileName);
java.io.File file = findFile(fileName);
RandomAccessFile rfile = new RandomAccessFile(file, "rw");
openFiles.put(++lastFileDescriptor, rfile);
return lastFileDescriptor;
}

public int read(int fileDescriptor, byte[] buf, int offset, int numBytes) throws IOException
{
System.out.println("Opening " + fileDescriptor);
RandomAccessFile file = openFiles.get(fileDescriptor);
if (file == null) { return -1; }

return file.read(buf, offset, numBytes);
}

public static int rename(String source, String dest) throws IOException { return findFile(source).renameTo(findFile(dest)) ? 1 : -1; }

public static int spaceAvailable() throws IOException
{
Mobile.log(Mobile.LOG_WARNING, File.class.getPackage().getName() + "." + File.class.getSimpleName() + ": " + "spaceAvailable() measurements not implemented");
return Integer.MAX_VALUE;
}

public static void truncate(int fileDescriptor, int size) throws IOException
{
RandomAccessFile file = openFiles.get(fileDescriptor);
if (file == null) { return; }

file.setLength(size);
}

public int write(int fileDescriptor, byte[] buf, int offset, int numBytes) throws IOException
{
RandomAccessFile file = openFiles.get(fileDescriptor);
if (file == null) { return -1; }

file.write(buf, offset, numBytes);
return numBytes;
}


private static java.io.File findFile(String fileName) throws IOException
{
java.io.File file;
int colon = fileName.indexOf(':');

if (colon == -1) { file = new java.io.File(Mobile.SIEMENS_DATA_PATH, fileName); }
else
{
fileName = fileName.substring(colon + 2);
file = new java.io.File(Mobile.SIEMENS_DATA_PATH, fileName);
}

// Create siemens dir if not available yet
file.getParentFile().mkdirs();

return file;
}
}
1 change: 1 addition & 0 deletions src/org/recompile/mobile/Mobile.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class Mobile
// Enable/disable logging to the console and optionally to a file
public static boolean logging = true;
private static final String LOG_FILE = "freej2me_system" + File.separatorChar + "FreeJ2ME.log";
public static final String SIEMENS_DATA_PATH = "freej2me_system" + File.separatorChar + "SiemensData" + File.separatorChar;
public static byte minLogLevel = 1;

// Log Levels
Expand Down

0 comments on commit 6086cbc

Please sign in to comment.