问:
MediaCodec decode && encode on-the-fly
I want convert some audio tracks from video files from AAC 5.1 (not only but for starting) to AAC 2 (just because ac3 not supported by MediaMuxer and vorbis and opus encoders has not hardware support in Pixel 2) and use callbacks for MediaCodec. I wrote code like that:

mExtractor = new MediaExtractor();
mExtractor.setDataSource(source.getPath());
mExtractor.selectTrack(trackNumber);

MediaFormat sourceMf = mExtractor.getTrackFormat(trackNumber);
mDecoder = MediaCodec.createDecoderByType(sourceMf.getString(MediaFormat.KEY_MIME));
mDecoder.setCallback(createCallbackDecoder());
mDecoder.configure(sourceMf, null, null, 0);

MediaFormat wantedMediaFormat = MediaFormat.createAudioFormat(MediaFormat.MIMETYPE_AUDIO_AAC, 44100, 2);
mEncoder = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_AUDIO_AAC);
mEncoder.setCallback(createCallbackEncoder());
mEncoder.configure(wantedMediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);

mMuxer = new MediaMuxer(saveTo.getPath(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

public void start() {
        mDecoder.start();
```

<p>And callbacks</p>

<p>Decoder:</p>

<pre class="lang-java prettyprint-override"><code>@Override
public void onInputBufferAvailable(@NonNull MediaCodec codec, int index) {
    ByteBuffer byteBuffer = codec.getInputBuffer(index);
    Log.i(TAG, "onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): " + byteBuffer);
    if (byteBuffer != null) {
        int offset = 0;
        long presentationTimeUs = 0;
        int flags;
        int size;
        if ((size = mExtractor.readSampleData(byteBuffer, offset)) > -1) {
            presentationTimeUs = mExtractor.getSampleTime();
            flags = mExtractor.getSampleFlags();
            mExtractor.advance();
        } else {
            flags = MediaCodec.BUFFER_FLAG_END_OF_STREAM;
        }
        try {
            codec.queueInputBuffer(index, offset, size, presentationTimeUs, flags);
            Log.i(TAG, "onInputBufferAvailable (decoder): SUCCESS");
        } catch (Exception e) {
            Log.e(TAG, "EXCEPTION (decoder)!\nonInputBufferAvailable (decoder): ", e);
            throw e;
        }
    } else {
        Log.e(TAG, "onInputBufferAvailable = null");
    }
}

@Override
public void onOutputBufferAvailable(@NonNull MediaCodec codec, int index, @NonNull MediaCodec.BufferInfo info) {
    ByteBuffer byteBuffer = codec.getOutputBuffer(index);
    Log.i(TAG, "onOutputBufferAvailable: byteBuffer with data (decoder): " + byteBuffer);
    if (byteBuffer != null) {
        ByteBuffer buffer2 = ByteBuffer.allocate(info.size);
        Log.i(TAG, "onOutputBufferAvailable: allocated byteBuffer (decoder): " + buffer2);
        buffer2.put(byteBuffer);
        MediaCodec.BufferInfo info2 = new MediaCodec.BufferInfo();
        info2.flags = info.flags;
        info2.size = info.size;
        info2.presentationTimeUs = info.presentationTimeUs;
        info2.offset = info.offset;
        if (mQueue.add(new Pair<>(buffer2, info2))) {
            Log.i(TAG, String.format("onOutputBufferAvailable (decoder): added in queue: %s\n%s %s %s %s", buffer2,
                      info2.offset, info2.size, info2.presentationTimeUs, info2.flags));
            codec.releaseOutputBuffer(index, false);
        }
    } else {
        Log.e(TAG, "onOutputBufferAvailable = null");
    }
}

@Override
public void onOutputFormatChanged(@NonNull MediaCodec codec, @NonNull MediaFormat format) {
    Log.i(TAG, String.format("onOutputFormatChanged (decoder): OLD=%s NEW=%s", codec.getInputFormat(), format));
    mEncoder.start();
}
</code>```

Encoder:

```<code>@Override
public void onInputBufferAvailable(@NonNull MediaCodec codec, int index) {
    Log.i(TAG, "onInputBufferAvailable (encoder): index=" + index);
    Pair<ByteBuffer, MediaCodec.BufferInfo> mediaChunk;
    if ((mediaChunk = mQueue.poll()) != null) {
        Log.i(TAG, "onInputBufferAvailable (encoder): queue poll != null");
        ByteBuffer byteBuffer = codec.getInputBuffer(index);
        Log.i(TAG, "onInputBufferAvailable: byteBuffer b/f queue (encoder): " + byteBuffer);
        if (byteBuffer != null) {
            int offset = mediaChunk.second.offset;
            int flags = mediaChunk.second.flags;
            long presentationTimeUs = mediaChunk.second.presentationTimeUs;
            int size = mediaChunk.second.size;
            byteBuffer.put(mediaChunk.first);
            try {
                Log.i(TAG, String.format("onInputBufferAvailable (encoder): %s\n%s %s %s %s", mediaChunk.first,
                    mediaChunk.second.offset,
                    mediaChunk.second.size,
                    mediaChunk.second.presentationTimeUs,
                    mediaChunk.second.flags));
                codec.queueInputBuffer(index, offset, size, presentationTimeUs, flags);
                Log.i(TAG, "queueInputBuffer (encoder): SUCCESS");
            } catch (Exception e) {
                Log.e(TAG, "EXCEPTION (encoder)!\nonInputBufferAvailable (encoder): ", e);
                throw e;
            }
        }
    } else {
        Log.e(TAG, "onInputBufferAvailable (encoder): empty queue");
    }
}

@Override
public void onOutputBufferAvailable(@NonNull MediaCodec codec, int index, @NonNull MediaCodec.BufferInfo info) {
    ByteBuffer byteBuffer = codec.getOutputBuffer(index);
    Log.i(TAG, "onOutputBufferAvailable: byteBuffer from codec with data (encoder): " + byteBuffer);
    if (byteBuffer != null) {
        mMuxer.writeSampleData(mTrackNumber, byteBuffer, info);
        Log.i(TAG, "onOutputBufferAvailable (encoder): muxer written");
        if (info.flags == MediaCodec.BUFFER_FLAG_END_OF_STREAM) {
            finish();
        } else {
            codec.releaseOutputBuffer(index, false);
        }
    } else {
        Log.e(TAG, "onOutputBufferAvailable (encoder): buffer = null");
    }
}

@Override
public void onOutputFormatChanged(@NonNull MediaCodec codec, @NonNull MediaFormat format) {
    Log.i(TAG, String.format("onOutputFormatChanged (encoder): OLD=%s NEW=%s", codec.getInputFormat(), format));
    mTrackNumber = mMuxer.addTrack(format);
    mMuxer.start();
}
</code>```

But on executing I got exception:

```<code>I/MediaCodec: MediaCodec will operate in async mode
I/MediaCodec: MediaCodec will operate in async mode
I/Codec: onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): java.nio.DirectByteBuffer[pos=0 lim=8192 cap=8192]
I/Codec: onInputBufferAvailable (decoder): SUCCESS
I/Codec: onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): java.nio.DirectByteBuffer[pos=0 lim=8192 cap=8192]
I/Codec: onInputBufferAvailable (decoder): SUCCESS
I/Codec: onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): java.nio.DirectByteBuffer[pos=0 lim=8192 cap=8192]
I/Codec: onInputBufferAvailable (decoder): SUCCESS
I/Codec: onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): java.nio.DirectByteBuffer[pos=0 lim=8192 cap=8192]
I/Codec: onInputBufferAvailable (decoder): SUCCESS
I/Codec: onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): java.nio.DirectByteBuffer[pos=0 lim=8192 cap=8192]
I/Codec: onInputBufferAvailable (decoder): SUCCESS
I/Codec: onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): java.nio.DirectByteBuffer[pos=0 lim=8192 cap=8192]
I/Codec: onInputBufferAvailable (decoder): SUCCESS
I/Codec: onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): java.nio.DirectByteBuffer[pos=0 lim=8192 cap=8192]
I/Codec: onInputBufferAvailable (decoder): SUCCESS
I/Codec: onOutputFormatChanged (decoder): OLD={sample-rate=44100, mime=audio/mp4a-latm, channel-count=1, bitrate=0} NEW={sample-rate=48000, pcm-encoding=2, mime=audio/raw, channel-count=6}
I/Codec: onOutputBufferAvailable: byteBuffer with data (decoder): java.nio.DirectByteBuffer[pos=0 lim=0 cap=32768]
I/Codec: onOutputBufferAvailable: allocated byteBuffer (decoder): java.nio.HeapByteBuffer[pos=0 lim=0 cap=0]
I/Codec: onOutputBufferAvailable (decoder): added in queue: java.nio.HeapByteBuffer[pos=0 lim=0 cap=0]
    0 0 0 0
I/Codec: onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): java.nio.DirectByteBuffer[pos=0 lim=8192 cap=8192]
I/Codec: onInputBufferAvailable (decoder): SUCCESS
I/Codec: onOutputBufferAvailable: byteBuffer with data (decoder): java.nio.DirectByteBuffer[pos=0 lim=0 cap=32768]
I/Codec: onOutputBufferAvailable: allocated byteBuffer (decoder): java.nio.HeapByteBuffer[pos=0 lim=0 cap=0]
I/Codec: onOutputBufferAvailable (decoder): added in queue: java.nio.HeapByteBuffer[pos=0 lim=0 cap=0]
    0 0 21333 0
I/Codec: onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): java.nio.DirectByteBuffer[pos=0 lim=8192 cap=8192]
I/Codec: onInputBufferAvailable (decoder): SUCCESS
I/Codec: onOutputBufferAvailable: byteBuffer with data (decoder): java.nio.DirectByteBuffer[pos=0 lim=12288 cap=32768]
I/Codec: onOutputBufferAvailable: allocated byteBuffer (decoder): java.nio.HeapByteBuffer[pos=0 lim=12288 cap=12288]
I/Codec: onOutputBufferAvailable (decoder): added in queue: java.nio.HeapByteBuffer[pos=12288 lim=12288 cap=12288]
    0 12288 42666 0
I/Codec: onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): java.nio.DirectByteBuffer[pos=0 lim=8192 cap=8192]
I/Codec: onInputBufferAvailable (decoder): SUCCESS
I/Codec: onOutputBufferAvailable: byteBuffer with data (decoder): java.nio.DirectByteBuffer[pos=0 lim=12288 cap=32768]
I/Codec: onOutputBufferAvailable: allocated byteBuffer (decoder): java.nio.HeapByteBuffer[pos=0 lim=12288 cap=12288]
I/Codec: onOutputBufferAvailable (decoder): added in queue: java.nio.HeapByteBuffer[pos=12288 lim=12288 cap=12288]
    0 12288 64000 0
I/Codec: onInputBufferAvailable (encoder): index=0
I/Codec: onInputBufferAvailable (encoder): queue poll != null
I/Codec: onInputBufferAvailable: byteBuffer b/f queue (encoder): java.nio.DirectByteBuffer[pos=0 lim=4096 cap=4096]
I/Codec: onInputBufferAvailable (encoder): java.nio.HeapByteBuffer[pos=0 lim=0 cap=0]
    0 0 0 0
I/Codec: queueInputBuffer (encoder): SUCCESS
I/Codec: onInputBufferAvailable (encoder): index=1
I/Codec: onInputBufferAvailable (encoder): queue poll != null
I/Codec: onInputBufferAvailable: byteBuffer b/f queue (encoder): java.nio.DirectByteBuffer[pos=0 lim=4096 cap=4096]
I/Codec: onInputBufferAvailable (encoder): java.nio.HeapByteBuffer[pos=0 lim=0 cap=0]
    0 0 21333 0
I/Codec: queueInputBuffer (encoder): SUCCESS
I/Codec: onInputBufferAvailable (encoder): index=2
I/Codec: onInputBufferAvailable (encoder): queue poll != null
I/Codec: onInputBufferAvailable: byteBuffer b/f queue (encoder): java.nio.DirectByteBuffer[pos=0 lim=4096 cap=4096]
I/Codec: onInputBufferAvailable (encoder): java.nio.HeapByteBuffer[pos=12288 lim=12288 cap=12288]
    0 12288 42666 0
E/Codec: EXCEPTION (encoder)!
    onInputBufferAvailable (encoder):
    java.lang.IllegalArgumentException
        at android.media.MediaCodec.native_queueInputBuffer(Native Method)
        at android.media.MediaCodec.queueInputBuffer(MediaCodec.java:2450)
        at opensource.umnik.media2media.codec.SyncCodec$2.onInputBufferAvailable(SyncCodec.java:149)
        at android.media.MediaCodec$EventHandler.handleCallback(MediaCodec.java:1738)
        at android.media.MediaCodec$EventHandler.handleMessage(MediaCodec.java:1696)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
D/AndroidRuntime: Shutting down VM
</code>```

at android.media.MediaCodec.queueInputBuffer(MediaCodec.java:2450) ← this is encoder's buffer

What I did wrong?

UPD: code updated some times.
UPD2: more logs

答:
The cause of the IllegalArgumentException is ultimately that you have specified a size of 12288 bytes for an input ByteBuffer that only has a capacity of 4096 bytes.

Note that, for the first two calls to queueInputBuffer() (encoder), you are not putting in any data; they are both 0-byte writes, therefor they are trivial, and they succeed. (Personally, I'd just skip the 0-byte writes).

The 3rd iteration is the first time you actually have some data to encode. But your byteBuffer.put(mediaChunk.first) isn't actually doing anything, because mediaChunk.first is already "played out". It has 0 bytes remaining. You need to "rewind" it before you do the put():

mediaChunk.first.position(0)

Of course, now the problem is, you have 12288 bytes to write, but the destination can only fit 4096 bytes, so the put() will undoubtedly throw. You must write a smaller amount. This will avoid the IllegalArgumentException.

As I mentioned in a comment, your decoded audio contains 6 channels of 5.1 audio. (`1024 samples x 2 bytes per sample x 6 channels == 12288). Your encoder would like 2048 samples of audio (at 2 bytes per sample; I assume you have configured the encoder for 1 channel/2 bytes per sample). This may be a good time to decide how you'd like to do the "5.1 -> mono" audio conversion; that will help reduce the amount of decoded audio that needs to be copied into the encoder.

Title: MediaCodec decode && encode on-the-fly

Body:
I want convert some audio tracks from video files from AAC 5.1 (not only but for starting) to AAC 2 (just because ac3 not supported by MediaMuxer and vorbis and opus encoders has not hardware support in Pixel 2) and use callbacks for MediaCodec. I wrote code like that:

mExtractor = new MediaExtractor();
mExtractor.setDataSource(source.getPath());
mExtractor.selectTrack(trackNumber);

MediaFormat sourceMf = mExtractor.getTrackFormat(trackNumber);
mDecoder = MediaCodec.createDecoderByType(sourceMf.getString(MediaFormat.KEY_MIME));
mDecoder.setCallback(createCallbackDecoder());
mDecoder.configure(sourceMf, null, null, 0);

MediaFormat wantedMediaFormat = MediaFormat.createAudioFormat(MediaFormat.MIMETYPE_AUDIO_AAC, 44100, 2);
mEncoder = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_AUDIO_AAC);
mEncoder.setCallback(createCallbackEncoder());
mEncoder.configure(wantedMediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);

mMuxer = new MediaMuxer(saveTo.getPath(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

public void start() {
        mDecoder.start();

And callbacks

Decoder:

@Override
public void onInputBufferAvailable(@NonNull MediaCodec codec, int index) {
    ByteBuffer byteBuffer = codec.getInputBuffer(index);
    Log.i(TAG, "onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): " + byteBuffer);
    if (byteBuffer != null) {
        int offset = 0;
        long presentationTimeUs = 0;
        int flags;
        int size;
        if ((size = mExtractor.readSampleData(byteBuffer, offset)) > -1) {
            presentationTimeUs = mExtractor.getSampleTime();
            flags = mExtractor.getSampleFlags();
            mExtractor.advance();
        } else {
            flags = MediaCodec.BUFFER_FLAG_END_OF_STREAM;
        }
        try {
            codec.queueInputBuffer(index, offset, size, presentationTimeUs, flags);
            Log.i(TAG, "onInputBufferAvailable (decoder): SUCCESS");
        } catch (Exception e) {
            Log.e(TAG, "EXCEPTION (decoder)!\nonInputBufferAvailable (decoder): ", e);
            throw e;
        }
    } else {
        Log.e(TAG, "onInputBufferAvailable = null");
    }
}

@Override
public void onOutputBufferAvailable(@NonNull MediaCodec codec, int index, @NonNull MediaCodec.BufferInfo info) {
    ByteBuffer byteBuffer = codec.getOutputBuffer(index);
    Log.i(TAG, "onOutputBufferAvailable: byteBuffer with data (decoder): " + byteBuffer);
    if (byteBuffer != null) {
        ByteBuffer buffer2 = ByteBuffer.allocate(info.size);
        Log.i(TAG, "onOutputBufferAvailable: allocated byteBuffer (decoder): " + buffer2);
        buffer2.put(byteBuffer);
        MediaCodec.BufferInfo info2 = new MediaCodec.BufferInfo();
        info2.flags = info.flags;
        info2.size = info.size;
        info2.presentationTimeUs = info.presentationTimeUs;
        info2.offset = info.offset;
        if (mQueue.add(new Pair<>(buffer2, info2))) {
            Log.i(TAG, String.format("onOutputBufferAvailable (decoder): added in queue: %s\n%s %s %s %s", buffer2,
                      info2.offset, info2.size, info2.presentationTimeUs, info2.flags));
            codec.releaseOutputBuffer(index, false);
        }
    } else {
        Log.e(TAG, "onOutputBufferAvailable = null");
    }
}

@Override
public void onOutputFormatChanged(@NonNull MediaCodec codec, @NonNull MediaFormat format) {
    Log.i(TAG, String.format("onOutputFormatChanged (decoder): OLD=%s NEW=%s", codec.getInputFormat(), format));
    mEncoder.start();
}

Encoder:

@Override
public void onInputBufferAvailable(@NonNull MediaCodec codec, int index) {
    Log.i(TAG, "onInputBufferAvailable (encoder): index=" + index);
    Pair<ByteBuffer, MediaCodec.BufferInfo> mediaChunk;
    if ((mediaChunk = mQueue.poll()) != null) {
        Log.i(TAG, "onInputBufferAvailable (encoder): queue poll != null");
        ByteBuffer byteBuffer = codec.getInputBuffer(index);
        Log.i(TAG, "onInputBufferAvailable: byteBuffer b/f queue (encoder): " + byteBuffer);
        if (byteBuffer != null) {
            int offset = mediaChunk.second.offset;
            int flags = mediaChunk.second.flags;
            long presentationTimeUs = mediaChunk.second.presentationTimeUs;
            int size = mediaChunk.second.size;
            byteBuffer.put(mediaChunk.first);
            try {
                Log.i(TAG, String.format("onInputBufferAvailable (encoder): %s\n%s %s %s %s", mediaChunk.first,
                    mediaChunk.second.offset,
                    mediaChunk.second.size,
                    mediaChunk.second.presentationTimeUs,
                    mediaChunk.second.flags));
                codec.queueInputBuffer(index, offset, size, presentationTimeUs, flags);
                Log.i(TAG, "queueInputBuffer (encoder): SUCCESS");
            } catch (Exception e) {
                Log.e(TAG, "EXCEPTION (encoder)!\nonInputBufferAvailable (encoder): ", e);
                throw e;
            }
        }
    } else {
        Log.e(TAG, "onInputBufferAvailable (encoder): empty queue");
    }
}

@Override
public void onOutputBufferAvailable(@NonNull MediaCodec codec, int index, @NonNull MediaCodec.BufferInfo info) {
    ByteBuffer byteBuffer = codec.getOutputBuffer(index);
    Log.i(TAG, "onOutputBufferAvailable: byteBuffer from codec with data (encoder): " + byteBuffer);
    if (byteBuffer != null) {
        mMuxer.writeSampleData(mTrackNumber, byteBuffer, info);
        Log.i(TAG, "onOutputBufferAvailable (encoder): muxer written");
        if (info.flags == MediaCodec.BUFFER_FLAG_END_OF_STREAM) {
            finish();
        } else {
            codec.releaseOutputBuffer(index, false);
        }
    } else {
        Log.e(TAG, "onOutputBufferAvailable (encoder): buffer = null");
    }
}

@Override
public void onOutputFormatChanged(@NonNull MediaCodec codec, @NonNull MediaFormat format) {
    Log.i(TAG, String.format("onOutputFormatChanged (encoder): OLD=%s NEW=%s", codec.getInputFormat(), format));
    mTrackNumber = mMuxer.addTrack(format);
    mMuxer.start();
}

But on executing I got exception:

I/MediaCodec: MediaCodec will operate in async mode
I/MediaCodec: MediaCodec will operate in async mode
I/Codec: onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): java.nio.DirectByteBuffer[pos=0 lim=8192 cap=8192]
I/Codec: onInputBufferAvailable (decoder): SUCCESS
I/Codec: onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): java.nio.DirectByteBuffer[pos=0 lim=8192 cap=8192]
I/Codec: onInputBufferAvailable (decoder): SUCCESS
I/Codec: onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): java.nio.DirectByteBuffer[pos=0 lim=8192 cap=8192]
I/Codec: onInputBufferAvailable (decoder): SUCCESS
I/Codec: onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): java.nio.DirectByteBuffer[pos=0 lim=8192 cap=8192]
I/Codec: onInputBufferAvailable (decoder): SUCCESS
I/Codec: onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): java.nio.DirectByteBuffer[pos=0 lim=8192 cap=8192]
I/Codec: onInputBufferAvailable (decoder): SUCCESS
I/Codec: onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): java.nio.DirectByteBuffer[pos=0 lim=8192 cap=8192]
I/Codec: onInputBufferAvailable (decoder): SUCCESS
I/Codec: onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): java.nio.DirectByteBuffer[pos=0 lim=8192 cap=8192]
I/Codec: onInputBufferAvailable (decoder): SUCCESS
I/Codec: onOutputFormatChanged (decoder): OLD={sample-rate=44100, mime=audio/mp4a-latm, channel-count=1, bitrate=0} NEW={sample-rate=48000, pcm-encoding=2, mime=audio/raw, channel-count=6}
I/Codec: onOutputBufferAvailable: byteBuffer with data (decoder): java.nio.DirectByteBuffer[pos=0 lim=0 cap=32768]
I/Codec: onOutputBufferAvailable: allocated byteBuffer (decoder): java.nio.HeapByteBuffer[pos=0 lim=0 cap=0]
I/Codec: onOutputBufferAvailable (decoder): added in queue: java.nio.HeapByteBuffer[pos=0 lim=0 cap=0]
    0 0 0 0
I/Codec: onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): java.nio.DirectByteBuffer[pos=0 lim=8192 cap=8192]
I/Codec: onInputBufferAvailable (decoder): SUCCESS
I/Codec: onOutputBufferAvailable: byteBuffer with data (decoder): java.nio.DirectByteBuffer[pos=0 lim=0 cap=32768]
I/Codec: onOutputBufferAvailable: allocated byteBuffer (decoder): java.nio.HeapByteBuffer[pos=0 lim=0 cap=0]
I/Codec: onOutputBufferAvailable (decoder): added in queue: java.nio.HeapByteBuffer[pos=0 lim=0 cap=0]
    0 0 21333 0
I/Codec: onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): java.nio.DirectByteBuffer[pos=0 lim=8192 cap=8192]
I/Codec: onInputBufferAvailable (decoder): SUCCESS
I/Codec: onOutputBufferAvailable: byteBuffer with data (decoder): java.nio.DirectByteBuffer[pos=0 lim=12288 cap=32768]
I/Codec: onOutputBufferAvailable: allocated byteBuffer (decoder): java.nio.HeapByteBuffer[pos=0 lim=12288 cap=12288]
I/Codec: onOutputBufferAvailable (decoder): added in queue: java.nio.HeapByteBuffer[pos=12288 lim=12288 cap=12288]
    0 12288 42666 0
I/Codec: onInputBufferAvailable: byteBuffer b/f readSampleData (decoder): java.nio.DirectByteBuffer[pos=0 lim=8192 cap=8192]
I/Codec: onInputBufferAvailable (decoder): SUCCESS
I/Codec: onOutputBufferAvailable: byteBuffer with data (decoder): java.nio.DirectByteBuffer[pos=0 lim=12288 cap=32768]
I/Codec: onOutputBufferAvailable: allocated byteBuffer (decoder): java.nio.HeapByteBuffer[pos=0 lim=12288 cap=12288]
I/Codec: onOutputBufferAvailable (decoder): added in queue: java.nio.HeapByteBuffer[pos=12288 lim=12288 cap=12288]
    0 12288 64000 0
I/Codec: onInputBufferAvailable (encoder): index=0
I/Codec: onInputBufferAvailable (encoder): queue poll != null
I/Codec: onInputBufferAvailable: byteBuffer b/f queue (encoder): java.nio.DirectByteBuffer[pos=0 lim=4096 cap=4096]
I/Codec: onInputBufferAvailable (encoder): java.nio.HeapByteBuffer[pos=0 lim=0 cap=0]
    0 0 0 0
I/Codec: queueInputBuffer (encoder): SUCCESS
I/Codec: onInputBufferAvailable (encoder): index=1
I/Codec: onInputBufferAvailable (encoder): queue poll != null
I/Codec: onInputBufferAvailable: byteBuffer b/f queue (encoder): java.nio.DirectByteBuffer[pos=0 lim=4096 cap=4096]
I/Codec: onInputBufferAvailable (encoder): java.nio.HeapByteBuffer[pos=0 lim=0 cap=0]
    0 0 21333 0
I/Codec: queueInputBuffer (encoder): SUCCESS
I/Codec: onInputBufferAvailable (encoder): index=2
I/Codec: onInputBufferAvailable (encoder): queue poll != null
I/Codec: onInputBufferAvailable: byteBuffer b/f queue (encoder): java.nio.DirectByteBuffer[pos=0 lim=4096 cap=4096]
I/Codec: onInputBufferAvailable (encoder): java.nio.HeapByteBuffer[pos=12288 lim=12288 cap=12288]
    0 12288 42666 0
E/Codec: EXCEPTION (encoder)!
    onInputBufferAvailable (encoder):
    java.lang.IllegalArgumentException
        at android.media.MediaCodec.native_queueInputBuffer(Native Method)
        at android.media.MediaCodec.queueInputBuffer(MediaCodec.java:2450)
        at opensource.umnik.media2media.codec.SyncCodec$2.onInputBufferAvailable(SyncCodec.java:149)
        at android.media.MediaCodec$EventHandler.handleCallback(MediaCodec.java:1738)
        at android.media.MediaCodec$EventHandler.handleMessage(MediaCodec.java:1696)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
D/AndroidRuntime: Shutting down VM

at android.media.MediaCodec.queueInputBuffer(MediaCodec.java:2450) ← this is encoder's buffer

What I did wrong?

UPD: code updated some times.
UPD2: more logs


来源: stackexchange

问:
Div width causing bug in IE when created in jQuery
I'm creating a schedule/calendar view in HTML/CSS/JQuery to help me learn a few things about all three.

So, if you go to....

http://handtomouthmusic.co.uk/Test.html

...you can see the beginnings of it all.  The "Static" view is just that, and the "Dynamic" view is created using jQuery.

The problem I face at the moment is that the width on the ".caption" class seems to be causing the ".bar" divs to disappear when viewed in IE? If you look in Google Chrome it should be fine.

As you can see in the example, it is only happening on the "Dynamic" section.

Any ideas what is causing this?

Note: If I press F12 in IE and disable the width attribute, then re-enable it, everything appears as it should?  Why not when loaded?

答:
The page renders in quirks mode because there is not DOCTYPE defined.

Set one so that the page renders in Standards mode.

It works fine for me although the static shows some problems then (that happens because you do not define a unit for the widths and positions, you should add px after the amount, so the inline widths should be 100px and not 100 etc..)

ContentLicense: CC BY-SA 3.0


问:
Configure database with keycloak using database service name
I have already successfully configured a database with keycloak using database SID and following configuration. However, I have to configure another database which doesn't has SID but with a service name in docker-compose file. I couldn't find a way to configure database to keycloak using it's service name.

Configuration using SID in docker-compose file

```<code>         - name: DB_VENDOR
           value: ORACLE
         - name: DB_ADDR
           value: xxx.xx.xx.xxx
         - name: DB_PORT
           value: "1521"
         - name: DB_DATABASE
           value: sid_name
         - name: DB_USER
           value: db_user_name
         - name: DB_PASSWORD
           value: ******
</code>```

When I tried to use service name instead of SID as value for DB_DATABASE it gave  following error.

```<code>Caused by: java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
</code>```

Appreciate your kind help to solve this issue.

Thank you in advance..

答:
It seems like Keycloak's docker container doesn't support it. But you can build a custom docker image to fix it. Please take a look at file change-database.cli, there is line:

```<code>connection-url=jdbc:oracle:thin:@${env.DB_ADDR:oracle}:${env.DB_PORT:1521}:${env.DB_DATABASE:XE}${env.JDBC_PARAMS:}
</code>```

And you need this line like this (slash after port instead of colon, this is required for Oracle service name to work):

```<code>connection-url=jdbc:oracle:thin:@${env.DB_ADDR:oracle}:${env.DB_PORT:1521}/${env.DB_DATABASE:XE}${env.JDBC_PARAMS:}
</code>```

So change this file and build a custom Keycloak docker image. Dockerfile may look something like:

```<code>FROM jboss/keycloak:8.0.1
ADD /<path_on_your_system>/change-database.cli /opt/jboss/tools/cli/databases/oracle/change-database.cli
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I have already successfully configured a database with keycloak using database SID and following configuration. However, I have to configure another database which doesn't has SID but with a service name in docker-compose file. I couldn't find a way to configure database to keycloak using it's service name.

Configuration using SID in docker-compose file

         - name: DB_VENDOR
           value: ORACLE
         - name: DB_ADDR
           value: xxx.xx.xx.xxx
         - name: DB_PORT
           value: "1521"
         - name: DB_DATABASE
           value: sid_name
         - name: DB_USER
           value: db_user_name
         - name: DB_PASSWORD
           value: ******

When I tried to use service name instead of SID as value for DB_DATABASE it gave  following error.

Caused by: java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor

Appreciate your kind help to solve this issue.

Thank you in advance..

Body:
It seems like Keycloak's docker container doesn't support it. But you can build a custom docker image to fix it. Please take a look at file change-database.cli, there is line:

connection-url=jdbc:oracle:thin:@${env.DB_ADDR:oracle}:${env.DB_PORT:1521}:${env.DB_DATABASE:XE}${env.JDBC_PARAMS:}

And you need this line like this (slash after port instead of colon, this is required for Oracle service name to work):

connection-url=jdbc:oracle:thin:@${env.DB_ADDR:oracle}:${env.DB_PORT:1521}/${env.DB_DATABASE:XE}${env.JDBC_PARAMS:}

So change this file and build a custom Keycloak docker image. Dockerfile may look something like:

FROM jboss/keycloak:8.0.1
ADD /<path_on_your_system>/change-database.cli /opt/jboss/tools/cli/databases/oracle/change-database.cli


问:
Run thread with derived method from base class without using templates
I'd like to achieve non-templated class just like commented one. Uncommented class will results in an compilation error error: invalid use of non-static member function 'virtual void BaseThread::worker()

```<code>#include <iostream>
#include <thread>

/*
class BaseThread
{
public:
    void start()
    {
// error: invalid use of non-static member function 'virtual void BaseThread::worker()'
        thread_ = std::thread(worker);
    }

private:
    virtual void worker() = 0;

    std::thread thread_;
};
*/

template <typename Derived>
class BaseThread
{
public:
    void start()
    {
        thread_ = std::thread(Derived::worker);
    }

private:
    virtual void worker() = 0;

    std::thread thread_;
};

class DerivedThread : public BaseThread<DerivedThread>
{
private:
    void worker()
    {
        std::cout << "Derived\n";
    }
};

int main()
{
    DerivedThread dt;
}
</code>```

答:
Simpler and non-templated implementation could be this.   </p>

```<code>#include <thread>
#include <iostream>
#include <functional>

class BaseThread
{
public:
    virtual ~BaseThread() { thread_.join(); }
    void start()
    {
        thread_ = std::thread( &BaseThread::worker , this );
    }

protected:

    virtual void worker() = 0;

private:

    std::thread thread_;
};

class DerivedThread : public BaseThread
{

protected:

    void worker() override
    {
        std::cout << "Derived\n";
    }
};

int main()
{

    DerivedThread dt;
    DerivedThread dt2;

    dt.start();
    dt2.start();

    std::this_thread::sleep_for( std::chrono::seconds { 5 } );
}
</code>```

Body:
I'd like to achieve non-templated class just like commented one. Uncommented class will results in an compilation error error: invalid use of non-static member function 'virtual void BaseThread::worker()

#include <iostream>
#include <thread>

/*
class BaseThread
{
public:
    void start()
    {
// error: invalid use of non-static member function 'virtual void BaseThread::worker()'
        thread_ = std::thread(worker);
    }

private:
    virtual void worker() = 0;

    std::thread thread_;
};
*/

template <typename Derived>
class BaseThread
{
public:
    void start()
    {
        thread_ = std::thread(Derived::worker);
    }

private:
    virtual void worker() = 0;

    std::thread thread_;
};

class DerivedThread : public BaseThread<DerivedThread>
{
private:
    void worker()
    {
        std::cout << "Derived\n";
    }
};

int main()
{
    DerivedThread dt;
}

Body:
Simpler and non-templated implementation could be this.

#include <thread>
#include <iostream>
#include <functional>

class BaseThread
{
public:
    virtual ~BaseThread() { thread_.join(); }
    void start()
    {
        thread_ = std::thread( &BaseThread::worker , this );
    }

protected:

    virtual void worker() = 0;

private:

    std::thread thread_;
};

class DerivedThread : public BaseThread
{

protected:

    void worker() override
    {
        std::cout << "Derived\n";
    }
};

int main()
{

    DerivedThread dt;
    DerivedThread dt2;

    dt.start();
    dt2.start();

    std::this_thread::sleep_for( std::chrono::seconds { 5 } );
}


问:
Merging data frames based on value in row and column name
I work with financial data and try to merge two pandas data frames.

In the first data frame I have the information of company name, ticker code, and date.

```<code>    Date       Ticker     Company
0   2020-01-15 CHR.CO     Chr. Hansen
1   2020-01-15 PNDORA.CO  Pandora A/S
</code>```

In my second df, I have a date and closing prices for stocks on some given dates.

```<code>    Date        CHR.CO       COLO-B.CO  DANSKE.CO   PNDORA.CO   VWS.CO
0   2020-01-15  89.5         89.5        187.39      54.4       552.0
1   2020-01-16  90           88.0        184.61      55.2       550.0
</code>```

How could I merge these two data frames so I could get the closing stock price in the first dataframe?

Here's the desired output:

```<code>Date       Ticker     Company         Close_price
0   2020-01-15 CHR.CO     Chr. Hansen 89.5
1   2020-01-15 PNDORA.CO  Pandora A/S 54.4
</code>```

Using the below line I merge the two dataframes on date, but also get all the tickers and the close price for all companies.

```<code>full = new_df.merge(stocks_close, on = "Date")
</code>```

答:
Add DataFrame.melt before merge and also specify both columns ["Date",'Ticker'] in parameter on:

```<code>df = stocks_close.melt(id_vars='Date', var_name='Ticker', value_name='Close_price')
full = new_df.merge(df, on = ["Date",'Ticker'])
print (full)
         Date     Ticker      Company  Close_price
0  2020-01-15     CHR.CO  Chr. Hansen         89.5
1  2020-01-15  PNDORA.CO  Pandora A/S         54.4
</code>```

Body:
I work with financial data and try to merge two pandas data frames.

In the first data frame I have the information of company name, ticker code, and date.

    Date       Ticker     Company
0   2020-01-15 CHR.CO     Chr. Hansen
1   2020-01-15 PNDORA.CO  Pandora A/S

In my second df, I have a date and closing prices for stocks on some given dates.

    Date        CHR.CO       COLO-B.CO  DANSKE.CO   PNDORA.CO   VWS.CO
0   2020-01-15  89.5         89.5        187.39      54.4       552.0
1   2020-01-16  90           88.0        184.61      55.2       550.0

How could I merge these two data frames so I could get the closing stock price in the first dataframe?

Here's the desired output:

Date       Ticker     Company         Close_price
0   2020-01-15 CHR.CO     Chr. Hansen 89.5
1   2020-01-15 PNDORA.CO  Pandora A/S 54.4

Using the below line I merge the two dataframes on date, but also get all the tickers and the close price for all companies.

full = new_df.merge(stocks_close, on = "Date")

Body:
Add DataFrame.melt before merge and also specify both columns ["Date",'Ticker'] in parameter on:

df = stocks_close.melt(id_vars='Date', var_name='Ticker', value_name='Close_price')
full = new_df.merge(df, on = ["Date",'Ticker'])
print (full)
         Date     Ticker      Company  Close_price
0  2020-01-15     CHR.CO  Chr. Hansen         89.5
1  2020-01-15  PNDORA.CO  Pandora A/S         54.4


问:
Popover attached to cell in UICollectionView moving when is called the ReloadData of this collection view
I have a UICollectionView that the user can clicks and it shows a popover attached to the cell clicked. In iOS 12, the popover stayed in the same origin (x,y or cell) if the reload data was called or not, but in iOS 13, the popover moves between the cells when the reload data of the collection view is called.

Follow a video of the behavior in iOS 13:
https://vimeo.com/385021234

The presentation is done using the following code:

```<code>func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    guard let cell = collectionView.cellForItem(at: indexPath) else {
        return
    }

    presentPopover(from: self, cell: cell)
}

func presentPopover(from view: UIViewController, cell: UIView) {
    let popoverView = PopoverViewController(nibName: "PopoverViewController", bundle: nil)
    let popover: UIPopoverPresentationController = popoverView.popoverPresentationController!

    popover.sourceRect = cell.bounds
    popover.sourceView = cell

    view.present(popoverView, animated: true, completion: nil)
}
</code>```

And the PopoverViewController is using the modalPresentationStyle = .popover

Anyone had this issue before in iOS 13?
Our application was working fine in iOS 11 and 12.

I have an example of this behavior in the following git repository: https://github.com/diegodossantos95/UICollectionViewPopoverBug

Steps to reproduce in the repository:

Click on a collection cell

Popover opens

Wait for the reload data

Thanks,

Diego.

答:
When you call reloadData this causes the collection view to discard any currently visible items (including placeholders) and recreate items based on the current state of the data source object.

Se the difference between the cells frame before and after the reload data:

```<code>  - some : <UICollectionViewCell: 0x7fd8abd02190; frame = (73.5 10; 50 50); clipsToBounds = YES; hidden = YES; opaque = NO; layer = <CALayer: 0x600001b50e20>>
  - some : <UICollectionViewCell: 0x7fd8abd02190; frame = (519.5 10; 50 50); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x600001b50e20>>
</code>```

if you need to change only one or more itens item on the collection try to use:

```<code>func reloadItems(at: [IndexPath])
</code>```

Body:
I have a UICollectionView that the user can clicks and it shows a popover attached to the cell clicked. In iOS 12, the popover stayed in the same origin (x,y or cell) if the reload data was called or not, but in iOS 13, the popover moves between the cells when the reload data of the collection view is called.

Follow a video of the behavior in iOS 13:
https://vimeo.com/385021234

The presentation is done using the following code:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    guard let cell = collectionView.cellForItem(at: indexPath) else {
        return
    }

    presentPopover(from: self, cell: cell)
}

func presentPopover(from view: UIViewController, cell: UIView) {
    let popoverView = PopoverViewController(nibName: "PopoverViewController", bundle: nil)
    let popover: UIPopoverPresentationController = popoverView.popoverPresentationController!

    popover.sourceRect = cell.bounds
    popover.sourceView = cell

    view.present(popoverView, animated: true, completion: nil)
}

And the PopoverViewController is using the modalPresentationStyle = .popover

Anyone had this issue before in iOS 13?
Our application was working fine in iOS 11 and 12.

I have an example of this behavior in the following git repository: https://github.com/diegodossantos95/UICollectionViewPopoverBug

Steps to reproduce in the repository:

Click on a collection cell

Popover opens

Wait for the reload data

Thanks,

Diego.

Body:
When you call reloadData this causes the collection view to discard any currently visible items (including placeholders) and recreate items based on the current state of the data source object.

Se the difference between the cells frame before and after the reload data:

  - some : <UICollectionViewCell: 0x7fd8abd02190; frame = (73.5 10; 50 50); clipsToBounds = YES; hidden = YES; opaque = NO; layer = <CALayer: 0x600001b50e20>>
  - some : <UICollectionViewCell: 0x7fd8abd02190; frame = (519.5 10; 50 50); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x600001b50e20>>

if you need to change only one or more itens item on the collection try to use:

func reloadItems(at: [IndexPath])


问:
How to use a string variable into re.findall?
I used Python to find a word started and ended with specified letters through a text file. But, I have some problem with using a string variable inside re.findall command. Could you please give me any suggestion?

```<code>import re
import sys

f = open(sys.argv[1]+'.txt', "r")
data = f.read()
re.findall(r'\b'+str(sys.argv[2])'\w+', data)
</code>```

Could you please expanding this code to find a word started and ended with defined letters?

答:
What you are looking for is re.compile function, not sure what your code does but with your given example:

import re
import sys
f = open(sys.argv[1]+'.txt',"r")
data = f.read()
regex = re.compile(r"\b" + str(sys.argv[2]) + r"\w+")
re.findall(regex, data)
```

<p>If you want to get words starting and ending with specified letters you could use something like:</p>

<pre class="lang-py prettyprint-override"><code>import re
import sys
f = open(sys.argv[1]+'.txt',"r")
data = f.read()
                   # starting with 'a' and ending with 'w'
regex = re.compile("^a" + str(sys.argv[2]) + "w$")
re.findall(regex, data)
</code>```

Body:
I used Python to find a word started and ended with specified letters through a text file. But, I have some problem with using a string variable inside re.findall command. Could you please give me any suggestion?

import re
import sys

f = open(sys.argv[1]+'.txt', "r")
data = f.read()
re.findall(r'\b'+str(sys.argv[2])'\w+', data)

Could you please expanding this code to find a word started and ended with defined letters?

Body:
What you are looking for is re.compile function, not sure what your code does but with your given example:

import re
import sys
f = open(sys.argv[1]+'.txt',"r")
data = f.read()
regex = re.compile(r"\b" + str(sys.argv[2]) + r"\w+")
re.findall(regex, data)

If you want to get words starting and ending with specified letters you could use something like:

import re
import sys
f = open(sys.argv[1]+'.txt',"r")
data = f.read()
                   # starting with 'a' and ending with 'w'
regex = re.compile("^a" + str(sys.argv[2]) + "w$")
re.findall(regex, data)


问:
MASM Offset vs. Label for addressing
I'm currently working my way through the Irvine x86 Assembly book, and I'm on chapter four.

They've introduced the OFFSET directive, but I'm confused on why I'd ever use it. Why wouldn't I just take the label (which is already the address of that data)? It seems like OFFSET just adds extra noise.

I have this small program to illustrate my point. I have a label for some data called array and I can move the elements of my array into al. But the book is talking about using the OFFSET directive to get the address of array and move it to esi. But this just seems unnecessary to me as I could just use the label.

I have two sections of code that do the same thing below. One where I'm using the label to access the elements of the array and the other where I'm using OFFSET to move the address into esi and then access the elements of the array.

```<code>.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD

.data
    array   BYTE 10h, 20h, 30h, 40h, 50h

.code
main PROC
    xor eax, eax        ; set eax to 0

    ; Using Labels
    mov al, array
    mov al, [array + 1]
    mov al, [array + 2]
    mov al, [array + 3]
    mov al, [array + 4]

    ; Using Offset
    mov esi, OFFSET array
    mov al, [esi]
    mov al, [esi + 1]
    mov al, [esi + 2]
    mov al, [esi + 3]
    mov al, [esi + 4]

    INVOKE ExitProcess, 0
main ENDP
END main
</code>```

Are they really just two ways to achieve the same thing?

Later on in the book when talking about pointers, they have this example:

```<code>.data
arrayB byte 10h, 20h, 30h, 40h
ptrB dword arrayB
</code>```

And this makes sense to me. ptrB holds the address of arrayB. But then they say, "Optionally, you can delcare ptrB with the OFFSET operator to make the relationship clearer:"

```<code>ptrB dword OFFSET arrayB
</code>```

That doesn't make it clearer to me at all. I already know arrayB is an address. It looks like OFFSET is just thrown in there and it's not really doing anything. Removing OFFSET from that last line would literally achieve the same thing. What exactly does OFFSET do if I can just use a label to get the address anyways?

答:
Are they really just two ways to achieve the same thing?

Yes, assembly has many ways to do things.

The C equivalent would be

char *p = array; then using p[0], p[1] etc. vs. using array[0], array[1], etc.

The advantage of putting a pointer in a register is that it saves some code size when you use it repeatedly; 2-byte mov instruction with just opcode + ModRM instead of encoding the absolute address into every instruction separately for a [disp32] addressing mode.

The other advantage is that you can increment the pointer with inc esi.  In other cases where you don't fully unroll a loop, you need either a pointer or an index in a register.

A plain pointer is usually better than [array + ecx], especially better than [array + ecx*4] because indexed addressing modes have some downsides.  ([array + ecx] is technically not indexed; it's [base + disp32] and doesn't need a SIB byte, and doesn't count as indexed for Micro fusion and addressing modes).

You can use byte offsets, though (e.g. add ecx, TYPE array), to allow a [base + disp32] addressing mode into a static array of int instead of [disp32 + idx*scale].

Using [disp32] every time avoids needing an extra instruction to put the address in a register.  mov reg, imm32 is only a 5-byte single-uop instruction, but it still might not be worth it for performance before a couple static array accesses.  It might depend on the how often your code is already hot in the uop cache vs. how often it has to fetch/decode.  (Saving code size improves L1 I$ hit rate, or at least means more instructions fit in one cache line, so it can be worth it to use more instructions / more uops if it saves code size in something that's not in the hottest inner loop.)

Before a loop (not fully unrolled), you'd normally need an instruction to zero a loop counter / index anyway, like xor ecx, ecx.  Using mov reg, imm32 is only 3 bytes longer, and no extra uops.  If you're saving 4 or 5 bytes every time you use the pointer instead of an indexed addressing mode, you already come out ahead from just one array reference per iteration.  And at a cost of no extra uops.  (Ignoring any minor differences between the outside-the-loop cost of executing an xor-zeroing vs. mov-immediate instruction.)

Note that for x86-64, you'd typically put a static address in a register with a 7-byte RIP-relative LEA.  And for you code to be LargeAddressAware at all, you can't use [array + rcx] because that only works with a [disp32 + reg] addressing mode, not [RIP + rel32].

And BTW, for consistency I'd recommend this over mov al, array

```<code>    mov al, [array + 0]
    mov al, [array + 1]
    ...
</code>```

The first comment under your question is from someone you confused by doing mov al, array and then mov al, [array + 1] using 2 different syntaxes for similar addresses; I think Jester thought you intended something like mov al, OFFSET array.  BTW, you could instead write it this way (I think)

```<code>mov al, array
mov al, array + 1
</code>```

but I always recommend using square brackets around a memory operand for clarity.  Especially if you ever look at NASM syntax where that's always required, but some people recommend that convention even if you only use MASM.  (But beware that MASM does ignore brackets in some cases, when there's no register: Confusing brackets in MASM32 so don't think that using brackets in MASM makes it work like NASM.)

BTW, the performance-efficient way to load a single byte is to zero-extend it into the full register, instead of merging into the low byte of a full register.  movzx eax, byte ptr [esi]

Also BTW, yes, mov esi, OFFSET array (5 bytes) is the most efficient way to put a static address in a register (code size and performance).  lea esi, array is 6 bytes (opcode + modrm + [disp32] addressing mode) and can run on fewer execution ports; never use LEA without a register in 32-bit mode.

In 64-bit mode you want lea rsi, array because MASM automatically uses RIP-relative addressing for that, which you want.  Otherwise still use mov esi, OFFSET array (yes ESI, not RSI) for code that isn't LargeAddressAware and can still take advantage of compact code using 32-bit absolute addresses.

Title: MASM Offset vs. Label for addressing

Body:
I'm currently working my way through the Irvine x86 Assembly book, and I'm on chapter four.

They've introduced the OFFSET directive, but I'm confused on why I'd ever use it. Why wouldn't I just take the label (which is already the address of that data)? It seems like OFFSET just adds extra noise.

I have this small program to illustrate my point. I have a label for some data called array and I can move the elements of my array into al. But the book is talking about using the OFFSET directive to get the address of array and move it to esi. But this just seems unnecessary to me as I could just use the label.

I have two sections of code that do the same thing below. One where I'm using the label to access the elements of the array and the other where I'm using OFFSET to move the address into esi and then access the elements of the array.

.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD

.data
    array   BYTE 10h, 20h, 30h, 40h, 50h

.code
main PROC
    xor eax, eax        ; set eax to 0

    ; Using Labels
    mov al, array
    mov al, [array + 1]
    mov al, [array + 2]
    mov al, [array + 3]
    mov al, [array + 4]

    ; Using Offset
    mov esi, OFFSET array
    mov al, [esi]
    mov al, [esi + 1]
    mov al, [esi + 2]
    mov al, [esi + 3]
    mov al, [esi + 4]

    INVOKE ExitProcess, 0
main ENDP
END main

Are they really just two ways to achieve the same thing?

Later on in the book when talking about pointers, they have this example:

.data
arrayB byte 10h, 20h, 30h, 40h
ptrB dword arrayB

And this makes sense to me. ptrB holds the address of arrayB. But then they say, "Optionally, you can delcare ptrB with the OFFSET operator to make the relationship clearer:"

ptrB dword OFFSET arrayB

That doesn't make it clearer to me at all. I already know arrayB is an address. It looks like OFFSET is just thrown in there and it's not really doing anything. Removing OFFSET from that last line would literally achieve the same thing. What exactly does OFFSET do if I can just use a label to get the address anyways?

Body:
Are they really just two ways to achieve the same thing?

Yes, assembly has many ways to do things.

The C equivalent would be

char *p = array; then using p[0], p[1] etc. vs. using array[0], array[1], etc.

The advantage of putting a pointer in a register is that it saves some code size when you use it repeatedly; 2-byte mov instruction with just opcode + ModRM instead of encoding the absolute address into every instruction separately for a [disp32] addressing mode.

The other advantage is that you can increment the pointer with inc esi.  In other cases where you don't fully unroll a loop, you need either a pointer or an index in a register.

A plain pointer is usually better than [array + ecx], especially better than [array + ecx*4] because indexed addressing modes have some downsides.  ([array + ecx] is technically not indexed; it's [base + disp32] and doesn't need a SIB byte, and doesn't count as indexed for Micro fusion and addressing modes).

You can use byte offsets, though (e.g. add ecx, TYPE array), to allow a [base + disp32] addressing mode into a static array of int instead of [disp32 + idx*scale].

Using [disp32] every time avoids needing an extra instruction to put the address in a register.  mov reg, imm32 is only a 5-byte single-uop instruction, but it still might not be worth it for performance before a couple static array accesses.  It might depend on the how often your code is already hot in the uop cache vs. how often it has to fetch/decode.  (Saving code size improves L1 I$ hit rate, or at least means more instructions fit in one cache line, so it can be worth it to use more instructions / more uops if it saves code size in something that's not in the hottest inner loop.)

Before a loop (not fully unrolled), you'd normally need an instruction to zero a loop counter / index anyway, like xor ecx, ecx.  Using mov reg, imm32 is only 3 bytes longer, and no extra uops.  If you're saving 4 or 5 bytes every time you use the pointer instead of an indexed addressing mode, you already come out ahead from just one array reference per iteration.  And at a cost of no extra uops.  (Ignoring any minor differences between the outside-the-loop cost of executing an xor-zeroing vs. mov-immediate instruction.)

Note that for x86-64, you'd typically put a static address in a register with a 7-byte RIP-relative LEA.  And for you code to be LargeAddressAware at all, you can't use [array + rcx] because that only works with a [disp32 + reg] addressing mode, not [RIP + rel32].

And BTW, for consistency I'd recommend this over mov al, array

    mov al, [array + 0]
    mov al, [array + 1]
    ...

The first comment under your question is from someone you confused by doing mov al, array and then mov al, [array + 1] using 2 different syntaxes for similar addresses; I think Jester thought you intended something like mov al, OFFSET array.  BTW, you could instead write it this way (I think)

mov al, array
mov al, array + 1

but I always recommend using square brackets around a memory operand for clarity.  Especially if you ever look at NASM syntax where that's always required, but some people recommend that convention even if you only use MASM.  (But beware that MASM does ignore brackets in some cases, when there's no register: Confusing brackets in MASM32 so don't think that using brackets in MASM makes it work like NASM.)

BTW, the performance-efficient way to load a single byte is to zero-extend it into the full register, instead of merging into the low byte of a full register.  movzx eax, byte ptr [esi]

Also BTW, yes, mov esi, OFFSET array (5 bytes) is the most efficient way to put a static address in a register (code size and performance).  lea esi, array is 6 bytes (opcode + modrm + [disp32] addressing mode) and can run on fewer execution ports; never use LEA without a register in 32-bit mode.

In 64-bit mode you want lea rsi, array because MASM automatically uses RIP-relative addressing for that, which you want.  Otherwise still use mov esi, OFFSET array (yes ESI, not RSI) for code that isn't LargeAddressAware and can still take advantage of compact code using 32-bit absolute addresses.


问:
How to get the duplicate key values in freemarker?
my input is :

```<code>someMap = { "item": "first" , "item": "second" }
</code>```

template:

```<code><#list someMap?keys as k>
  ${k}
</#list>
</code>```

output:

```<code>item
</code>```

but the expected output is :

```<code>item
item
</code>```

How to get the duplicate keys in freemarker?

Thank you!

答: That's not possible. A dict contains only one value per key.</p>

Body:
my input is :

someMap = { "item": "first" , "item": "second" }

template:

<#list someMap?keys as k>
  ${k}
</#list>

output:

item

but the expected output is :

item
item

How to get the duplicate keys in freemarker?

Thank you!


问:
Angular - Calling a function from an extended class
I'm using a RAD software that generates Angular code.

It always generates 2 components, a "generated" one, and a empty one where you can put some custom functions.

I want to call from the empty one, let's call it the child component, a function that is present in the "parent" component.

To explain it better, here follows the declaration of the 2 components (they are in the same folder):

Parent component:

```<code>export class MainViewGenerated implements AfterViewInit, OnInit, OnDestroy {

  constructor(private injector: Injector) {}

 myFunc() {
      console.log("hello World")
  }
</code>```

Child component:

```<code>export class MainViewComponent extends MainViewGenerated {

  constructor(injector: Injector) {
    super(injector);
  }
</code>```

so what is the correct syntax to call, from the Child component, the function "myFunc()" declared in the Parent component?

答:
You can call the method of your parent component using the super keyword:

```<code>export class MainViewComponent extends MainViewGenerated {
  constructor(injector: Injector) {
    super(injector);
    super.myFunc();
  }
}
</code>```

Body:
I'm using a RAD software that generates Angular code.

It always generates 2 components, a "generated" one, and a empty one where you can put some custom functions.

I want to call from the empty one, let's call it the child component, a function that is present in the "parent" component.

To explain it better, here follows the declaration of the 2 components (they are in the same folder):

Parent component:

export class MainViewGenerated implements AfterViewInit, OnInit, OnDestroy {

  constructor(private injector: Injector) {}

 myFunc() {
      console.log("hello World")
  }

Child component:

export class MainViewComponent extends MainViewGenerated {

  constructor(injector: Injector) {
    super(injector);
  }

so what is the correct syntax to call, from the Child component, the function "myFunc()" declared in the Parent component?

Body:
You can call the method of your parent component using the super keyword:

export class MainViewComponent extends MainViewGenerated {
  constructor(injector: Injector) {
    super(injector);
    super.myFunc();
  }
}


问:
How can I create a custom crontab?
I want a crontab to run every 15 minutes starting from 9:45 to 16:00.

I currently have this:

```<code>crontab(minute='*/15', hour='9:45-16', day_of_week='mon,tue,wed,thu,fri')
</code>```

but 9:45 is not valid

Is there any way to solve this?

答:
Read crontab(5). Run crontab(1) or edit /etc/crontab according to your needs (perhaps using emacs  thru sudo). Of course you might use some Python library editing these textual files.

I see no reason to use Python for your purposes. Of course, you could run your Python script from cron, or any other executable. See execve(2). Beware that your $PATH could be different for a script started from cron and in your interactive shell.

Title: How can I create a custom crontab?

Body:
I want a crontab to run every 15 minutes starting from 9:45 to 16:00.

I currently have this:

crontab(minute='*/15', hour='9:45-16', day_of_week='mon,tue,wed,thu,fri')

but 9:45 is not valid

Is there any way to solve this?


问:
.click() callback function explained
I am making a Google Chrome extension and I am trying to use the .click() function within one of my script. The click function should click a link which redirects me to another page. Once I have been redirected, I would like to send a chrome.runtime message.

```<code>document.getElementsByClassName('example-link')[1].click(function() {
       console.log("Hello World");
        let data = {
            type: "example"
        }
        chrome.runtime.sendMessage({...data});

    });
</code>```

So far the page redirects, but the console.log() doesn't show and the runtime message doesn't send.
I am not sure whether I have misunderstood the purpose of the callback function or whether the scripts stops running because I am on a new page.

Any explanation / help would be appreciated.

答:
click() doesn't take a callback.

In order to detect navigation you need:

to listen for an onUpdated event (see How to listen for url change with Chrome Extension.)

then click() to trigger the navigation

Title: .click() callback function explained

Body:
I am making a Google Chrome extension and I am trying to use the .click() function within one of my script. The click function should click a link which redirects me to another page. Once I have been redirected, I would like to send a chrome.runtime message.

document.getElementsByClassName('example-link')[1].click(function() {
       console.log("Hello World");
        let data = {
            type: "example"
        }
        chrome.runtime.sendMessage({...data});

    });

So far the page redirects, but the console.log() doesn't show and the runtime message doesn't send.
I am not sure whether I have misunderstood the purpose of the callback function or whether the scripts stops running because I am on a new page.

Any explanation / help would be appreciated.


问:
How can I do something 0.5 seconds after text changed in my EditText control?
I am filtering my list using an EditText control. I want to filter the list 0.5 seconds after the user has finished typing in EditText. I used the afterTextChanged event of TextWatcher for this purpose. But this event rises for each character changes in EditText.

What should I do?

答:
Try this

```<code>class DelayTextWatcher(val ms: Long = 500, val textChanged: (String) -> Unit) : TextWatcher {

private var timer: CountDownTimer? = null
override fun afterTextChanged(p0: Editable) {
    timer?.cancel()
    timer = object : CountDownTimer(ms, ms) {
        override fun onTick(millisUntilFinished: Long) {

        }

        override fun onFinish() {
            textChanged(p0.toString())
        }
    }.start()
}

override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}

override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}

fun dispose() {
    timer?.cancel()
}
</code>```

}

Body:
Try this

class DelayTextWatcher(val ms: Long = 500, val textChanged: (String) -> Unit) : TextWatcher {

private var timer: CountDownTimer? = null
override fun afterTextChanged(p0: Editable) {
    timer?.cancel()
    timer = object : CountDownTimer(ms, ms) {
        override fun onTick(millisUntilFinished: Long) {

        }

        override fun onFinish() {
            textChanged(p0.toString())
        }
    }.start()
}

override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}

override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}

fun dispose() {
    timer?.cancel()
}

}


问:
FIWARE - Token API Orion Context Broker
I have in my server a Orion Context Broker (https://github.com/telefonicaid/fiware-orion) but i need to add some restrictions to my "consumers" when they use the endpoint(s) e.g(http://myhost:1026/v2/entities). Is possible configure the local/personal Broker with token like https://fiware-orion.readthedocs.io/en/1.11.0/quick_start_guide/index.html#orion-context-broker-quick-start-guide ?

Thank you very much.

答:
The Orion Context Broker does not offer roles and permissions directly.
To add roles and permissions to restrict an endpoint, you will need to use a PEP Proxy - which security mechanism you use to do this is up to you.

There are several OAuth2-based security components found within the FIWARE Catalogue, alternatively you could another open source PEP Proxy such as steelskin which integrates nicely with either Keyrock or Keystone

Title: FIWARE - Token API Orion Context Broker


问:
Connect MuleSoft to SQL Server with an AD User
I am trying to get MuleSoft to talk to a SQL Server using an Active Directory User.

I have gotten it to connect using a user defined in SQL Server, however our DBAs are saying we have to connect with an AD User.

I am getting "login failed" errors

java.sql.SQLException: Cannot get connection for URL jdbc:sqlserver://AG-Q4Test1:1433;databaseName=T10SRV01 : Login failed for user 'HQ\MuleSoftSvc'

I have tried to connect using my credentials and have received the same error. I can connect using SQL Management Studio with my credentials.

I am using the Microsoft SQL Server Driver com.microsoft.sqlserver:mssql-jdbc.6.2.2.jre8

I am thinking that is because its using Windows Authentication

But will this work in CloudHub?

答:
I was able get Windows Auth working from my local runtime using this article.

After more digging I found this KB Article from MuleSoft which states that Windows Auth is not supported in CloudHub.


问:
Columns containing circles overlap vertically in Bootrap 4
I'm trying to achieve the look displayed in the image below with 'Bootstrap 4'. What I achieved is partly what I want, because there is too much vertical distance between the dots.

I first tried nesting the cols and then I used negative 'margins', but it didn't help move the circles vertically closer to each other. Is it even possible to make the 'cols' "overlap", or do I have to find another way to achieve the Look I want?

.block {
  text-align: center;
  vertical-align: middle;
}
.circle {
  background-color: darkred;
  color: white;
  border-radius: 200px;
  height: 110px;
  width: 110px;
  display: table;
  /* margin-bottom: -50px; */

}
.circle p {
  vertical-align: middle;
  display: table-cell;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><!--Circles-->
  <div class="row no-gutters justify-content-center">
    <div class="col-lg-3 ">
      <div class="col-lg-2 block">
        <div class="circle   ">
          <p>Circle 1</p>
        </div>
      </div>
      <div class="col-lg-1 block offset-5">
        <div class="circle  ">
          <p>Circle 2</p>
        </div>
      </div>
      <div class="col-lg-1 block ">
        <div class="circle  ">
          <p>Circle 3</p>
        </div>
      </div>
      <div class="col-lg-1 block  offset-5 ">
        <div class="circle  ">
          <p>Circle 4</p>
        </div>
      </div>
      <div class="col-lg-1 block ">
        <div class="circle  ">
          <p>Circle 5</p>
        </div>
      </div>
<!-- Text-->
    </div>
    <div class="col-lg-7 ">
      <p>
        Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et
        dolore magna aliquyam erat, sed diam voluptua.
      </p>
    </div>
  </div></code>```

答:
A simple idea using flex you can use BS flex classes as well

.box {
    display: flex;
        align-items: center;
}
.containerr {
  display: flex;
  flex-direction: column;
      margin-right: 16px;
}
.holder {
  margin: 1%;
  border-radius: 50%;
  height: 40px;
  width: 40px;

   background: red;
}
.holder:before {
  content: "";
  display: inline-block;
  padding-top: 100%;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="box">
<div class="containerr">
  <div class="holder "></div>
  <div class="holder "></div>
  <div class="holder "></div>
</div>
<div class="containerr">
  <div class="holder "></div>
  <div class="holder "></div>
</div>
</div></code>```

Body:
I'm trying to achieve the look displayed in the image below with 'Bootstrap 4'. What I achieved is partly what I want, because there is too much vertical distance between the dots.

I first tried nesting the cols and then I used negative 'margins', but it didn't help move the circles vertically closer to each other. Is it even possible to make the 'cols' "overlap", or do I have to find another way to achieve the Look I want?

.block {
  text-align: center;
  vertical-align: middle;
}
.circle {
  background-color: darkred;
  color: white;
  border-radius: 200px;
  height: 110px;
  width: 110px;
  display: table;
  /* margin-bottom: -50px; */

}
.circle p {
  vertical-align: middle;
  display: table-cell;
}

Circle 1

Circle 2

Circle 3

Circle 4

Circle 5

        Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et
        dolore magna aliquyam erat, sed diam voluptua.

Body:
A simple idea using flex you can use BS flex classes as well

.box {
    display: flex;
        align-items: center;
}
.containerr {
  display: flex;
  flex-direction: column;
      margin-right: 16px;
}
.holder {
  margin: 1%;
  border-radius: 50%;
  height: 40px;
  width: 40px;

   background: red;
}
.holder:before {
  content: "";
  display: inline-block;
  padding-top: 100%;
}

<div class="box">
<div class="containerr">
  <div class="holder "></div>
  <div class="holder "></div>
  <div class="holder "></div>
</div>
<div class="containerr">
  <div class="holder "></div>
  <div class="holder "></div>
</div>
</div>


问:
Python: Removing list element while iterating over list
I'm iterating over a list of elements in Python, do some action on it, and then remove them if they meet certain criteria.

```<code>for element in somelist:
    do_action(element)
    if check(element):
        remove_element_from_list
</code>```

What should I use in place of remove_element?
I have seen similar questions asked, but notice the presence of the do_action part that is to be executed for all elements and thus eliminates the solution of using filters.

答:
```<code>for element in somelist:
    do_action(element)
somelist[:] = (x for x in somelist if not check(x))
</code>```

If you really need to do it in one pass without copying the list

```<code>i=0
while i < len(somelist):
    element = somelist[i]
    do_action(element)
    if check(element):
        del somelist[i]
    else:
        i+=1
</code>```

Body:
I'm iterating over a list of elements in Python, do some action on it, and then remove them if they meet certain criteria.

for element in somelist:
    do_action(element)
    if check(element):
        remove_element_from_list

What should I use in place of remove_element?
I have seen similar questions asked, but notice the presence of the do_action part that is to be executed for all elements and thus eliminates the solution of using filters.

Body:
for element in somelist:
    do_action(element)
somelist[:] = (x for x in somelist if not check(x))

If you really need to do it in one pass without copying the list

i=0
while i < len(somelist):
    element = somelist[i]
    do_action(element)
    if check(element):
        del somelist[i]
    else:
        i+=1


问:
Workarounds for TensorFlow (and Keras) on RStudio Server Pro Version 1.2.5033-1
Does anyone know how to use tensorflow in RStudio Cloud without running into this known fatal error? Are there versions of Python, Miniconda, TensorFlow, and Keras that make it work? My current setup script is here.

I use an RStudio Cloud project to teach a workshop. I am running into the known bug described here and here, and it is only a matter of time before the amazing folks at RStudio release a new IDE with a patch. But in the meantime, I have several workshops to teach, and I cannot install a different version of the IDE on Cloud. Cloud would be perfect if I can get it to work.

I already posted here about this, but I am trying to cast a wider net to see if anyone found a clever workaround.

答:
I figured it out! All I needed was to downgrade to TensorFlow 1.13.1. I thought I already tried that, but then I realized install_keras() automatically upgrades TensorFlow unless I supply a version string to the tensorflow argument. Here is how I set up a local Python environment with TensorFlow 1.13.1 in RStudio Cloud.

reticulate::install_miniconda("miniconda")
Sys.setenv(WORKON_HOME = "virtualenvs")
reticulate::virtualenv_create("r-reticulate", python = "miniconda/bin/python")
keras::install_keras(
  method = "virtualenv",
  conda = "miniconda/bin/conda",
  envname = "r-reticulate",
  tensorflow = "1.13.1",
  restart_session = FALSE
)
# Now add WORKON_HOME=/cloud/project/virtualenvs to .Renviron
```


问:
Which is the best option? Use an unupdated component in React or try to implement the library in react without the component?
I have found some components that are not updated with the last React version, however, those components work.  But I was wondering if those components are not updated would be the best option or a good practice to try to implement the libraries in React without the npm packages?

Thanks.

答:
I'm my experience, it'll depend upon the product you're working on. IE. you're working in a UI that is not likely changing and once it's done, it'll remain the same for some time (rare case), and it's not too complex. I'd go for the component even if it's not that updated.

If the benefits from constructing the component yourself (long term maintainability, extensibility, etc.) are important to your project and it won't take that much time, maybe it'll be the best option.


问:
Getting error in Hive : vertex failed failed due to Root_Input_init_failure, how much we can increase the memory while using Tez?
Getting error in Hive : vertex failed #some vertex name# Killed/failed due to Root_Input_init_failure</p>

答:
I'm not sure it is a memory problem; the parameter is
tez.am.resource.memory.mb  which is the size of a container.
The number of container is function of the evaluated size of the data source.
With crypto, a common workaround is (vertices crash due to changing format betwwen map and reduc)</p>

```<code>set hive.auto.convert.join=false;
</code>```

Body:
I'm not sure it is a memory problem; the parameter is
tez.am.resource.memory.mb  which is the size of a container.
The number of container is function of the evaluated size of the data source.
With crypto, a common workaround is (vertices crash due to changing format betwwen map and reduc)

set hive.auto.convert.join=false;


问:
How can i convert image buffer to photo to upload to Cloudinary?
How can I use the buffer to upload to Cloudinary as an image. In the buffer using Sharp, I am specifying it is a jpeg. I don't want to save it in the server, so I am proccesing the image, and sending it through the req object

```<code>exports.resizeUserPhoto = async (req, res, next) => {
  if (!req.file) return next();

  req.file.filename = `user-${req.user.id}-${Date.now()}.jpeg`;

  const processedImage = await sharp(req.file.buffer)
    .resize(150, 150)
    .toFormat("jpeg")
    .jpeg({ quality: 90 })
    .toBuffer();

  //saving the buffer to a new file object
  req.file.processedImage = processedImage;

  next();
};

</code>```

```<code>exports.updateMe = catchAsync(async (req, res, next) => {
if (req.file) {
    console.log(req.file.processedImage); //returns <Buffer ....... >
    const result = await cloudinary.uploader.upload(req.file.processedImage, {
      use_filename: true,
      folder: `${req.user.id}/profile/`
    });
    filteredBody.photo = result.url;
  }
}
</code>```

*** Update:
I later found out that it can also be done using DataUri, but somehow read in forums that it slows down the server... which I don't know if its true or not.

Also, most of my problem is with this method: cloudinary.uploader.upload_stream. It says that it returns a promise, which is not true.

I asked the question in Cloudinary's forum. I will post answer here.

答:
```<code>exports.updateMe = catchAsync((req, res, next) => {
  return new Promise((resolve, reject) => {
    if (req.file) {
      // console.log(req.file.processedImage.toString('base64'));
      cloudinary.v2.uploader.upload_stream({ resource_type: 'raw' }, (err, res) => {
        if (err) {
          console.log(err);
          reject(err);
        } else {
          console.log(`Upload succeed: ${res}`);
          // filteredBody.photo = result.url;
          resolve(res);
        }
      }).end(req.file.processedImage);
    }
  })
}
</code>```

Body:
How can I use the buffer to upload to Cloudinary as an image. In the buffer using Sharp, I am specifying it is a jpeg. I don't want to save it in the server, so I am proccesing the image, and sending it through the req object

exports.resizeUserPhoto = async (req, res, next) => {
  if (!req.file) return next();

  req.file.filename = `user-${req.user.id}-${Date.now()}.jpeg`;

  const processedImage = await sharp(req.file.buffer)
    .resize(150, 150)
    .toFormat("jpeg")
    .jpeg({ quality: 90 })
    .toBuffer();

  //saving the buffer to a new file object
  req.file.processedImage = processedImage;

  next();
};

exports.updateMe = catchAsync(async (req, res, next) => {
if (req.file) {
    console.log(req.file.processedImage); //returns <Buffer ....... >
    const result = await cloudinary.uploader.upload(req.file.processedImage, {
      use_filename: true,
      folder: `${req.user.id}/profile/`
    });
    filteredBody.photo = result.url;
  }
}

*** Update:
I later found out that it can also be done using DataUri, but somehow read in forums that it slows down the server... which I don't know if its true or not.

Also, most of my problem is with this method: cloudinary.uploader.upload_stream. It says that it returns a promise, which is not true.

I asked the question in Cloudinary's forum. I will post answer here.


问:
Global CONSTANTS. Any problem using them?
I've been programming for over 6 years now and i've always avoided using global variables because there is always another way around the problems.

Today, i work on a (big) project, we want to use a dictionnary of mathematical CONSTANTS what will never be modified anywhere. The only problem i seem to find with globals on internet is the fact that if someone overwrites one it could bug out the whole project. But as mine are constants this problem doesnt apply.

(as a second security to avoid people creating a variable with the same name as one of the constants i will probably pack them all in a single global struct)

Does anyone know of problems that still happend using global constants?

Thanks for your answers! :)

答:
The old-fashioned standard way to create a constant in MATLAB is to write a function. For example pi is a function. It could be written as:

function value = pi
value = 3.14159;
end
```

Of course we can overwrite the value of pi in MATLAB, but it is always a local change, it is not possible to affect another workspace.

Body:
The old-fashioned standard way to create a constant in MATLAB is to write a function. For example pi is a function. It could be written as:

function value = pi
value = 3.14159;
end

Of course we can overwrite the value of pi in MATLAB, but it is always a local change, it is not possible to affect another workspace.


问:
Kotlin: what does annotation (acc: S, T) -> S mean?
I'm very new to Kotlin. I looked at Kotlin's source code and I saw this prototype:

```<code>public inline fun <S, T : S> Iterable<T>.reduce(operation: (acc: S, T) -> S): S
</code>```

I understand that operation is a method, 'acc' is one its arguments, 'acc' is of type S and the return value of operation is of type S. However, what does the T inside list of arguments mean?
Where is its arguments?

答:
Naming the function parameters in a function type is optional. The names are only significant for documentation purposes.

What the above says is that reduce expects a function taking an S (the accumulated value) and a T (the current element). They gave the S the name acc, but didn't give the T any name.

Body:
I'm very new to Kotlin. I looked at Kotlin's source code and I saw this prototype:

public inline fun <S, T : S> Iterable<T>.reduce(operation: (acc: S, T) -> S): S

I understand that operation is a method, 'acc' is one its arguments, 'acc' is of type S and the return value of operation is of type S. However, what does the T inside list of arguments mean?
Where is its arguments?


问:
Looking for ionic app that works on offfline mode (i.e. without internet)
I have an ionic 1 (angularjs) app, which doesn't work offline, on first launch user creates account, logs in and next time he opens app he is already logged in.

The scenario is I am looking for is an offline mode(that is without internet connection), here if the user is not connected to internet he's not allowed to explore the app, here i want to let the user explore the app even without internet connection, with the credentials already logged in.

A lot of resources suggests to use localstorage, but i can't find any relevant resources regarding the same.

I have spent hour reading and testing different approaches but well even more confused than ever. It seems to me as such important feature of hybrid app that there should be a good implementation... Would appreciate any help/suggestions/examples/links...

My ultimate goal would be that once authorized user can access and manipulate his profile data even if in offline mode. That means that opening app allready logs him in an his profile info is stored as well.

My minimum viable goal would be that when app is opened app recognizes user, checks as logged in, redirects to logged in state and makes http to get all user details. While user is waiting for that response there are loading spinners but he can start to use logged in app experience.

答:
Your connection windows is controlling your App. So basing on this, you can easily make a checknetwork function to make it check if you want.

If this function return "false" then you bypass the login if only the user has already logged in.
On your provider for the LogPage, you should control this kind of things with shared values as

```<code>let isOnceConnected: boolean;
</code>```

Hope this help.

Body:
Your connection windows is controlling your App. So basing on this, you can easily make a checknetwork function to make it check if you want.

If this function return "false" then you bypass the login if only the user has already logged in.
On your provider for the LogPage, you should control this kind of things with shared values as

let isOnceConnected: boolean;

Hope this help.


问:
Result of call to 'subscribe(onNext:onError:onCompleted:onDisposed:)' is unused
I am trying to create function which return Observable<(HTTPURLResponse, Any)> using RxAlamofire:

```<code>func getResponse(credentialData: Credentials, ulr: String)->Observable<(HTTPURLResponse, Any)>{
        let credentialData = "\(credentialData.username):\(credentialData.password)".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
        let base64Credentials = credentialData.base64EncodedString()
        let headers = ("Authorization", "Basic \(base64Credentials)")
        let header = HTTPHeaders.init(dictionaryLiteral: headers)

        return Observable.create{ observer in
            requestJSON(.get, ulr, parameters: nil, encoding: JSONEncoding.default, headers: header)
            .subscribe(onNext: { response in
                observer.onNext(response)
                } ,
        onError: { error in
            observer.onError(error)
            })
        return Disposables.create()
        }
    }
}
</code>```

but I get this below warning:-

Result of call to 'subscribe(onNext:onError:onCompleted:onDisposed:)'
  is unused

How to fix it? After adding .disposed(by: disposeBag) my function isn't working.

答:
Hello you have to add your request into a DisposeBag :

```<code>return Observable.create { observer in
        requestJSON(.get, ulr, parameters: nil, encoding: JSONEncoding.default, headers: header)
            .subscribe(onNext: { response in
                observer.onNext(response)
            }, onError: { error in
                observer.onError(error)
            }).disposed(by: disposeBag)
        return Disposables.create()
    }
</code>```

That should works.

Body:
I am trying to create function which return Observable<(HTTPURLResponse, Any)> using RxAlamofire:

func getResponse(credentialData: Credentials, ulr: String)->Observable<(HTTPURLResponse, Any)>{
        let credentialData = "\(credentialData.username):\(credentialData.password)".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
        let base64Credentials = credentialData.base64EncodedString()
        let headers = ("Authorization", "Basic \(base64Credentials)")
        let header = HTTPHeaders.init(dictionaryLiteral: headers)

        return Observable.create{ observer in
            requestJSON(.get, ulr, parameters: nil, encoding: JSONEncoding.default, headers: header)
            .subscribe(onNext: { response in
                observer.onNext(response)
                } ,
        onError: { error in
            observer.onError(error)
            })
        return Disposables.create()
        }
    }
}

but I get this below warning:-

Result of call to 'subscribe(onNext:onError:onCompleted:onDisposed:)'
  is unused

How to fix it? After adding .disposed(by: disposeBag) my function isn't working.

Body:
Hello you have to add your request into a DisposeBag :

return Observable.create { observer in
        requestJSON(.get, ulr, parameters: nil, encoding: JSONEncoding.default, headers: header)
            .subscribe(onNext: { response in
                observer.onNext(response)
            }, onError: { error in
                observer.onError(error)
            }).disposed(by: disposeBag)
        return Disposables.create()
    }

That should works.


问:
How to display active directory browser from powershell
I have a GUI for AD user creation. One of the parameters I put in is location where the user shall be created. For now I have it in a text box as a simple text (ou=subcontainer,ou=container,dc=domain,dc=com). But this is not very user friendly and I'd like to add a button there to open a browser (like the left panel if you run dsa.msc) to select the path. I found a specially created browser for that, but it's again made of the text strings, which could be confusing for low-skilled technicians. Some of the people supposed to use this script are just skilled enough to create and modify a user in a GUI, but don't have much deeper knowledge and have no idea what that string means, so I'd like to have the classical gui from active directory users and computers. I tried  to search classes under system.directoryservices.activedirectory but didn't find anything that could display the dialog

答: I found what I needed here: https://gist.github.com/supercheetah/b68023f3254dfc9a6497 - posted in comment by CraftyB</p>


问:
Updating .Spec file for Pyinstaller gets reset
I am trying to modify the default .spec file created by Pyinstaller to include hidden imports and datas however everytime I run pyinstaller and specify the spec file like pyinstaller source.py spource.spec my source.spec gets rest to its default state i.e.

```<code>a = Analysis(['source.py'],
pathex=['C:\\PATHTOSOURCE'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
</code>```

what I am trying to accomplish is

```<code>from PyInstaller.utils.hooks import collect_data_files, collect_submodules
Mydatas = collect_data_files("skimage.io._plugins")
Myhiddenimports = collect_submodules('skimage.io._plugins')

a = Analysis(['source.py'],
pathex=['C:\\PATHTOSOURCE'],
binaries=[],
datas=Mydatas,
hiddenimports=Myhiddenimports,
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
</code>```

But again this gets reset to the above default analysis class
Thanks so much

答:
The official way is using makespec.py script (come with PyInstaller module) to create permanent spec file first (This example is my Windows system, you need modify the python path of your system), pass the options which same as PyInstaller options (If the makespec.py path changed in future, try to find the file start from PyInstaller module path):

```<code>"c:\Program Files\Python38\python.exe" "c:\Program Files\Python38\Lib\site-packages\PyInstaller\utils\cliutils\makespec.py" -F --noconsole --icon=example_small.ico example.py
</code>```

Then edit the generated file example.spec as you wish, then do this to generate exe file as PyInstaller do:

```<code>"c:\Program Files\Python38\python.exe" -m PyInstaller example.spec
</code>```

In future, you can simply rerun second command above to generate new exe after modified your python code. But sometime(e.g. import new library) you need to edit the example.spec file, do it in  either of two ways:

Edit example.spec file, then rerun the second command

Edit first command options, then rerun both commands. And of course, the example.spec file will get replaced by
first command. You should backup example.spec first if you edited example.spec manually before, so you can edit example.spec by referring backup file after regenerate example.spec.

More info:

https://pyinstaller.readthedocs.io/en/stable/spec-files.html

https://pyinstaller.readthedocs.io/en/stable/man/pyi-makespec.html

You can use pyi-makespec command if it already in environment path instead of makespec.py file I mentioned above. As stated:

pyi-makespec is used to create a spec file. See Using Spec Files.

If you do not perform a complete installation (installing via pip or
executing setup.py), these commands will not be installed as commands.
However, you can still execute all the functions documented below by
running Python scripts found in the distribution folder. The
equivalent of the pyinstaller command is
pyinstaller-folder/pyinstaller.py. The other commands are found in
pyinstaller-folder/cliutils/ with meaningful names (makespec.py, etc.)

Body:
I am trying to modify the default .spec file created by Pyinstaller to include hidden imports and datas however everytime I run pyinstaller and specify the spec file like pyinstaller source.py spource.spec my source.spec gets rest to its default state i.e.

a = Analysis(['source.py'],
pathex=['C:\\PATHTOSOURCE'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)

what I am trying to accomplish is

from PyInstaller.utils.hooks import collect_data_files, collect_submodules
Mydatas = collect_data_files("skimage.io._plugins")
Myhiddenimports = collect_submodules('skimage.io._plugins')

a = Analysis(['source.py'],
pathex=['C:\\PATHTOSOURCE'],
binaries=[],
datas=Mydatas,
hiddenimports=Myhiddenimports,
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)

But again this gets reset to the above default analysis class
Thanks so much

Body:
The official way is using makespec.py script (come with PyInstaller module) to create permanent spec file first (This example is my Windows system, you need modify the python path of your system), pass the options which same as PyInstaller options (If the makespec.py path changed in future, try to find the file start from PyInstaller module path):

"c:\Program Files\Python38\python.exe" "c:\Program Files\Python38\Lib\site-packages\PyInstaller\utils\cliutils\makespec.py" -F --noconsole --icon=example_small.ico example.py

Then edit the generated file example.spec as you wish, then do this to generate exe file as PyInstaller do:

"c:\Program Files\Python38\python.exe" -m PyInstaller example.spec

In future, you can simply rerun second command above to generate new exe after modified your python code. But sometime(e.g. import new library) you need to edit the example.spec file, do it in  either of two ways:

Edit example.spec file, then rerun the second command

Edit first command options, then rerun both commands. And of course, the example.spec file will get replaced by
first command. You should backup example.spec first if you edited example.spec manually before, so you can edit example.spec by referring backup file after regenerate example.spec.

More info:

https://pyinstaller.readthedocs.io/en/stable/spec-files.html

https://pyinstaller.readthedocs.io/en/stable/man/pyi-makespec.html

You can use pyi-makespec command if it already in environment path instead of makespec.py file I mentioned above. As stated:

pyi-makespec is used to create a spec file. See Using Spec Files.

If you do not perform a complete installation (installing via pip or
executing setup.py), these commands will not be installed as commands.
However, you can still execute all the functions documented below by
running Python scripts found in the distribution folder. The
equivalent of the pyinstaller command is
pyinstaller-folder/pyinstaller.py. The other commands are found in
pyinstaller-folder/cliutils/ with meaningful names (makespec.py, etc.)


问:
Auto import in Visual Studio Code only offering absolute path with Lerna subpackages in TypeScript
For some reason, very recently my Visual Studio Code changed and started only offering absolute imports from the sub-package level with my Lerna packages, for example:

As you can see, the auto import is suggesting the @package/server/src/database path to the file when it should just be ../database as the file being edited is within the same package and is just one folder below the file containing the database variable I'm trying to use.

Is this a bug or configuration issue?

I've set my Import Module Specifiersetting for TypeScript in Visual Studio Code to all three options (auto, relative, and absolute) and none of them seem to make any difference.

答:
I landed here from Google and had the opposite problem. My Visual Studio Code instance always imported the relative path even though it was from a different Lerna package.

It turns out that I simply forgot to add the package that it was wrongly importing to my consuming package’s package.json file.

Now, everything works as expected.


问:
hosting C# windows service or alternative to run continuous tasks in shared hosting environment
My application required to run continuous tasks without any user intervention but I am finding difficult how to host it on shared virtual server as a windows service, because those control panels are only providing access to IIS.

My questions is what will be way to achieve this goal or alternative to run continuous task on shared hosted server?

I am not thinking for dedicated server because that is too costly..

Thanks

答:
Can you define "continuous" in your case?
Since you can't get access to a physical box (or even full access to a virtual box), your options are kind of limited.

You could have an ASP.NET application that is kept alive somehow (most shared hosting environment will recycle when there aren't requests, but there are ways around that) that runs a process on a timer in the background. We've done this with some cleanup tasks for sites before.

But if you need something more continuous than that... I don't know. I think you're either going to need to get full access to a box so you can install your service, or change the idea of how your service runs.


问:
Chi-Square test for groups of unequal size
I'd like to apply chi-square test scipy.stats.chisquare. And the total number of observations is different in my groups.

```<code>import pandas as pd

data={'expected':[20,13,18,21,21,29,45,37,35,32,53,38,25,21,50,62],
      'observed':[19,10,15,14,15,25,25,20,26,38,50,36,30,28,59,49]}

data=pd.DataFrame(data)
print(data.expected.sum())
print(data.observed.sum())
</code>```

To ignore this is incorrect - right?

Does the default behavior of scipy.stats.chisquare takes this into account? I checked with pen and paper and looks like it doesn't. Is there a parameter for this?

```<code>from scipy.stats import chisquare
# incorrect since the number of observations is unequal
chisquare(f_obs=data.observed, f_exp=data.expected)
</code>```

When I do manual adjustment I get slightly different result.

```<code># adjust actual number of observations
data['obs_prop']=data['observed'].apply(lambda x: x/data['observed'].sum())
data['observed_new']=data['obs_prop']*data['expected'].sum()

# proper way
chisquare(f_obs=data.observed_new, f_exp=data.expected)
</code>```

Please correct me if I am wrong at some point. Thanks.

ps: I tagged R for additional statistical expertise

答:
I didn't get the question quite well. However, the way I see it is that you can use scipy.stats.chi2_contingency if you want to compute the independence test between two categorical variable.
Also the scipy.stats.chi2_sqaure can be used to compare observed vs expected. Here the number of categories should be the same. Logicaly a category would get a 0 frequency if there is an observed frequecy but the expeceted frequency does not exist and vice-versa.

Hope this helps

Body:
I'd like to apply chi-square test scipy.stats.chisquare. And the total number of observations is different in my groups.

import pandas as pd

data={'expected':[20,13,18,21,21,29,45,37,35,32,53,38,25,21,50,62],
      'observed':[19,10,15,14,15,25,25,20,26,38,50,36,30,28,59,49]}

data=pd.DataFrame(data)
print(data.expected.sum())
print(data.observed.sum())

To ignore this is incorrect - right?

Does the default behavior of scipy.stats.chisquare takes this into account? I checked with pen and paper and looks like it doesn't. Is there a parameter for this?

from scipy.stats import chisquare
# incorrect since the number of observations is unequal
chisquare(f_obs=data.observed, f_exp=data.expected)

When I do manual adjustment I get slightly different result.

# adjust actual number of observations
data['obs_prop']=data['observed'].apply(lambda x: x/data['observed'].sum())
data['observed_new']=data['obs_prop']*data['expected'].sum()

# proper way
chisquare(f_obs=data.observed_new, f_exp=data.expected)

Please correct me if I am wrong at some point. Thanks.

ps: I tagged R for additional statistical expertise


问:
How can I create a custom crontab?
I want a crontab to run every 15 minutes starting from 9:45 to 16:00.

I currently have this:

```<code>crontab(minute='*/15', hour='9:45-16', day_of_week='mon,tue,wed,thu,fri')
</code>```

but 9:45 is not valid

Is there any way to solve this?

答:
for this purpose you cannot do this in one command it is better to do it in two cron commands</p>

```<code>crontab(minute='*/15', hour='10-16', day_of_week='mon,tue,wed,thu,fri')
crontab(minute='45', hour='9', day_of_week='mon,tue,wed,thu,fri')
</code>```

Title: How can I create a custom crontab?

Body:
I want a crontab to run every 15 minutes starting from 9:45 to 16:00.

I currently have this:

crontab(minute='*/15', hour='9:45-16', day_of_week='mon,tue,wed,thu,fri')

but 9:45 is not valid

Is there any way to solve this?

Body:
for this purpose you cannot do this in one command it is better to do it in two cron commands

crontab(minute='*/15', hour='10-16', day_of_week='mon,tue,wed,thu,fri')
crontab(minute='45', hour='9', day_of_week='mon,tue,wed,thu,fri')