Update Favicon : Http Connection « Network « Android






Update Favicon

     

//package com.dreamcode.anfeedreader.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.content.Context;

class Main {
  public byte[] updateFavicon(Context ctx, String iconUrl)
      throws MalformedURLException {
    try {
      return loadBytesFromURL(ctx, new URL(iconUrl));
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return null;
  }

  public byte[] loadBytesFromURL(Context ctxU, URL url) throws IOException {
    byte[] b = null;
    URLConnection con = url.openConnection();
    int size = con.getContentLength();
    InputStream in = null;

    try {
      if ((in = con.getInputStream()) != null)
        b = (size != -1) ? loadBytesFromStreamForSize(in, size)
            : loadBytesFromStream(in);
    } finally {
      if (in != null)
        try {
          in.close();
        } catch (IOException ioe) {
        }
    }
    return b;
  }

  public byte[] loadBytesFromStream(InputStream in) throws IOException {
    return loadBytesFromStream(in, kDEFAULT_CHUNK_SIZE);
  }

  public byte[] loadBytesFromStreamForSize(InputStream in, int size)
      throws IOException {
    int count, index = 0;
    byte[] b = new byte[size];

    // read in the bytes from input stream
    while ((count = in.read(b, index, size)) > 0) {
      size -= count;
      index += count;
    }
    return b;
  }
  private static int kDEFAULT_CHUNK_SIZE = 256;
  public byte[] loadBytesFromStream(InputStream in, int chunkSize)
      throws IOException {
    if (chunkSize < 1)
      chunkSize = kDEFAULT_CHUNK_SIZE;

    int count;
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    byte[] b = new byte[chunkSize];
    try {
      while ((count = in.read(b, 0, chunkSize)) > 0)
        bo.write(b, 0, count);
      byte[] thebytes = bo.toByteArray();
      return thebytes;
    } finally {
      bo.close();
      bo = null;
    }
  }
}

   
    
    
    
    
  








Related examples in the same category

1.Http Connection
2.Using HttpGet to get the web response
3.Create Http connection
4.Http connection with
5.HttpGet and DefaultHttpClient
6.Http Post
7.Simple HTTP Request
8.Http Request Class
9.Http Get and Http Post
10.Get Text from HttpResponse
11.Http Request
12.Http Downloader
13.Is Http Url Available
14.Http Retriever
15.Receive Response from HttpURLConnection
16.Print http headers. Useful for debugging.
17.Return the base URL from the given URL. Example: http://foo.org/abc.html -> http://foo.org/
18.Send message with HttpPost
19.Get Http Stream
20.Generic Object Http Loader
21.Http Get and DefaultHttpClient
22.Gets http output from URL
23.Util for Http Get
24.do Http Get Request and return the status code
25.Http Get
26.implements HttpClient
27.Get File From HTTP
28.Make all redirects to go to http in stead of https
29.New Http Client Manager
30.Http Client Manager
31.Multipart Post
32.Get Server Data
33.Yahoo News Crawler
34.Send Packet
35.Read a web page
36.parse Request Header
37.Data Send Utils
38.This class is in charge of synchronizing the events (with due dates) with Google Calendar
39.Converts key-value pair to appropriate signature for Facebook.