Android4,2024最新Android面试笔试题目分享

  1. String[] parts = line.split(" ");

  2. if (parts.length < 2) {

  3. throw new IOException("unexpected journal line: " + line);

  4. }

  1. String key = parts[1];

  2. if (parts[0].equals(REMOVE) && parts.length == 2) {

  3. lruEntries.remove(key);

  4. return;

  5. }

  1. Entry entry = lruEntries.get(key);

  2. if (entry == null) {

  3. entry = new Entry(key);

  4. lruEntries.put(key, entry);

  5. }

  1. if (parts[0].equals(CLEAN) && parts.length == 2 + valueCount) {

  2. entry.readable = true;

  3. entry.currentEditor = null;

  4. entry.setLengths(Arrays.copyOfRange(parts, 2, parts.length));

  5. } else if (parts[0].equals(DIRTY) && parts.length == 2) {

  6. entry.currentEditor = new Editor(entry);

  7. } else if (parts[0].equals(READ) && parts.length == 2) {

  8. // this work was already done by calling lruEntries.get()

  9. } else {

  10. throw new IOException("unexpected journal line: " + line);

  11. }

  12. }

  1. /**

  2. * Computes the initial size and collects garbage as a part of opening the

  3. * cache. Dirty entries are assumed to be inconsistent and will be deleted.

  4. */

  5. private void processJournal() throws IOException {

  6. deleteIfExists(journalFileTmp);

  7. for (Iterator<Entry> i = lruEntries.values().iterator(); i.hasNext(); ) {

  8. Entry entry = i.next();

  9. if (entry.currentEditor == null) {

  10. for (int t = 0; t < valueCount; t++) {

  11. size += entry.lengths[t];

  12. }

  13. } else {

  14. entry.currentEditor = null;

  15. for (int t = 0; t < valueCount; t++) {

  16. deleteIfExists(entry.getCleanFile(t));

  17. deleteIfExists(entry.getDirtyFile(t));

  18. }

  19. i.remove();

  20. }

  21. }

  22. }

  1. /**

  2. * Creates a new journal that omits redundant information. This replaces the

  3. * current journal if it exists.

  4. */

  5. private synchronized void rebuildJournal() throws IOException {

  6. if (journalWriter != null) {

  7. journalWriter.close();

  8. }

  1. Writer writer = new BufferedWriter(new FileWriter(journalFileTmp));

  2. writer.write(MAGIC);

  3. writer.write(“\n”);

  4. writer.write(VERSION_1);

  5. writer.write(“\n”);

  6. writer.write(Integer.toString(appVersion));

  7. writer.write(“\n”);

  8. writer.write(Integer.toString(valueCount));

  9. writer.write(“\n”);

  10. writer.write(“\n”);

  1. for (Entry entry : lruEntries.values()) {

  2. if (entry.currentEditor != null) {

  3. writer.write(DIRTY + ’ ’ + entry.key + ‘\n’);

  4. } else {

  5. writer.write(CLEAN + ’ ’ + entry.key + entry.getLengths() + ‘\n’);

  6. }

  7. }

  1. writer.close();

  2. journalFileTmp.renameTo(journalFile);

  3. journalWriter = new BufferedWriter(new FileWriter(journalFile, true));

  4. }

  1. private static void deleteIfExists(File file) throws IOException {

  2. try {

  3. Libcore.os.remove(file.getPath());

  4. } catch (ErrnoException errnoException) {

  5. if (errnoException.errno != OsConstants.ENOENT) {

  6. throw errnoException.rethrowAsIOException();

  7. }

  8. }

  9. }

  1. /**

  2. * Returns a snapshot of the entry named {@code key}, or null if it doesn’t

  3. * exist is not currently readable. If a value is returned, it is moved to

  4. * the head of the LRU queue.

  5. */

  6. public synchronized Snapshot get(String key) throws IOException {

  7. checkNotClosed();

  8. validateKey(key);

  9. Entry entry = lruEntries.get(key);

  10. if (entry == null) {

  11. return null;

  12. }

  1. if (!entry.readable) {

  2. return null;

  3. }

  1. /*

  2. * Open all streams eagerly to guarantee that we see a single published

  3. * snapshot. If we opened streams lazily then the streams could come

  4. * from different edits.

  5. */

  6. InputStream[] ins = new InputStream[valueCount];

  7. try {

  8. for (int i = 0; i < valueCount; i++) {

  9. ins[i] = new FileInputStream(entry.getCleanFile(i));

  10. }

  11. } catch (FileNotFoundException e) {

  12. // a file must have been deleted manually!

  13. return null;

  14. }

  1. redundantOpCount++;

  2. journalWriter.append(READ + ’ ’ + key + ‘\n’);

  3. if (journalRebuildRequired()) {

  4. executorService.submit(cleanupCallable);

  5. }

  1. return new Snapshot(key, entry.sequenceNumber, ins);

  2. }

  1. /**

  2. * Returns an editor for the entry named {@code key}, or null if another

  3. * edit is in progress.

  4. */

  5. public Editor edit(String key) throws IOException {

  6. return edit(key, ANY_SEQUENCE_NUMBER);

  7. }

  1. private synchronized Editor edit(String key, long expectedSequenceNumber) throws IOException {

  2. checkNotClosed();

  3. validateKey(key);

  4. Entry entry = lruEntries.get(key);

  5. if (expectedSequenceNumber != ANY_SEQUENCE_NUMBER

  6. && (entry == null || entry.sequenceNumber != expectedSequenceNumber)) {

  7. return null; // snapshot is stale

  8. }

  9. if (entry == null) {

  10. entry = new Entry(key);

  11. lruEntries.put(key, entry);

  12. } else if (entry.currentEditor != null) {

  13. return null; // another edit is in progress

  14. }

  1. Editor editor = new Editor(entry);

  2. entry.currentEditor = editor;

  1. // flush the journal before creating files to prevent file leaks

  2. journalWriter.write(DIRTY + ’ ’ + key + ‘\n’);

  3. journalWriter.flush();

  4. return editor;

  5. }

  1. /**

  2. * Returns the directory where this cache stores its data.

  3. */

  4. public File getDirectory() {

  5. return directory;

  6. }

  1. /**

  2. * Returns the maximum number of bytes that this cache should use to store

  3. * its data.

  4. */

  5. public long maxSize() {

  6. return maxSize;

  7. }

  1. /**

  2. * Returns the number of bytes currently being used to store the values in

  3. * this cache. This may be greater than the max size if a background

  4. * deletion is pending.

  5. */

  6. public synchronized long size() {

  7. return size;

  8. }

  1. private synchronized void completeEdit(Editor editor, boolean success) throws IOException {

  2. Entry entry = editor.entry;

  3. if (entry.currentEditor != editor) {

  4. throw new IllegalStateException();

  5. }

  1. // if this edit is creating the entry for the first time, every index must have a value

  2. if (success && !entry.readable) {

  3. for (int i = 0; i < valueCount; i++) {

  4. if (!entry.getDirtyFile(i).exists()) {

  5. editor.abort();

  6. throw new IllegalStateException("edit didn’t create file " + i);

  7. }

  8. }

  9. }

  1. for (int i = 0; i < valueCount; i++) {

  2. File dirty = entry.getDirtyFile(i);

  3. if (success) {

  4. if (dirty.exists()) {

  5. File clean = entry.getCleanFile(i);

  6. dirty.renameTo(clean);

  7. long oldLength = entry.lengths[i];

  8. long newLength = clean.length();

  9. entry.lengths[i] = newLength;

  10. size = size - oldLength + newLength;

  11. }

  12. } else {

  13. deleteIfExists(dirty);

  14. }

  15. }

  1. redundantOpCount++;

  2. entry.currentEditor = null;

  3. if (entry.readable | success) {

  4. entry.readable = true;

  5. journalWriter.write(CLEAN + ’ ’ + entry.key + entry.getLengths() + ‘\n’);

  6. if (success) {

  7. entry.sequenceNumber = nextSequenceNumber++;

  8. }

  9. } else {

  10. lruEntries.remove(entry.key);

  11. journalWriter.write(REMOVE + ’ ’ + entry.key + ‘\n’);

  12. }

  1. if (size > maxSize || journalRebuildRequired()) {

  2. executorService.submit(cleanupCallable);

  3. }

  4. }

  1. /**

  2. * We only rebuild the journal when it will halve the size of the journal

  3. * and eliminate at least 2000 ops.

  4. */

  5. private boolean journalRebuildRequired() {

  6. final int REDUNDANT_OP_COMPACT_THRESHOLD = 2000;

  7. return redundantOpCount >= REDUNDANT_OP_COMPACT_THRESHOLD

  8. && redundantOpCount >= lruEntries.size();

  9. }

  1. /**

  2. * Drops the entry for {@code key} if it exists and can be removed. Entries

  3. * actively being edited cannot be removed.

  4. *

  5. * @return true if an entry was removed.

  6. */

  7. public synchronized boolean remove(String key) throws IOException {

  8. checkNotClosed();

  9. validateKey(key);

  10. Entry entry = lruEntries.get(key);

  11. if (entry == null || entry.currentEditor != null) {

  12. return false;

  13. }

  1. for (int i = 0; i < valueCount; i++) {

  2. File file = entry.getCleanFile(i);

  3. if (!file.delete()) {

  4. throw new IOException("failed to delete " + file);

  5. }

  6. size -= entry.lengths[i];

  7. entry.lengths[i] = 0;

  8. }

  1. redundantOpCount++;

  2. journalWriter.append(REMOVE + ’ ’ + key + ‘\n’);

  3. lruEntries.remove(key);

  1. if (journalRebuildRequired()) {

  2. executorService.submit(cleanupCallable);

  3. }

  1. return true;

  2. }

  1. /**

  2. * Returns true if this cache has been closed.

  3. */

  4. public boolean isClosed() {

  5. return journalWriter == null;

  6. }

  1. private void checkNotClosed() {

  2. if (journalWriter == null) {

  3. throw new IllegalStateException(“cache is closed”);

  4. }

  5. }

  1. /**

  2. * Force buffered operations to the filesystem.

  3. */

  4. public synchronized void flush() throws IOException {

  5. checkNotClosed();

  6. trimToSize();

  7. journalWriter.flush();

  8. }

  1. /**

  2. * Closes this cache. Stored values will remain on the filesystem.

  3. */

  4. public synchronized void close() throws IOException {

  5. if (journalWriter == null) {

  6. return; // already closed

  7. }

  8. for (Entry entry : new ArrayList<Entry>(lruEntries.values())) {

  9. if (entry.currentEditor != null) {

  10. entry.currentEditor.abort();

  11. }

  12. }

  13. trimToSize();

  14. journalWriter.close();

  15. journalWriter = null;

  16. }

  1. private void trimToSize() throws IOException {

  2. while (size > maxSize) {

  3. Map.Entry<String, Entry> toEvict = lruEntries.eldest();

  4. remove(toEvict.getKey());

  5. }

  6. }

  1. /**

  2. * Closes the cache and deletes all of its stored values. This will delete

  3. * all files in the cache directory including files that weren’t created by

  4. * the cache.

  5. */

  6. public void delete() throws IOException {

  7. close();

  8. IoUtils.deleteContents(directory);

  9. }

  1. private void validateKey(String key) {

  2. if (key.contains(" “) || key.contains(”\n") || key.contains(“\r”)) {

  3. throw new IllegalArgumentException(

  4. “keys must not contain spaces or newlines: \”" + key + “\”");

  5. }

  6. }

  1. private static String inputStreamToString(InputStream in) throws IOException {

  2. return Streams.readFully(new InputStreamReader(in, Charsets.UTF_8));

  3. }

  1. /**

  2. * A snapshot of the values for an entry.

  3. */

  4. public final class Snapshot implements Closeable {

  5. private final String key;

  6. private final long sequenceNumber;

  7. private final InputStream[] ins;

  1. private Snapshot(String key, long sequenceNumber, InputStream[] ins) {

  2. this.key = key;

  3. this.sequenceNumber = sequenceNumber;

  4. this.ins = ins;

  5. }

  1. /**

  2. * Returns an editor for this snapshot’s entry, or null if either the

  3. * entry has changed since this snapshot was created or if another edit

  4. * is in progress.

  5. */

  6. public Editor edit() throws IOException {

  7. return DiskLruCache.this.edit(key, sequenceNumber);

  8. }

  1. /**

  2. * Returns the unbuffered stream with the value for {@code index}.

  3. */

  4. public InputStream getInputStream(int index) {

  5. return ins[index];

  6. }

  1. /**

  2. * Returns the string value for {@code index}.

  3. */

  4. public String getString(int index) throws IOException {

  5. return inputStreamToString(getInputStream(index));

  6. }

  1. @Override public void close() {

  2. for (InputStream in : ins) {

  3. IoUtils.closeQuietly(in);

  4. }

  5. }

  6. }

  1. /**

  2. * Edits the values for an entry.

  3. */

  4. public final class Editor {

  5. private final Entry entry;

  6. private boolean hasErrors;

  1. private Editor(Entry entry) {

  2. this.entry = entry;

  3. }

  1. /**

  2. * Returns an unbuffered input stream to read the last committed value,

  3. * or null if no value has been committed.

  4. */

  5. public InputStream newInputStream(int index) throws IOException {

  6. synchronized (DiskLruCache.this) {

  7. if (entry.currentEditor != this) {

  8. throw new IllegalStateException();

  9. }

  10. if (!entry.readable) {

  11. return null;

  12. }

  13. return new FileInputStream(entry.getCleanFile(index));

  14. }

  15. }

  1. /**

  2. * Returns the last committed value as a string, or null if no value

  3. * has been committed.

  4. */

  5. public String getString(int index) throws IOException {

  6. InputStream in = newInputStream(index);

  7. return in != null ? inputStreamToString(in) : null;

  8. }

  1. /**

  2. * Returns a new unbuffered output stream to write the value at

  3. * {@code index}. If the underlying output stream encounters errors

  4. * when writing to the filesystem, this edit will be aborted when

  5. * {@link #commit} is called. The returned output stream does not throw

  6. * IOExceptions.

  7. */

  8. public OutputStream newOutputStream(int index) throws IOException {

  9. synchronized (DiskLruCache.this) {

  10. if (entry.currentEditor != this) {

  11. throw new IllegalStateException();

  12. }

  13. return new FaultHidingOutputStream(new FileOutputStream(entry.getDirtyFile(index)));

  14. }

  15. }

  1. /**

  2. * Sets the value at {@code index} to {@code value}.

  3. */

  4. public void set(int index, String value) throws IOException {

  5. Writer writer = null;

  6. try {

  7. writer = new OutputStreamWriter(newOutputStream(index), Charsets.UTF_8);

  8. writer.write(value);

  9. } finally {

  10. IoUtils.closeQuietly(writer);

  11. }

  12. }

  1. /**

  2. * Commits this edit so it is visible to readers.  This releases the

  3. * edit lock so another edit may be started on the same key.

  4. */

  5. public void commit() throws IOException {

  6. if (hasErrors) {

  7. completeEdit(this, false);

  8. remove(entry.key); // the previous entry is stale

  9. } else {

  10. completeEdit(this, true);

  11. }

  12. }

  1. /**

  2. * Aborts this edit. This releases the edit lock so another edit may be

  3. * started on the same key.

  4. */

  5. public void abort() throws IOException {

  6. completeEdit(this, false);

  7. }

  1. private class FaultHidingOutputStream extends FilterOutputStream {

  2. private FaultHidingOutputStream(OutputStream out) {

  3. super(out);

  4. }

  1. @Override public void write(int oneByte) {

  2. try {

  3. out.write(oneByte);

  4. } catch (IOException e) {

  5. hasErrors = true;

  6. }

  7. }

  1. @Override public void write(byte[] buffer, int offset, int length) {

  2. try {

  3. out.write(buffer, offset, length);

  4. } catch (IOException e) {

  5. hasErrors = true;

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

最后

代码真的是重质不重量,质量高的代码,是当前代码界提倡的,当然写出高质量的代码肯定需要一个相当高的专业素养,这需要在日常的代码书写中逐渐去吸收掌握,谁不是每天都在学习呀,目的还不是为了一个,为实现某个功能写出高质量的代码。

所以,长征路还长,大家还是好好地做个务实的程序员吧。

最后,小编这里有一系列Android提升学习资料,有兴趣的小伙伴们可以来看下哦~

completeEdit(this, false);

  1. }

  1. private class FaultHidingOutputStream extends FilterOutputStream {

  2. private FaultHidingOutputStream(OutputStream out) {

  3. super(out);

  4. }

  1. @Override public void write(int oneByte) {

  2. try {

  3. out.write(oneByte);

  4. } catch (IOException e) {

  5. hasErrors = true;

  6. }

  7. }

  1. @Override public void write(byte[] buffer, int offset, int length) {

  2. try {

  3. out.write(buffer, offset, length);

  4. } catch (IOException e) {

  5. hasErrors = true;

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-900vESPw-1712070700826)]
[外链图片转存中…(img-BNsFUaNa-1712070700827)]
[外链图片转存中…(img-ghxYW02e-1712070700827)]
[外链图片转存中…(img-l86SSqDE-1712070700827)]
[外链图片转存中…(img-I06OVIe0-1712070700828)]
[外链图片转存中…(img-oq9F2OH5-1712070700828)]
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-EdMaOXBB-1712070700828)]

最后

代码真的是重质不重量,质量高的代码,是当前代码界提倡的,当然写出高质量的代码肯定需要一个相当高的专业素养,这需要在日常的代码书写中逐渐去吸收掌握,谁不是每天都在学习呀,目的还不是为了一个,为实现某个功能写出高质量的代码。

所以,长征路还长,大家还是好好地做个务实的程序员吧。

最后,小编这里有一系列Android提升学习资料,有兴趣的小伙伴们可以来看下哦~

本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

  • 29
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值