Equivalent function for url.openStream() in Golang - url

import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.oracle.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
I need to do the same in golang i.e read and print the html source code, but cannot find the relation between two, I am a beginner with Go language, thank you in advance

Hope this will help:
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func Error(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
response, err := http.Get("http://www.oracle.com/")
Error(err)
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
Error(err)
fmt.Printf("%s\n", contents)
}
For more details: https://golang.org/pkg/net/http/

Related

How to successfully perform file I/O with asynchronous input in Dart using Streams

I was wondering if anyone knew how to correctly perform asynchronous file I/O in Dart using Streams, because clearly my method is not working. I have seen other people's solutions by converting the stream to a list, but I don't really want to do that because of speed concerns and was wondering if there was any way to do this operation using just streams?
Anyways, here is my code:
import 'dart:io';
import 'dart:collection';
import 'dart:convert';
final validCharacters = RegExp(r'^[a-zA-Z]+$'); // a-z, A-Z
main() async {
final dict1File = 'american-english';
final dict2File = 'british-english';
final cleanedDictFile = 'Cleansed_English_Dictionary';
SplayTreeSet<String> wordSet = new SplayTreeSet<String>();
await readFile(dict1File, wordSet);
await readFile(dict2File, wordSet);
await writeFile(cleanedDictFile, wordSet);
}
readFile(String fileName, SplayTreeSet<String> wordSet) async {
File file = new File(fileName);
if (await file.exists()) {
file
.openRead()
.transform(Utf8Decoder())
.transform(LineSplitter())
.where((String data) => validCharacters.hasMatch(data))
.listen((String data) => wordSet.add(data),
onDone: () => print('${wordSet.length}'), onError: (e) => print(e));
} else {
print('The desired file $fileName does not exist');
}
}
writeFile(String fileName, SplayTreeSet<String> wordSet) async {
File file = new File(fileName);
if (await file.exists()) {
file.openWrite().writeAll(wordSet, '\n');
} else {
print('The desired file $fileName does not exist');
}
}
Here is me performing a similar operation in Java (my native language) :p
import java.io.*;
import java.util.Set;
import java.util.TreeSet;
public class DictCleaner {
public static void main(String[] args) throws IOException {
System.out.println("Beginning process");
Set<String> words = new TreeSet<>();
long time1 = System.currentTimeMillis();
readFile("american-english", words);
readFile("british-english", words);
long time2 = System.currentTimeMillis();
System.out.println(
"Time to read files: " + (time2 - time1) + " milliseconds"
);
System.out.println("Number of words: " + words.size());
String filename = "Cleansed_English_Dictionary";
System.out.println("Outputting new file, " + filename);
outputFile(filename, words);
long time3 = System.currentTimeMillis();
System.out.println(
"Total execution time: " + (time3 - time1) + " milliseconds"
);
File file = new File(filename);
double fileSize = file.length() >> 10;
System.out.println(
"Output file size of " + filename + "= " + fileSize + " kilobytes"
);
}
public static void readFile(String fileName, Set<String> words)
throws IOException {
BufferedReader br;
try {
br = new BufferedReader(new FileReader(fileName));
String currentLine;
while ((currentLine = br.readLine()) != null) {
if (currentLine.matches("^[a-zA-Z]") && currentLine.length() <= 15) {
words.add(currentLine);
}
}
br.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public static void outputFile(String filename, Set<String> words)
throws FileNotFoundException {
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(filename)
);
try {
for (String s : words) {
bos.write(s.getBytes());
bos.write(System.lineSeparator().getBytes());
}
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Anyways, I am basically just cleaning up a dictionary for use with my app. Does anyone have any guidance on how I can make the Dart code work, or perhaps a more generic explanation?
My new code after comments!
import 'dart:io';
import 'dart:collection';
import 'dart:convert';
final validCharacters = RegExp(r'^[a-zA-Z]+$'); // a-z, A-Z
main() async {
Stopwatch stopwatch = new Stopwatch()..start();
final dict1File = 'american-english';
final dict2File = 'british-english';
final cleanedDictFile = 'Cleansed_English_Dictionary';
SplayTreeSet<String> wordSet = new SplayTreeSet<String>();
await readFile(dict1File, wordSet);
await readFile(dict2File, wordSet);
await writeFile(cleanedDictFile, wordSet);
print('Total execution time: ${stopwatch.elapsed}');
}
Future<void> readFile(String fileName, SplayTreeSet<String> wordSet) async {
File file = new File(fileName);
if (await file.exists()) {
await for (final data in file
.openRead()
.transform(Utf8Decoder())
.transform(LineSplitter())
.where((String data) => validCharacters.hasMatch(data))) {
wordSet.add(data);
}
print('${wordSet.length}');
} else {
print('The desired file $fileName does not exist');
}
}
Future<void> writeFile(String fileName, SplayTreeSet<String> wordSet) async {
File file = new File(fileName);
if (await file.exists()) {
IOSink sink = file.openWrite();
sink.writeAll(wordSet, '\n');
sink.close();
} else {
print('The desired file $fileName does not exist');
}
}
The problem is that .listen does not halt the program until all events has been consumed.
I think the most clean way to fix this is to use "await for-each" like this:
Future<void> readFile(String fileName, SplayTreeSet<String> wordSet) async {
File file = new File(fileName);
if (await file.exists()) {
await for (final data in file
.openRead()
.transform(Utf8Decoder())
.transform(LineSplitter())
.where((String data) => validCharacters.hasMatch(data))) {
wordSet.add(data);
}
print('${wordSet.length}');
} else {
print('The desired file $fileName does not exist');
}
}
More optimized solution
As requested, I have tried to make the program run faster. If performance are the main goal, you can often save some time by using the "sync" edition of the IO file operations. In this case, the program are really not doing anything else, so it is fine to let the main thread wait on e.g. reading the file.
One optimization is more about code readability but Dart does already provide readAsLines and readAsLinesSync on File so we don't need to use a Utf8Decoder and LineSplitter if we just want to split the lines. This will potentially take some more memory since we now get a list containing all lines instead of iterating each line. The list are short-lived so it is properly not a problem for this program. But also, the list contains String objects where most of them are going to be used in our Set so most of the used memory is not really something we can avoid.
Another optimization is the writing. Here we generate the final string first in memory (wordSet.join('\n')) and then write the whole string to disk as one operation. This is much faster but does take some additional memory.
import 'dart:collection';
import 'dart:io';
final validCharacters = RegExp(r'^[a-zA-Z]+$'); // a-z, A-Z
void main() {
const dict1File = 'american-english-insane';
const dict2File = 'british-english-insane';
const cleanedDictFile = 'Cleansed_English_Dictionary';
final wordSet = SplayTreeSet<String>();
readFile(dict1File, wordSet);
readFile(dict2File, wordSet);
writeFile(cleanedDictFile, wordSet);
}
void readFile(String fileName, Set<String> wordSet) {
final file = File(fileName);
if (file.existsSync()) {
file
.readAsLinesSync()
.where((data) => validCharacters.hasMatch(data))
.forEach(wordSet.add);
print(wordSet.length);
} else {
print('The desired file $fileName does not exist');
}
}
void writeFile(String fileName, Iterable<String> wordSet) =>
File(fileName).writeAsStringSync(wordSet.join('\n'));

Convert relative to absolute URLs in Go

I'm writing a little web crawler, and a lot of the links on sites I'm crawling are relative (so they're /robots.txt, for example). How do I convert these relative URLs to absolute URLs (so /robots.txt => http://google.com/robots.txt)? Does Go have a built-in way to do this?
Yes, the standard library can do this with the net/url package. Example (from the standard library):
package main
import (
"fmt"
"log"
"net/url"
)
func main() {
u, err := url.Parse("../../..//search?q=dotnet")
if err != nil {
log.Fatal(err)
}
base, err := url.Parse("http://example.com/directory/")
if err != nil {
log.Fatal(err)
}
fmt.Println(base.ResolveReference(u))
}
Notice that you only need to parse the absolute URL once and then you can reuse it over and over.
On top of #Not_a_Golfer's solution.
You can also use base URL's Parse method to provide a relative or absolute URL.
package main
import (
"fmt"
"log"
"net/url"
)
func main() {
// parse only base url
base, err := url.Parse("http://example.com/directory/")
if err != nil {
log.Fatal(err)
}
// and then use it to parse relative URLs
u, err := base.Parse("../../..//search?q=dotnet")
if err != nil {
log.Fatal(err)
}
fmt.Println(u.String())
}
Try it on Go Playground.
I think you are looking for ResolveReference method.
import (
"fmt"
"log"
"net/url"
)
func main() {
u, err := url.Parse("../../..//search?q=dotnet")
if err != nil {
log.Fatal(err)
}
base, err := url.Parse("http://example.com/directory/")
if err != nil {
log.Fatal(err)
}
fmt.Println(base.ResolveReference(u))
}
// gives: http://example.com/search?q=dotnet
I use it for my crawler as well and works like a charm!

I am getting no audio for Watson TTS

This is my java code:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.ibm.watson.developer_cloud.text_to_speech.v1.TextToSpeech;
import com.ibm.watson.developer_cloud.text_to_speech.v1.model.AudioFormat;
import com.ibm.watson.developer_cloud.text_to_speech.v1.model.Voice;
public static void main(String[] args) {
TextToSpeech textService = new TextToSpeech(IBM_WATSON_USERNAME, IBM_WATSON_PASSWORD);
String text = "Show me the meaning of being lonely";
try {
InputStream in = textService.synthesize(text, Voice.EN_ALLISON, AudioFormat.WAV)
.execute();
System.out.println(in.available());
byte[] buffer = new byte[in.available()];
in.read(buffer);
File targetFile = new File("local_path/Aud.wav");
OutputStream outStream = new FileOutputStream(targetFile);
outStream.write(buffer);
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
When i run the code i am getting the response in Eclipse Console as:
Dec 13, 2017 5:38:35 PM okhttp3.internal.platform.Platform log
INFO: --> POST https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?voice=en-US_AllisonVoice&accept=audio/wav http/1.1 (71-byte body)
Dec 13, 2017 5:38:35 PM okhttp3.internal.platform.Platform log
INFO: <-- 200 OK https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?voice=en-US_AllisonVoice&accept=audio/wav (501ms, unknown-length body)
Value of in.available() is 0
I am getting no audio generated. So as per the code flow i am getting a .wav file of 0kB getting generated. What am i getting no Audio?
Try this simple method
public class tts
{
public static void main( String[] args ) throws Exception
{
TextToSpeech service = new TextToSpeech();
service.setUsernameAndPassword();
String text = "Show me the meaning of being lonely";
InputStream stream =service.synthesize(text, Voice.EN_ALLISON, AudioFormat.WAV).execute();
AudioPlayer.player.start(WaveUtils.reWriteWaveHeader(stream));
}}
catch (Exception e) {
e.printStackTrace();
}
}
}

Get Image from client server & display on screen - blackberry java

I am trying to show image on screen by using client server, but I got exception
Protocol not found: net.rim.device.cldc.io.ftp.Protocol" , java.lang.IllegalArgumentException.
Here I have post the code where I get the exception(Currently on app I successfully login with client server, show folders & directories, now I want to click on any file it open on new screen.)
package com.rim.samples.device.mapactiondemo;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.container.MainScreen;
public class ShowData extends MainScreen {
String connParams;
public ShowData() {
// Check Type of connection
CheckConnection obj1 = new CheckConnection();
connParams = obj1.getConnParam();
Bitmap listThumb;
String path = "ftp://dice:pAssw0rd#64.207.149.236:21/images/facebook.png"
+ connParams + "";
listThumb = getImage.getImageFromUrl(path);
BitmapField bitmapField1 = new BitmapField(listThumb);
add(bitmapField1);
}
}
getImage.java
package com.rim.samples.device.mapactiondemo;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
import java.io.IOException;
import java.io.InputStream;
import java.lang.String;
import net.rim.device.api.system.Bitmap;
public final class getImage {
/**
* Fetches the content on the speicifed url. The url of the content to fetch
*/
public static Bitmap getImageFromUrl(String url) {
Bitmap bitmap = null;
try {
String bitmapData = getDataFromUrl(url);
bitmap = Bitmap.createBitmapFromBytes(bitmapData.getBytes(), 0,
bitmapData.length(), 1);
// Image.createImage(imageData.getBytes(), 0,imageData.length());
} catch (Exception e1) {
e1.printStackTrace();
System.out.println(e1);
}
return bitmap;
}
/**
* Fetches the content on the speicifed url. The url of the content to fetch
*/
private static String getDataFromUrl(String url) {
StringBuffer b = new StringBuffer();
InputStream is = null;
SocketConnection c = null;
long len = 0;
int ch = 0;
try {
c = (SocketConnection) Connector.open(url);
c.setSocketOption(SocketConnection.LINGER, 5);
c.setSocketOption(SocketConnection.DELAY, 5);
is = c.openInputStream();
//len = is.getLength();
if (len != -1) {
// Read exactly Content-Length bytes
for (int i = 0; i < len; i++)
if ((ch = is.read()) != -1) {
b.append((char) ch);
}
} else {
// Read until the connection is closed.
while ((ch = is.read()) != -1) {
len = is.available();
b.append((char) ch);
}
}
is.close();
c.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b.toString();
}
}
As far as I know ftp protocol is not implemented in BlackBerry Java SDK. Use http protocol instead of ftp.

Reading from a url resource in Go

How do we read from a url resource. I have used the https://github.com/Kissaki/rest.go api in the following example. Below is the example I use to write a string to the url http://localhost:8080/cool
But now I need to retrieve the data from the url, how do I read it back?
package main
import (
"fmt"
"http"
"github.com/nathankerr/rest.go"
)
type FileString struct {
value string
}
func (t *FileString) Index(w http.ResponseWriter) {
fmt.Fprintf(w, "%v", t.value)
}
func main(){
rest.Resource("cool", &FileString{s:"this is file"})
http.ListenAndServe(":3000", nil)
}
if you just want to fetch a file over http you could do something like this i guess
resp, err := http.Get("http://your.url.com/whatever.html")
check(err) // does some error handling
// read from resp.Body which is a ReadCloser
response, err := http.Get(URL) //use package "net/http"
if err != nil {
fmt.Println(err)
return
}
defer response.Body.Close()
// Copy data from the response to standard output
n, err1 := io.Copy(os.Stdout, response.Body) //use package "io" and "os"
if err != nil {
fmt.Println(err1)
return
}
fmt.Println("Number of bytes copied to STDOUT:", n)

Resources