<commit_before>package php.runtime.ext.net;

import php.runtime.Memory;
import php.runtime.annotation.Reflection.*;
import php.runtime.env.Environment;
import php.runtime.ext.NetExtension;
import php.runtime.ext.core.classes.stream.Stream;
import php.runtime.memory.BinaryMemory;
import php.runtime.memory.LongMemory;
import php.runtime.reflection.ClassEntity;

import java.io.*;
import java.net.*;
import java.util.Arrays;

@Name(NetExtension.NAMESPACE + "NetStream")
@Stream.UsePathWithProtocols
public class WrapNetStream extends Stream {
    protected URL url;

    protected Proxy proxy;

    protected boolean eof = false;
    protected int position = 0;

    protected URLConnection urlConnection;

    protected InputStream currentInputStream;
    protected OutputStream currentOutputStream;

    public WrapNetStream(Environment env, URL url) {
        super(env);
        this.url = url;
    }

    public WrapNetStream(Environment env, ClassEntity clazz) {
        super(env, clazz);
    }

    public URLConnection getURLConnection() throws IOException {
        if (urlConnection == null) {
            if (proxy != null) {
                urlConnection = url.openConnection(proxy);
            } else {
                urlConnection = url.openConnection();
            }

            urlConnection.setDoInput(false);
            urlConnection.setDoOutput(false);

            if (getMode().startsWith("r") || getMode().startsWith("w") || getMode().startsWith("a")) {
                urlConnection.setDoInput(true);
            }

            if (getMode().startsWith("w") || getMode().startsWith("a")) {
                urlConnection.setDoOutput(true);
            }
        }

        return urlConnection;
    }

    public HttpURLConnection getHttpURLConnection() throws IOException {
        return (HttpURLConnection) getURLConnection();
    }

    public OutputStream getOutputStream() throws IOException {
        if (currentOutputStream == null) {
            currentOutputStream = getURLConnection().getOutputStream();
        }

        return currentOutputStream;
    }

    public InputStream getInputStream() throws IOException {
        if (currentInputStream == null) {
            URLConnection urlConnection = getURLConnection();
            try {
                currentInputStream = urlConnection.getInputStream();
            } catch (IOException e) {
                if (urlConnection instanceof HttpURLConnection) {
                    currentInputStream = ((HttpURLConnection) urlConnection).getErrorStream();
                } else {
                    throw e;
                }
            }
        }

        return currentInputStream;
    }

    @Override
    @Signature({@Arg("path"), @Arg(value = "mode", optional = @Optional("w+"))})
    public Memory __construct(Environment env, Memory... args) throws IOException {
        super.__construct(env, args);

        if (getMode() == null) {<commit_msg>Fix NPE bug in net streams.<commit_after>package php.runtime.ext.net;

import php.runtime.Memory;
import php.runtime.annotation.Reflection.*;
import php.runtime.env.Environment;
import php.runtime.ext.NetExtension;
import php.runtime.ext.core.classes.stream.Stream;
import php.runtime.memory.BinaryMemory;
import php.runtime.memory.LongMemory;
import php.runtime.reflection.ClassEntity;

import java.io.*;
import java.net.*;
import java.util.Arrays;

@Name(NetExtension.NAMESPACE + "NetStream")
@Stream.UsePathWithProtocols
public class WrapNetStream extends Stream {
    protected URL url;

    protected Proxy proxy;

    protected boolean eof = false;
    protected int position = 0;

    protected URLConnection urlConnection;

    protected InputStream currentInputStream;
    protected OutputStream currentOutputStream;

    public WrapNetStream(Environment env, URL url) {
        super(env);
        this.url = url;
    }

    public WrapNetStream(Environment env, ClassEntity clazz) {
        super(env, clazz);
    }

    public URLConnection getURLConnection() throws IOException {
        if (urlConnection == null) {
            if (proxy != null) {
                urlConnection = url.openConnection(proxy);
            } else {
                urlConnection = url.openConnection();
            }

            urlConnection.setDoInput(false);
            urlConnection.setDoOutput(false);

            if (getMode().startsWith("r") || getMode().startsWith("w") || getMode().startsWith("a")) {
                urlConnection.setDoInput(true);
            }

            if (getMode().startsWith("w") || getMode().startsWith("a")) {
                urlConnection.setDoOutput(true);
            }
        }

        return urlConnection;
    }

    public HttpURLConnection getHttpURLConnection() throws IOException {
        return (HttpURLConnection) getURLConnection();
    }

    public OutputStream getOutputStream() throws IOException {
        if (currentOutputStream == null) {
            currentOutputStream = getURLConnection().getOutputStream();
        }

        return currentOutputStream;
    }

    synchronized public InputStream getInputStream() throws IOException {
        if (currentInputStream == null) {
            URLConnection urlConnection = getURLConnection();
            try {
                currentInputStream = urlConnection.getInputStream();

                if (currentInputStream == null && urlConnection instanceof HttpURLConnection) {
                    currentInputStream = ((HttpURLConnection) urlConnection).getErrorStream();
                }
            } catch (IOException e) {
                if (urlConnection instanceof HttpURLConnection) {
                    currentInputStream = ((HttpURLConnection) urlConnection).getErrorStream();
                } else {
                    throw e;
                }
            }
        }

        if (currentInputStream == null) {
            throw new IOException("Unable to get input stream for connection " + getPath());
        }

        return currentInputStream;
    }

    @Override
    @Signature({@Arg("path"), @Arg(value = "mode", optional = @Optional("w+"))})
    public Memory __construct(Environment env, Memory... args) throws IOException {
        super.__construct(env, args);

        if (getMode() == null) {
{'fraction_non_alphanumeric': 0.1009714922758401, 'fraction_numerical': 0.00031852205765249244, 'mean_word_length': 2.4638720353006067, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6983540', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1509, 'n_words': 362}