WeixiangYan/CodeTransOcean huggingface dataset:
Task: Take notes on the command line








VB: Imports System.IO

Module Notes
    Function Main(ByVal cmdArgs() As String) As Integer
        Try
            If cmdArgs.Length = 0 Then
                Using sr As New StreamReader("NOTES.TXT")
                    Console.WriteLine(sr.ReadToEnd)
                End Using
            Else
                Using sw As New StreamWriter("NOTES.TXT", True)
                    sw.WriteLine(Date.Now.ToString())
                    sw.WriteLine("{0}{1}", ControlChars.Tab, String.Join(" ", cmdArgs))
                End Using
            End If
        Catch
        End Try
    End Function
End Module


COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. NOTES.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT OPTIONAL notes ASSIGN TO "NOTES.TXT"
               ORGANIZATION LINE SEQUENTIAL
               FILE STATUS note-status.

       DATA DIVISION.
       FILE SECTION.
       FD  notes.
       01  note-record       PIC X(256).

       LOCAL-STORAGE SECTION.
       01  note-status       PIC 99.
           88  notes-ok      VALUE 0 THRU 9.

       01  date-now.
           03  current-year  PIC 9(4).
           03  current-month PIC 99.
           03  current-day   PIC 99.

       01  time-now.
           03  current-hour  PIC 99.
           03  current-min   PIC 99.
           03  current-sec   PIC 99.

       01  args              PIC X(256).

       PROCEDURE DIVISION.
       DECLARATIVES.
       note-error SECTION.
           USE AFTER STANDARD ERROR PROCEDURE ON notes.

           DISPLAY "Error using NOTES.TXT. Error code: " note-status
           .
       END DECLARATIVES.

       main.
           ACCEPT args FROM COMMAND-LINE

*          
           IF args = SPACES
               OPEN INPUT notes

               PERFORM FOREVER
*                  
*                  
                   READ notes 
                       AT END
                           EXIT PERFORM

                       NOT AT END
                           DISPLAY FUNCTION TRIM(note-record)
                   END-READ
               END-PERFORM
           ELSE
               OPEN EXTEND notes

*              
               ACCEPT date-now FROM DATE YYYYMMDD
               ACCEPT time-now FROM TIME
               STRING current-year "-" current-month "-" current-day
                   " " current-hour ":" current-min ":" current-sec
                   INTO note-record
               WRITE note-record
               
*              
               STRING X"09", args INTO note-record
               WRITE note-record
           END-IF

           CLOSE notes

           GOBACK
           .
Task: Take notes on the command line




Go: package main

import (
	"fmt"
	"io"
	"os"
	"strings"
	"time"
)

