deprecated readLine() What to change?

NestedCodeblocksFTW picture NestedCodeblocksFTW · Nov 18, 2012 · Viewed 12.6k times · Source

I'm a Java beginner, trying to get this to work in Eclipse. However, the readLine is struck through and a notice says it is deprecated. The code works, albeit not the while ((var2 = var5.readLine()) != null) { bit ... So I'm wondering how to fix it.

final class ScreenShotHelper$1 implements Runnable
{
    public void run()
    {
        try
        {
            String var1 = ScreenShotHelper.access$000().getAbsolutePath();
            String var2 = "";
            HttpURLConnection var3 = null;
            DataOutputStream var4 = null;
            DataInputStream var5 = null;
            String var6 = "\r\n";
            String var7 = "--";
            String var8 = "*****";
            String var9 = "";
            int var10 = 1048576;
            String var11 = "";
            var9 = Minecraft.getMinecraft().thePlayer.username;
            String var12 = "http://localhost/screenupload/index.php?playername=" + var9;
            try
            {
                FileInputStream var13 = new FileInputStream(new File(var1));
                URL var14 = new URL(var12);
                var3 = (HttpURLConnection)var14.openConnection();
                var3.setDoInput(true);
                var3.setDoOutput(true);
                var3.setUseCaches(false);
                var3.setRequestMethod("POST");
                var3.setRequestProperty("Connection", "Keep-Alive");
                var3.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + var8);
                var4 = new DataOutputStream(var3.getOutputStream());
                var4.writeBytes(var7 + var8 + var6);
                var4.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + var1 + "\"" + var6);
                var4.writeBytes(var6);
                int var15 = var13.available();
                int var16 = Math.min(var15, var10);
                byte[] var17 = new byte[var16];

                for (int var18 = var13.read(var17, 0, var16); var18 > 0; var18 = var13.read(var17, 0, var16))
                {
                    var4.write(var17, 0, var16);
                    var15 = var13.available();
                    var16 = Math.min(var15, var10);
                }
                var4.writeBytes(var6);
                var4.writeBytes(var7 + var8 + var7 + var6);
                System.out.println("File is written");
                var13.close();
                var4.flush();
                var4.close();
            }
            catch (MalformedURLException var20)
            {
                System.out.println("error1: " + var20.getMessage());
            }
            catch (IOException var21)
            {
                System.out.println("error2: " + var21.getMessage());
            }

            try
            {
                var5 = new DataInputStream(var3.getInputStream());

                while ((var2 = var5.readLine()) != null) {
                    System.out.println("Server Response " + var2);
                    ScreenShotHelper.mc.thePlayer.addChatMessage("\u00a7aSuccessfully uploaded screenshot!  Direct link:");
                    ScreenShotHelper.mc.thePlayer.addChatMessage("\u00a7a" + var2);
                }
                var5.close();
            }
            catch (IOException var19)
            {
                System.out.println("error3: " + var19.getMessage());
            }
        }
        catch (Exception var22)
        {
            var22.printStackTrace();
            ScreenShotHelper.mc.thePlayer.addChatMessage("\u00a74failed to save");
        }
    }
}

Answer

erickson picture erickson · Nov 19, 2012
  • Give your variables meaningful names.
  • Declare your variables in the smallest possible scope.
  • Don't assign dummy values (like null or "") to variables.
  • Avoid side-effects in tests.
  • Use automatic resource management to cleanly ensure that streams are closed.
  • Determine and use the correct character encoding when converting bytes to characters.

Here is an example of your code that applies these points:

try (InputStream is = connection.getInputStream()) {
  BufferedReader lines = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  while (true) {
    String line = lines.readLine();
    if (line == null)
      break;
    System.out.println("Server Response " + line);
    ...
  }
}