/*
 * Copyright 2018 Nikita Shakarun
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.microemu.cldc.resource;

import org.microemu.microedition.io.ConnectionImplementation;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.InputConnection;
import javax.microedition.util.ContextHolder;

public class Connection implements InputConnection, ConnectionImplementation {

	private final static String PROTOCOL = "resource://";

	private final static String PROTOCOL2 = "resource:";

	private String path;

	private InputStream stream;

	@Override
	public javax.microedition.io.Connection openConnection(String name, int mode, boolean timeouts) throws IOException {
		path = name.replace(PROTOCOL, "").replace(PROTOCOL2, "");
		return this;
	}

	@Override
	public InputStream openInputStream() throws IOException {
		if (stream != null) {
			throw new IOException("InputStream already opened");
		}
		stream = ContextHolder.getResourceAsStream(null, path);
		if (stream == null) {
			throw new IOException("File not found");
		}
		return stream;
	}

	@Override
	public DataInputStream openDataInputStream() throws IOException {
		return new DataInputStream(openInputStream());
	}

	@Override
	public void close() throws IOException {
		stream.close();
		stream = null;
	}
}

@@ -1620,22 +1620,47 @@
 %09return 
+new ResourceInputStream(
 stream
+)
 ;%0A%09%7D%0A%0A%09@
@@ -1859,16 +1859,16 @@
 lose();%0A
-
 %09%09stream
@@ -1878,11 +1878,459 @@
 null;%0A%09%7D
+%0A%0A%09class ResourceInputStream extends InputStream %7B%0A%0A%09%09private InputStream inputStream;%0A%0A%09%09ResourceInputStream(InputStream inputStream) %7B%0A%09%09%09this.inputStream = inputStream;%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public int read() throws IOException %7B%0A%09%09%09return inputStream.read();%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public int read(byte%5B%5D b, int off, int len) throws IOException %7B%0A%09%09%09int result = inputStream.read(b, off, len);%0A%09%09%09if (result == -1) result = 0;%0A%09%09%09return result;%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

Fix Siemens InputStream