func addNote(fn string, note string) error {
	f, err := os.OpenFile(fn, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
	if err != nil {
		return err
	}
	_, err = fmt.Fprint(f, time.Now().Format(time.RFC1123), "\n\t", note, "\n")
	
	if cErr := f.Close(); err == nil {
		err = cErr
	}
	return err
}

func showNotes(w io.Writer, fn string) error {
	f, err := os.Open(fn)
	if err != nil {
		if os.IsNotExist(err) {
			return nil 
		}
		return err
	}
	_, err = io.Copy(w, f)
	f.Close()
	return err
}

func main() {
	const fn = "NOTES.TXT"
	var err error
	if len(os.Args) > 1 {
		err = addNote(fn, strings.Join(os.Args[1:], " "))
	} else {
		err = showNotes(os.Stdout, fn)
	}
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}






COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. NOTES.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT OPTIONAL notes ASSIGN TO "NOTES.TXT"
               ORGANIZATION LINE SEQUENTIAL
               FILE STATUS note-status.

       DATA DIVISION.
       FILE SECTION.
       FD  notes.
       01  note-record       PIC X(256).

       LOCAL-STORAGE SECTION.
       01  note-status       PIC 99.
           88  notes-ok      VALUE 0 THRU 9.

       01  date-now.
           03  current-year  PIC 9(4).
           03  current-month PIC 99.
           03  current-day   PIC 99.

       01  time-now.
           03  current-hour  PIC 99.
           03  current-min   PIC 99.
           03  current-sec   PIC 99.

       01  args              PIC X(256).

       PROCEDURE DIVISION.
       DECLARATIVES.
       note-error SECTION.
           USE AFTER STANDARD ERROR PROCEDURE ON notes.

           DISPLAY "Error using NOTES.TXT. Error code: " note-status
           .
       END DECLARATIVES.

       main.
           ACCEPT args FROM COMMAND-LINE

*          
           IF args = SPACES
               OPEN INPUT notes

               PERFORM FOREVER
*                  
*                  
                   READ notes 
                       AT END
                           EXIT PERFORM

                       NOT AT END
                           DISPLAY FUNCTION TRIM(note-record)
                   END-READ
               END-PERFORM
           ELSE
               OPEN EXTEND notes

*              
               ACCEPT date-now FROM DATE YYYYMMDD
               ACCEPT time-now FROM TIME
               STRING current-year "-" current-month "-" current-day
                   " " current-hour ":" current-min ":" current-sec
                   INTO note-record
               WRITE note-record
               
*              
               STRING X"09", args INTO note-record
               WRITE note-record
           END-IF

           CLOSE notes

           GOBACK
           .
Task: Take notes on the command line






PHP: #!/usr/bin/php
<?php
if ($argc > 1)
    file_put_contents(
        'notes.txt', 
        date('r')."\n\t".implode(' ', array_slice($argv, 1))."\n",
        FILE_APPEND
    );
else
    @readfile('notes.txt');
Task: Take notes on the command line






PHP: #!/usr/bin/php
<?php
if ($argc > 1)
    file_put_contents(
        'notes.txt', 
        date('r')."\n\t".implode(' ', array_slice($argv, 1))."\n",
        FILE_APPEND
    );
else
    @readfile('notes.txt');






Ada: with Ada.Calendar.Formatting;
with Ada.Characters.Latin_1;
with Ada.Command_Line;
with Ada.IO_Exceptions;
with Ada.Text_IO;
procedure Notes is
   Notes_Filename : constant String := "notes.txt";
   Notes_File     : Ada.Text_IO.File_Type;
   Argument_Count : Natural         := Ada.Command_Line.Argument_Count;
begin
   if Argument_Count = 0 then
      begin
         Ada.Text_IO.Open
           (File => Notes_File,
            Mode => Ada.Text_IO.In_File,
            Name => Notes_Filename);
         while not Ada.Text_IO.End_Of_File (File => Notes_File) loop
            Ada.Text_IO.Put_Line (Ada.Text_IO.Get_Line (File => Notes_File));
         end loop;
      exception
         when Ada.IO_Exceptions.Name_Error =>
            null;
      end;
   else
      begin
         Ada.Text_IO.Open
           (File => Notes_File,
            Mode => Ada.Text_IO.Append_File,
            Name => Notes_Filename);
      exception
         when Ada.IO_Exceptions.Name_Error =>
            Ada.Text_IO.Create (File => Notes_File, Name => Notes_Filename);
      end;
      Ada.Text_IO.Put_Line
        (File => Notes_File,
         Item => Ada.Calendar.Formatting.Image (Date => Ada.Calendar.Clock));
      Ada.Text_IO.Put (File => Notes_File, Item => Ada.Characters.Latin_1.HT);
      for I in 1 .. Argument_Count loop
         Ada.Text_IO.Put
           (File => Notes_File,
            Item => Ada.Command_Line.Argument (I));
         if I /= Argument_Count then
            Ada.Text_IO.Put (File => Notes_File, Item => ' ');
         end if;
      end loop;
      Ada.Text_IO.Flush (File => Notes_File);
   end if;
   if Ada.Text_IO.Is_Open (File => Notes_File) then
      Ada.Text_IO.Close (File => Notes_File);
   end if;
end Notes;
Task: Take notes on the command line






PHP: #!/usr/bin/php
<?php
if ($argc > 1)
    file_put_contents(
        'notes.txt', 
        date('r')."\n\t".implode(' ', array_slice($argv, 1))."\n",
        FILE_APPEND
    );
else
    @readfile('notes.txt');




COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. NOTES.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT OPTIONAL notes ASSIGN TO "NOTES.TXT"
               ORGANIZATION LINE SEQUENTIAL
               FILE STATUS note-status.

       DATA DIVISION.
       FILE SECTION.
       FD  notes.
       01  note-record       PIC X(256).

       LOCAL-STORAGE SECTION.
       01  note-status       PIC 99.
           88  notes-ok      VALUE 0 THRU 9.

       01  date-now.
           03  current-year  PIC 9(4).
           03  current-month PIC 99.
           03  current-day   PIC 99.

       01  time-now.
           03  current-hour  PIC 99.
           03  current-min   PIC 99.
           03  current-sec   PIC 99.

       01  args              PIC X(256).

       PROCEDURE DIVISION.
       DECLARATIVES.
       note-error SECTION.
           USE AFTER STANDARD ERROR PROCEDURE ON notes.

           DISPLAY "Error using NOTES.TXT. Error code: " note-status
           .
       END DECLARATIVES.

       main.
           ACCEPT args FROM COMMAND-LINE

*          
           IF args = SPACES
               OPEN INPUT notes

               PERFORM FOREVER
*                  
*                  
                   READ notes 
                       AT END
                           EXIT PERFORM

                       NOT AT END
                           DISPLAY FUNCTION TRIM(note-record)
                   END-READ
               END-PERFORM
           ELSE
               OPEN EXTEND notes

*              
               ACCEPT date-now FROM DATE YYYYMMDD
               ACCEPT time-now FROM TIME
               STRING current-year "-" current-month "-" current-day
                   " " current-hour ":" current-min ":" current-sec
                   INTO note-record
               WRITE note-record
               
*              
               STRING X"09", args INTO note-record
               WRITE note-record
           END-IF

           CLOSE notes

           GOBACK
           .
Task: Take notes on the command line







Python: import sys, datetime, shutil

if len(sys.argv) == 1:
    try:
        with open('notes.txt', 'r') as f:
            shutil.copyfileobj(f, sys.stdout)
    except IOError:
        pass
else:
    with open('notes.txt', 'a') as f:
        f.write(datetime.datetime.now().isoformat() + '\n')
        f.write("\t%s\n" % ' '.join(sys.argv[1:]))
Task: Base64 encode data



C#: namespace RosettaCode.Base64EncodeData
{
    using System;
    using System.Net;

    internal static class Program
    {
        private static void Main()
        {
            const string path = "http:

            byte[] input;
            using (var client = new WebClient())
            {
                input = client.DownloadData(path);
            }

            var output = Convert.ToBase64String(input);
            Console.WriteLine(output);
        }
    }
}









Ada: with Ada.Text_IO;

with AWS.Response;
with AWS.Client;
with AWS.Translator;

procedure Encode_AWS is
   URL     : constant String := "http://rosettacode.org/favicon.ico";
   Page    : constant AWS.Response.Data := AWS.Client.Get (URL);
   Payload : constant String := AWS.Response.Message_Body (Page);
   Icon_64 : constant String := AWS.Translator.Base64_Encode (Payload);
begin
   Ada.Text_IO.Put_Line (Icon_64);
end Encode_AWS;
Task: Base64 encode data

C: #include <stdio.h>
#include <stdlib.h>
#include <resolv.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

int main() {
  int fin = open("favicon.ico",  O_RDONLY);
  if (fin == -1)
    return 1;

  struct stat st;
  if (fstat(fin, &st))
    return 1;

  void *bi = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fin,  0);
  if (bi == MAP_FAILED)
    return 1;

  int outLength = ((st.st_size + 2) / 3) * 4 + 1;
  char *outBuffer = malloc(outLength);
  if (outBuffer == NULL)
    return 1;

  int encodedLength = b64_ntop(bi, st.st_size, outBuffer, outLength);
  if (encodedLength < 0)
    return 1;

  puts(outBuffer);

  free(outBuffer);
  munmap(bi, st.st_size);
  close(fin);

  return 0;
}











