Categories
discuss

Effect of frequent sdcard writes

In my chat app, I am adding the ability to log chats. The logs are saved on the sdcard and one BufferedWriter is kept open for every person/channel chat is done with. I’m wondering what effects this might have have on the sdcard and its life.

My BufferedWriter‘s buffer size is to 1024, I’m also wondering if that is too small or too big.

Answer

Flash memory cards have an endurance of about a million write cycles per area and probably include wear levelling which basically means trying to write to different areas so specific spots dont wear out.

So after about (sizeOfSDCard/sizeOfYourData)*1,000,000 writes, you will have ruined their card.

Practically, this means you probably aren’t going to cause any harm.

Categories
discuss

BindException with INTERNET permission requested

I have seen several questions regarding SocketException when using Android, but none of them cover the BindException that I get even with the INTERNET permission specified in my manifest.

Here is part of my manifest:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

<uses-permission android:name="android.permission.READ_OWNER_DATA"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

<uses-permission android:name="android.permission.ACCOUNT_MANAGER"></uses-permission>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"></uses-permission>

Here is the relevant portion of my LogCat output:

04-22 14:49:06.117: DEBUG/MyLibrary(4844): Address to bind: 192.168.1.14 port: 843
04-22 14:49:06.197: WARN/System.err(4844): java.net.BindException: Permission denied (maybe missing INTERNET permission)
04-22 14:49:06.207: WARN/System.err(4844):     at org.apache.harmony.luni.platform.OSNetworkSystem.socketBindImpl(Native Method)
04-22 14:49:06.207: WARN/System.err(4844):     at org.apache.harmony.luni.platform.OSNetworkSystem.bind(OSNetworkSystem.java:107)
04-22 14:49:06.217: WARN/System.err(4844):     at org.apache.harmony.luni.net.PlainSocketImpl.bind(PlainSocketImpl.java:184)
04-22 14:49:06.217: WARN/System.err(4844):     at java.net.ServerSocket.bind(ServerSocket.java:414)
04-22 14:49:06.227: WARN/System.err(4844):     at org.apache.harmony.nio.internal.ServerSocketChannelImpl$ServerSocketAdapter.bind(ServerSocketChannelImpl.java:213)
04-22 14:49:06.227: WARN/System.err(4844):     at java.net.ServerSocket.bind(ServerSocket.java:367)
04-22 14:49:06.237: WARN/System.err(4844):     at org.apache.harmony.nio.internal.ServerSocketChannelImpl$ServerSocketAdapter.bind(ServerSocketChannelImpl.java:283)
04-22 14:49:06.237: WARN/System.err(4844):     at mylibrary.net.PolicyConnection$PolicyServerWorker.(PolicyConnection.java:201)

I Really hope this is a simple problem and not something complicated by the fact that the binding is occurring within a worker thread on a port less than 1024.

Update

Looks as if this is a privileged port issue, anyone know how to bind to ports lower than 1024 in Android?

    SelectorProvider provider = SelectorProvider.provider();
    try {
        ServerSocketChannel channel = provider.openServerSocketChannel();
        policySocket = channel.socket();
        Log.d("MyLibrary", "Address to bind: " + device.getAddress().getAddress() + " port: 843");              
        InetSocketAddress addr = new InetSocketAddress(InetAddress.getByName(device.getAddress().getAddress()), 843);
        policySocket.bind(addr);
        policySocket.setReuseAddress(true);
        policySocket.setReceiveBufferSize(256);
    } catch (Exception e) {
        e.printStackTrace();
    }

Answer

anyone know how to bind to ports lower than 1024 in Android?

Either root your phone, modify the firmware, or don’t bind to ports lower than 1024. That’s a Linux thing more than an Android thing.

Categories
discuss

How does volatile actually work?

Marking a variable as volatile in Java ensures that every thread sees the value that was last written to it instead of some stale value. I was wondering how this is actually achieved. Does the JVM emit special instructions that flush the CPU cashes or something?

Answer

From what I understand it always appears as if the cache has been flushed after write, and always appears as if reads are conducted straight from memory on read. The effect is that a Thread will always see the results of writes from another Thread and (according to the Java Memory Model) never a cached value. The actual implementation and CPU instructions will vary from one architecture to another however.

It doesn’t guarantee correctness if you increment the variable in more than one thread, or check its value and take some action since obviously there is no actual synchronization. You can generally only guarantee correct execution if there is only just Thread writing to the variable and others are all reading.

Also note that a 64 bit NON-volatile variable can be read/written as two 32 bit variables, so the 32 bit variables are atomic on write but the 64 bit ones aren’t. One half can be written before another – so the value read could be nether the old or the new value.

This is quite a helpful page from my bookmarks:

http://www.cs.umd.edu/~pugh/java/memoryModel/

Categories
discuss

Can I move another program’s window to the front of focus?

I am interacting with a third party application using it’s API, and would like to move it to the front of focus (so that it is on top of all other open windows) when a user performs a certain action. While I can move my application up and down in the hierarchy pretty easily, there doesn’t appear to be a way to interact with other windows. Is it possible to move another program’s window to front with Java?

Answer

You can’t do it in pure Java code, but you could using JNI. See In Java Swing how do you get a Win32 window handle (hwnd) reference to a window? for how to get a handle to the window. Then you could do something like http://msdn.microsoft.com/en-us/library/ms633545 to move it to the front.

Caveat: This is for windows only

Categories
discuss

Android ADT Eclipse plugin, parseSDKContent failed

I’ve just set up my first Android development environment consisting of

  • Eclipse 3.5
  • Mac OSX 10.5
  • Android SDK for x86 macs
  • ADT Eclipse plugin 0.9.6

I’ve set set $PATH to my SDK/tools directory (which shouldn’t matter if I only use Eclipse right?) and started Eclipse, but when I try to set the path to the SDK in Eclipse, i get the error “parseSdkContent failed”. The stack trace of from the thrown exception is

java.lang.IllegalArgumentException: http://www.w3.org/2001/XMLSchema
at javax.xml.validation.SchemaFactory.newInstance(SchemaFactory.java:181)
at com.android.ide.eclipse.adt.internal.sdk.LayoutDevicesXsd.getValidator(Unknown Source)
at com.android.ide.eclipse.adt.internal.sdk.LayoutDeviceManager.parseLayoutDevices(Unknown Source)
at com.android.ide.eclipse.adt.internal.sdk.LayoutDeviceManager.loadDefaultLayoutDevices(Unknown Source)
at com.android.ide.eclipse.adt.internal.sdk.LayoutDeviceManager.loadDefaultAndUserDevices(Unknown Source)
at com.android.ide.eclipse.adt.internal.sdk.Sdk.<init>(Unknown Source)
at com.android.ide.eclipse.adt.internal.sdk.Sdk.loadSdk(Unknown Source)
at com.android.ide.eclipse.adt.AdtPlugin$13.run(Unknown Source)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)

I can’t see that I’ve missed anything in the setup process, according to the instructions it should basically just work out of the box. Any ideas as to why this might fail?

Answer

A quick glance at http://java.sun.com/javase/6/docs/api/javax/xml/validation/SchemaFactory.html suggests that something is wrong with the JDK 1.6 setup.

Setting the -vm flag in eclipse to the original 1.5 did not help, but after removing all traces of the apparently misconfigured 1.6 everything worked like a charm.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..