/*
 * 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 new ResourceInputStream(stream);
	}

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

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

	class ResourceInputStream extends InputStream {

		private InputStream inputStream;

		ResourceInputStream(InputStream inputStream) {
			this.inputStream = inputStream;
		}

		@Override
		public int read() throws IOException {
			return inputStream.read();
		}

		@Override
		public int read(byte[] b, int off, int len) throws IOException {
			int result = inputStream.read(b, off, len);
			if (result == -1) result = 0;
			return result;
		}
	}
}

@@ -1117,16 +1117,52 @@
  stream;
+%0A%09private boolean isSiemensProtocol;
 %0A%0A%09@Over
@@ -1290,65 +1290,205 @@
 %7B%0A%09%09
-path = name.replace(PROTOCOL, %22%22).replace(PROTOCOL2, %22%22);
+if (name.startsWith(PROTOCOL)) %7B%0A%09%09%09path = name.substring(PROTOCOL.length());%0A%09%09%09isSiemensProtocol = false;%0A%09%09%7D else %7B%0A%09%09%09path = name.substring(PROTOCOL2.length());%0A%09%09%09isSiemensProtocol = true;%0A%09%09%7D
 %0A%09%09r
@@ -1783,24 +1783,52 @@
 ound%22);%0A%09%09%7D%0A
+%09%09if (isSiemensProtocol) %7B%0A%09
 %09%09return new
@@ -1857,16 +1857,49 @@
 tream);%0A
+%09%09%7D else %7B%0A%09%09%09return stream;%0A%09%09%7D%0A
 %09%7D%0A%0A%09@Ov
@@ -2078,16 +2078,41 @@
 ption %7B%0A
+%09%09if (stream != null) %7B%0A%09
 %09%09stream
@@ -2121,16 +2121,17 @@
 lose();%0A
+%09
 %09%09stream
@@ -2139,21 +2139,32 @@
 = null;%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09
+static 
 class Re
@@ -2206,32 +2206,38 @@
 am %7B%0A%0A%09%09private 
+final 
 InputStream inpu
@@ -2570,16 +2570,22 @@
  == -1) 
+%7B%0A%09%09%09%09
 result =
@@ -2588,16 +2588,21 @@
 lt = 0;%0A
+%09%09%09%7D%0A
 %09%09%09retur

Handle non Siemens resource connection