Ada: with Ada.Text_IO;

with AWS.Response;
with AWS.Client;
with AWS.Translator;

procedure Encode_AWS is
   URL     : constant String := "http://rosettacode.org/favicon.ico";
   Page    : constant AWS.Response.Data := AWS.Client.Get (URL);
   Payload : constant String := AWS.Response.Message_Body (Page);
   Icon_64 : constant String := AWS.Translator.Base64_Encode (Payload);
begin
   Ada.Text_IO.Put_Line (Icon_64);
end Encode_AWS;
Task: Base64 encode data


C++: #include <iostream>
#include <fstream>
#include <vector>

typedef unsigned char byte;
using namespace std;

const unsigned m1 = 63 << 18, m2 = 63 << 12, m3 = 63 << 6;

class base64
{
public:
    base64() { char_set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; }
    string encode( vector<byte> v )
    {
	string res;
	unsigned d, a = 0, l = static_cast<unsigned>( v.size() );
	while( l > 2 )
	{
	    d = v[a++] << 16 | v[a++] << 8 | v[a++];	
	    res.append( 1, char_set.at( ( d & m1 ) >> 18 ) );	
	    res.append( 1, char_set.at( ( d & m2 ) >> 12 ) );	
	    res.append( 1, char_set.at( ( d & m3 ) >>  6 ) );
	    res.append( 1, char_set.at( d & 63 ) );
	    l -= 3;
	}
	if( l == 2 )
	{
	    d = v[a++] << 16 | v[a++] << 8;
	    res.append( 1, char_set.at( ( d & m1 ) >> 18 ) );	
	    res.append( 1, char_set.at( ( d & m2 ) >> 12 ) );	
	    res.append( 1, char_set.at( ( d & m3 ) >>  6 ) );
	    res.append( 1, '=' );
	}
	else if( l == 1 )
	{
	    d = v[a++] << 16;
	    res.append( 1, char_set.at( ( d & m1 ) >> 18 ) );	
	    res.append( 1, char_set.at( ( d & m2 ) >> 12 ) );	
	    res.append( "==", 2 );
	}
	return res;
    }

private:
    string char_set;
};

int main( int argc, char* argv[] )
{
    base64 b;
    basic_ifstream<byte> f( "favicon.ico", ios::binary );
    string r = b.encode( vector<byte>( ( istreambuf_iterator<byte>( f ) ), istreambuf_iterator<byte>() ) );
    copy( r.begin(), r.end(), ostream_iterator<char>( cout ) );
    return 0;
}










Ada: with Ada.Text_IO;

with AWS.Response;
with AWS.Client;
with AWS.Translator;

procedure Encode_AWS is
   URL     : constant String := "http://rosettacode.org/favicon.ico";
   Page    : constant AWS.Response.Data := AWS.Client.Get (URL);
   Payload : constant String := AWS.Response.Message_Body (Page);
   Icon_64 : constant String := AWS.Translator.Base64_Encode (Payload);
begin
   Ada.Text_IO.Put_Line (Icon_64);
end Encode_AWS;
Task: Base64 encode data




Go: package main

import (
    "encoding/base64"
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    r, err := http.Get("http:
    if err != nil {
        fmt.Println(err)
        return
    }
    defer r.Body.Close()
    d, err := ioutil.ReadAll(r.Body)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(base64.StdEncoding.EncodeToString(d))
}








Ada: with Ada.Text_IO;

with AWS.Response;
with AWS.Client;
with AWS.Translator;

procedure Encode_AWS is
   URL     : constant String := "http://rosettacode.org/favicon.ico";
   Page    : constant AWS.Response.Data := AWS.Client.Get (URL);
   Payload : constant String := AWS.Response.Message_Body (Page);
   Icon_64 : constant String := AWS.Translator.Base64_Encode (Payload);
begin
   Ada.Text_IO.Put_Line (Icon_64);
end Encode_AWS;
Task: Base64 encode data





Java: import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;

public class Base64 {

    private static final char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();

    static String base64(InputStream is) throws IOException {
        StringBuilder sb = new StringBuilder();
        int blocks = 0;

        while (true) {
            int c0 = is.read();
            if (c0 == -1)
                break;
            int c1 = is.read();
            int c2 = is.read();

            int block = ((c0 & 0xFF) << 16) | ((Math.max(c1, 0) & 0xFF) << 8) | (Math.max(c2, 0) & 0xFF);

            sb.append(alpha[block >> 18 & 63]);
            sb.append(alpha[block >> 12 & 63]);
            sb.append(c1 == -1 ? '=' : alpha[block >> 6 & 63]);
            sb.append(c2 == -1 ? '=' : alpha[block & 63]);

            if (++blocks == 19) {
                blocks = 0;
                sb.append('\n');
            }
        }

        if (blocks > 0)
            sb.append('\n');

        return sb.toString();
    }

    private static void assertBase64(String expected, byte[] bytes) throws IOException {
        String actual = base64(new ByteArrayInputStream(bytes));
        if (!actual.equals(expected)) {
            throw new IllegalStateException(String.format("Expected %s for %s, but got %s.",
                    expected, Arrays.toString(bytes), actual));
        }
    }

    private static void testBase64() throws IOException {
        assertBase64("", new byte[]{});
        assertBase64("AA==\n", new byte[]{0});
        assertBase64("AAA=\n", new byte[]{0, 0});
        assertBase64("AAAA\n", new byte[]{0, 0, 0});
        assertBase64("AAAAAA==\n", new byte[]{0, 0, 0, 0});
        assertBase64("/w==\n", new byte[]{-1});
        assertBase64("
        assertBase64("
        assertBase64("
    }

    public static void main(String[] args) throws IOException {
        testBase64();

        URLConnection conn = new URL("http:
        conn.addRequestProperty("User-Agent", "Mozilla"); 
        try (InputStream is = conn.getInputStream()) {
            System.out.println(base64(is));
        }
    }
}







Ada: with Ada.Text_IO;

with AWS.Response;
with AWS.Client;
with AWS.Translator;

procedure Encode_AWS is
   URL     : constant String := "http://rosettacode.org/favicon.ico";
   Page    : constant AWS.Response.Data := AWS.Client.Get (URL);
   Payload : constant String := AWS.Response.Message_Body (Page);
   Icon_64 : constant String := AWS.Translator.Base64_Encode (Payload);
begin
   Ada.Text_IO.Put_Line (Icon_64);
end Encode_AWS;
Task: Base64 encode data







Python: import urllib
import base64

data = urllib.urlopen('http://rosettacode.org/favicon.ico').read()
print base64.b64encode(data)





Ada: with Ada.Text_IO;

with AWS.Response;
with AWS.Client;
with AWS.Translator;

procedure Encode_AWS is
   URL     : constant String := "http://rosettacode.org/favicon.ico";
   Page    : constant AWS.Response.Data := AWS.Client.Get (URL);
   Payload : constant String := AWS.Response.Message_Body (Page);
   Icon_64 : constant String := AWS.Translator.Base64_Encode (Payload);
begin
   Ada.Text_IO.Put_Line (Icon_64);
end Encode_AWS;
Task: Base64 encode data

C: #include <stdio.h>
#include <stdlib.h>
#include <resolv.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

int main() {
  int fin = open("favicon.ico",  O_RDONLY);
  if (fin == -1)
    return 1;

  struct stat st;
  if (fstat(fin, &st))
    return 1;

  void *bi = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fin,  0);
  if (bi == MAP_FAILED)
    return 1;

  int outLength = ((st.st_size + 2) / 3) * 4 + 1;
  char *outBuffer = malloc(outLength);
  if (outBuffer == NULL)
    return 1;

  int encodedLength = b64_ntop(bi, st.st_size, outBuffer, outLength);
  if (encodedLength < 0)
    return 1;

  puts(outBuffer);

  free(outBuffer);
  munmap(bi, st.st_size);
  close(fin);

  return 0;
}
Task: Base64 encode data



C#: namespace RosettaCode.Base64EncodeData
{
    using System;
    using System.Net;

    internal static class Program
    {
        private static void Main()
        {
            const string path = "http:

            byte[] input;
            using (var client = new WebClient())
            {
                input = client.DownloadData(path);
            }

            var output = Convert.ToBase64String(input);
            Console.WriteLine(output);
        }
    }
}
Task: Base64 encode data


C++: #include <iostream>
#include <fstream>
#include <vector>

typedef unsigned char byte;
using namespace std;

const unsigned m1 = 63 << 18, m2 = 63 << 12, m3 = 63 << 6;

class base64
{
public:
    base64() { char_set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; }
    string encode( vector<byte> v )
    {
	string res;
	unsigned d, a = 0, l = static_cast<unsigned>( v.size() );
	while( l > 2 )
	{
	    d = v[a++] << 16 | v[a++] << 8 | v[a++];	
	    res.append( 1, char_set.at( ( d & m1 ) >> 18 ) );	
	    res.append( 1, char_set.at( ( d & m2 ) >> 12 ) );	
	    res.append( 1, char_set.at( ( d & m3 ) >>  6 ) );
	    res.append( 1, char_set.at( d & 63 ) );
	    l -= 3;
	}
	if( l == 2 )
	{
	    d = v[a++] << 16 | v[a++] << 8;
	    res.append( 1, char_set.at( ( d & m1 ) >> 18 ) );	
	    res.append( 1, char_set.at( ( d & m2 ) >> 12 ) );	
	    res.append( 1, char_set.at( ( d & m3 ) >>  6 ) );
	    res.append( 1, '=' );
	}
	else if( l == 1 )
	{
	    d = v[a++] << 16;
	    res.append( 1, char_set.at( ( d & m1 ) >> 18 ) );	
	    res.append( 1, char_set.at( ( d & m2 ) >> 12 ) );	
	    res.append( "==", 2 );
	}
	return res;
    }

private:
    string char_set;
};

int main( int argc, char* argv[] )
{
    base64 b;
    basic_ifstream<byte> f( "favicon.ico", ios::binary );
    string r = b.encode( vector<byte>( ( istreambuf_iterator<byte>( f ) ), istreambuf_iterator<byte>() ) );
    copy( r.begin(), r.end(), ostream_iterator<char>( cout ) );
    return 0;
}
Task: Base64 encode data





Java: import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;

public class Base64 {

    private static final char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();

    static String base64(InputStream is) throws IOException {
        StringBuilder sb = new StringBuilder();
        int blocks = 0;

        while (true) {
            int c0 = is.read();
            if (c0 == -1)
                break;
            int c1 = is.read();
            int c2 = is.read();

            int block = ((c0 & 0xFF) << 16) | ((Math.max(c1, 0) & 0xFF) << 8) | (Math.max(c2, 0) & 0xFF);

            sb.append(alpha[block >> 18 & 63]);
            sb.append(alpha[block >> 12 & 63]);
            sb.append(c1 == -1 ? '=' : alpha[block >> 6 & 63]);
            sb.append(c2 == -1 ? '=' : alpha[block & 63]);

            if (++blocks == 19) {
                blocks = 0;
                sb.append('\n');
            }
        }

        if (blocks > 0)
            sb.append('\n');

        return sb.toString();
    }

    private static void assertBase64(String expected, byte[] bytes) throws IOException {
        String actual = base64(new ByteArrayInputStream(bytes));
        if (!actual.equals(expected)) {
            throw new IllegalStateException(String.format("Expected %s for %s, but got %s.",
                    expected, Arrays.toString(bytes), actual));
        }
    }

    private static void testBase64() throws IOException {
        assertBase64("", new byte[]{});
        assertBase64("AA==\n", new byte[]{0});
        assertBase64("AAA=\n", new byte[]{0, 0});
        assertBase64("AAAA\n", new byte[]{0, 0, 0});
        assertBase64("AAAAAA==\n", new byte[]{0, 0, 0, 0});
        assertBase64("/w==\n", new byte[]{-1});
        assertBase64("
        assertBase64("
        assertBase64("
    }

    public static void main(String[] args) throws IOException {
        testBase64();

        URLConnection conn = new URL("http:
        conn.addRequestProperty("User-Agent", "Mozilla"); 
        try (InputStream is = conn.getInputStream()) {
            System.out.println(base64(is));
        }
    }
}
Task: Base64 encode data







Python: import urllib
import base64

data = urllib.urlopen('http://rosettacode.org/favicon.ico').read()
print base64.b64encode(data)